>>> import theano.tensor as T
>>> x = T.dscalar('x') # 定义了一个名字叫“x”的double类型的标量,并将该符号变量的值赋给了变量x。>>> y = T.dvector('y')
>>> z = T.imatrix('z')
'''上面的三句都是定义了theano的符号变量,其中后面括号中的('x')等是该符号变量的名字,是给theano看的
(theano的computing graph 中的名字),然后将符号变量的值赋给等号前面的x,此x是python的普通变量。注意区分'''
2.function 函数,对应着computing graph 中的operation操作
举例子,每一句后面有详细解释:
>>> import numpy
>>> 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) # 第一个参数是一个list,对应着该function的输入列表,第二个参数对应着输出。'''此函数中输入为符号变量名字为x和y,输出为名字为z的符号变量,此操作并没有执行,下面调用函数以后才执行'''>>> f(2, 3) # 调用函数,对应着computing graph中的Apply,此时函数才会执行。
array(5.0) # 函数执行结果。
注:运行函数 f = function([x, y], z) 会花费比较长的时间,theano会将函数构建成计算图,并且做一些优化。详细过程可见计算图部分。
>>> fn_of_state = state * 2 + inc
>>> # The type of foo must match the shared variable we are replacing>>> # with the ``givens``>>> foo = T.scalar(dtype=state.dtype)
>>> skip_shared = function([inc, foo], fn_of_state, givens=[(state, foo)])
>>> skip_shared(1, 3) # we're using 3 for the state, not state.value
array(7) # 结果>>> print(state.get_value()) # old state still there, but we didn't use it0# 结果是0,而不是上面调用函数时传入的3,在computing graph中实际上用foo替换了state。