标量相加
import theano.tensor as T
from theano import function
x = T.dscalar('x')
y = T.dscalar('y')
z = x + y
f = function([x, y], z)
输入定义两个符号变量来代替数值,输出是一个0维的numpy.ndarray数组。
矩阵相加
把输入类型换一下就行了,矩阵如果维数不同,会遵循NumPy的广播规则。
import theano.tensor as T
from theano import function
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)
定义一个公式如:a ** 2 + b ** 2 + 2 * a* b
这里每个变量都需要单独申明。
import theano
a = theano.tensor.vector()
b = theano.tensor.vector()
out = a ** 2 + b ** 2 + 2 * a * b
f = theano.function([a,b],out)
print f([0, 1],[1,2])
>>>
[ 1. 9.]
支持多输出
import theano.tensor as T
from theano import function
a, b = T.dmatrices('a', 'b')
diff = a - b