theano 常见函数困惑介绍
1、theano.tensor.function 函数
a、利用function函数计算映射时,先以一个表达数学表达式建立映射,然后再以数学表达式的自变量、因变量为参数构建function函数
import theano.tensor as T x = T.dmatrix('x') s = 1/(1+T.exp(-x)) logistic = theano.function([x], s)
b、可以在一个函数中计算多种映射
import theano.tensor as T a, b=T.dscala('a', 'b') diff = a- b abs _ diff = abs(diff) diff_squared = diff**2 f= theano.function([x],[diff, abs_diff, diff_squared])
c、在function函数种还可以设置参数的默认值,通过In函数
x, y, w = T.dscalars('x', 'y', 'w') z = (x + y) * w f = function([x, In(y, value=1), In(w, value=2, name='w_by_name')], z)
d、共享变量
利用共享变量可以在function实现一个内部状态参数,通过在function函数种引入第三个参数updates,可以实现对共享变量的值进行更新,updates的一般为一个二元组合,记录了对变量的更新情况。
from theano import share state = shared(0) inc = T.iscala('inc') accumulator = function([inc],state, updates[(state, state + inc)])
e、其他还有一些参数待更新如no_default_updates=True,该参数涉及到了随机状态信息,待更新
2、theano.scan 函数
scan函数是一个很重要的函数,该函数用以实现循环处理:可以是循环k次,或者对序列种的每一个元素进行循环处理。
scan函数中重要的参数有:fn、sequences、non_sequence、n_steps、outputs_info
- fn:循环执行的函数,函数接受的参数顺序为sequences中的序列,outputs_info中的序列,最后是non_sequences中序列。所以在定义函数时,要注意输入参数的顺序。
- sequences:输入序列,可以是多个序列,循环每次从每个序列中取一个元素作为fn函数的输入,第一个序列取的元素作为函数第一个参数,第二个作为函数第二参数,以此类推。
- non_sequences:循环中不会改变的数值,即每次循环取值一样。
- n_steps:循环次数
- outputs_info:这个参数比较复杂。首先明确一点,如果sequences中有i个序列的话,那么outputs_info中第一个参数将作为fn函数的第i+1个输入参数,还有outputs_info中第一个参数代表fn函数前一次循环中返回的第一个值。所以,outputs_info需要赋初值。也就是说,如果outputs_info=[a,…],表示在第一次循环中fn的第i+1个输入参数为a(初值),而第t次循环的时候第i+1个输入参数的值为fn函数在第t-1次循环中返回的一个值。
以下为几个scan函数的例子
a、 tanh(x(t).dot(W) + b)
import theano import theano.tensor as T import numpy as np X = T.matrix("X") W = T.matrix("W") b_sym = T.vector("b_sym") results, updates = theano.scan(lambda v: T.tanh(T.dot(v, W) + b_sym), sequences = X) compute_elementwise = theano.function(inputes = [X,W, b_sym], outputs=results)
b、计算序列x(t) = x(t - 2).dot(U) + x(t - 1).dot(V) + tanh(x(t - 1).dot(W) + b)
import theano import theano.tensor as T import numpy as np # define tensor variables X = T.matrix("X") W = T.matrix("W") b_sym = T.vector("b_sym") U = T.matrix("U") V = T.matrix("V") n_sym = T.iscalar("n_sym") results, updates = theano.scan(lambda x_tm2, x_tm1: T.dot(x_tm2, U) + T.dot(x_tm1, V) + T.tanh(T.dot(x_tm1, W) + b_sym), n_steps=n_sym, outputs_info=[dict(initial=X, taps=[-2, -1])]) compute_seq2 = theano.function(inputs=[X, U, V, W, b_sym, n_sym], outputs=results)
c、多项式
import numpy import theano import theano.tensor as T theano.config.warn.subtensor_merge_bug = False coefficients = theano.tensor.vector("coefficients") x = T.scalar("x") max_coefficients_supported = 10000 # Generate the components of the polynomial full_range=theano.tensor.arange(max_coefficients_supported) components, updates = theano.scan(fn=lambda coeff, power, free_var: coeff * (free_var ** power), outputs_info=None, sequences=[coefficients, full_range], non_sequences=x) polynomial = components.sum() calculate_polynomial = theano.function(inputs=[coefficients, x], outputs=polynomial)

浙公网安备 33010602011771号