在调用theano.function()
时,有没有办法在同一个共享变量上定义多个更新?
示例:
updates = []
updates.append([(self.W, self.W - 1)])
updates.append([(self.W, self.W - 2)])
train = th.function(inputs=[index], outputs=[cost], updates=updates)
会抛出错误this shared variable already has an update expression
:
ignore_bug_before` to at least "0.7".
tolerate_inplace_aliasing=tolerate_inplace_aliasing)
Traceback (most recent call last):
File "ae.py", line 340, in <module>
main()
File "ae.py", line 315, in main
ae.train(n_epochs=n_epochs, mini_batch_size=100, learning_rate=0.002, train_data= train_sentence_embeddings, test_data= test_sentence_embeddings)
File "ae.py", line 97, in train
givens={x:self.X[index:index+mini_batch_size,:]})
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function.py", line 266, in function
profile=profile)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 489, in pfunc
no_default_updates=no_default_updates)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 198, in rebuild_collect_shared
(store_into, update_d[store_into]))
ValueError: ('this shared variable already has an update expression', (W, DimShuffle{1,0}.0))
我有兴趣这样做,因为我有权重矩阵,我需要更新它的不同部分有不同的更新(我使用set_subtensor
)。
答案 0 :(得分:4)
正如theano-users邮件列表this thread中所解释的那样......
您需要链接set_subtensor
,以便最终只在更新列表中为整个张量输入一个条目。
例如(代码不像原版,因为原版实际上并没有在任何地方包含set_subtensor):
new_w = self.W
new_W = theano.tensor.set_subtensor(new_W[0], -1)
new_W = theano.tensor.set_subtensor(new_W[1], -2)
updates = [(self.W, new_W)]
train = theano.function(inputs=[index], outputs=[cost], updates=updates)