Theano:这个共享变量已经有一个更新表达式

Theano: this shared variable already has an update expression

调用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))

我有兴趣这样做,因为我有权重矩阵,我需要更新它的不同部分有不同的更新(我使用 进行更新)。

如 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)