How to Using Theano

来源:互联网 发布:中国服务贸易数据 编辑:程序博客网 时间:2024/05/22 08:16

使用theano的function生成公式的时候,需要首先声明symbol(variable)变量,这个变量可以是很多类型,比如scalar、vector或者matrix等类型。

如果要实现如下的向量计算公式:

out = a^2 + b^2 + 2*a*b

则可以使用下面的代码完成:

In [1]: import theano.tensor as TIn [2]: from theano import functionIn [3]: a = T.dvector('a')In [4]: b = T.dvector('b')In [5]: out = a**2 + b**2 + 2*a*bIn [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]])



0 0
原创粉丝点击