How to Using Theano
theanoのfunctionを使用して式を生成する場合は、scalar、vector、matrixなどの多くのタイプのsymbol変数を宣言する必要があります.
次のベクトル計算式を実行する場合は、次のようになります.
out = a^2 + b^2 + 2*a*b
次のコードを使用して完了できます.
これによりsigmoid関数,sigmoid(x)=1/(1+exp(-x))を容易に実現できる.
次のベクトル計算式を実行する場合は、次のようになります.
out = a^2 + b^2 + 2*a*b
次のコードを使用して完了できます.
In [1]: import theano.tensor as T
In [2]: from theano import function
In [3]: a = T.dvector('a')
In [4]: b = T.dvector('b')
In [5]: out = a**2 + b**2 + 2*a*b
In [6]: f = function([a, b], out)
In [7]: f([1, 2], [3, 4])
Out[7]: array([ 16., 36.])
これによりsigmoid関数,sigmoid(x)=1/(1+exp(-x))を容易に実現できる.
In [8]: x = T.dmatrix('x')
In [9]: s = 1 / (1 + T.exp(-x))
In [10]: logistic = function([x], s)
In [11]: logistic([[0,1], [1, 2], [-1, -2]])
Out[11]:
array([[ 0.5 , 0.73105858],
[ 0.73105858, 0.88079708],
[ 0.26894142, 0.11920292]])