在Theano中更新共享变量的一部分 [英] Updating part of a shared variable in Theano

查看:94
本文介绍了在Theano中更新共享变量的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Theano中更新部分共享变量?

How to update part of a shared variable in Theano?

例如而不是:

gradient_W = T.grad(cost,W)
updates.append((W, W-learning_rate*gradient_W))
train = th.function(inputs=[index], outputs=[cost], updates=updates,
                            givens={x:self.X[index:index+mini_batch_size,:]})

我只想更新W的一部分,例如仅第一列:

I'd like to only update part of W, e.g. only the first column:

 updates.append((W[:, 0], W[:, 0]-learning_rate*gradient_W))

但是这样做会导致错误TypeError: ('update target must be a SharedVariable', Subtensor{::, int64}.0):

But doing so gives the error TypeError: ('update target must be a SharedVariable', Subtensor{::, int64}.0):

Traceback (most recent call last):
  File "ae.py", line 330, in <module>
    main()
  File "ae.py", line 305, 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 87, 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 194, in rebuild_collect_shared
    store_into)
TypeError: ('update target must be a SharedVariable', Subtensor{::, int64}.0)

在Theano中这样做的典型方法是什么?

What is the typical way to do so in Theano?

W是典型的权重矩阵:

initial_W = np.asarray(rng.uniform(
                 low=-4 * np.sqrt(6. / (self.hidden_size + self.n)),
                 high=4 * np.sqrt(6. / (self.hidden_size + self.n)),
                 size=(self.n, self.hidden_size)), dtype=th.config.floatX)
W = th.shared(value=initial_W, name='W', borrow=True)

推荐答案

例如,您可以使用theano.tensor.set_subtensor.

采用您上面提到的更新行,它将变为:

Taking the updates line you mention above, this becomes:

updates.append((W, T.set_subtensor(W[:, 0], W[:, 0]-learning_rate*gradient_W)))

其中T = theano.tensor.

T.inc_subtensor是另一种可能的情况,在您的情况下更为简洁一些:您仅指定增量而不是新值:

Another possibility, a little more concise in your case, is T.inc_subtensor, where you specify only the increment instead of the new value:

updates.append((W, T.inc_subtensor(W[:, 0], -learning_rate*gradient_W)))

这篇关于在Theano中更新共享变量的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆