作者:东隅海纳堂_684 | 来源:互联网 | 2023-10-13 12:03
theano 下的 functions (from theano import function)的两个重要性质: 多个 outputs; 多个 updates; 1. shared shared 与 theano.tensor.matrix()/vector() 的区别在于,tensor.matrix/vector 符号变量不需要初始化 ,而 shared 变量则需要初始化赋值 ; >>> import theano >>> import theano.tensor as T >>> state = theano.shared(0 ) >>> type(state) theano.tensor.sharedvar.ScalarSharedVariable>>> state.get_value(borrow=True ) array(0 )>>> x = T.iscalar('x' )>>> type(x) theano.tensor.var.TensorVariable>>> type(x+state) theano.tensor.var.TensorVariable
2. updates >>> counter = function ([x], state, updates=[(state, state+x) ]) >>> counter (5) >>> state .get_value (borrow=True ) 5 >>> counter (6) >>> state .get_value (borrow=True ) 11
不使用可选参数updates:
>>> state += x>>> counter = theano.function([x], state)>>> counter(5 ) array(5 )>>> state.get_value() AttributeError: 'TensorVariable' object has no attribute 'get_value'
theano.function 的 updates, 二元 tuple 构成的 list ,
[(a, a+1 ), (b, b+1 )]
其含义其实为,a = a+1, b = b+1;
所以常见与,梯度下降算法中的权值的更新,如下:
updates = [(W, W-eta*delta_W ),(b, b-eta*delta_b )]
3. givens test_model = theano.function(inputs=[idx],outputs=layer3.error(y ),givens={x : test_set_x[idx*batch_size :(idx+1 )*batch_size ],y : test_set_y[idx*batch_size :(idx+1 )*batch_size ]} ) valid_model = theano.function(inputs=[idx],outputs=layer3.error(y ),givens={x : valid_set_x[idx*batch_size :(idx+1 )*batch_size ],y : valid_set_y[idx*batch_size :(idx+1 )*batch_size ]} )