问题描述
在theano
中编译函数时,可以通过指定updates=[(X, new_value)]
来更新共享变量(例如X).现在,我仅尝试更新共享变量的子集:
When compiling a function in theano
, a shared variable(say X) can be updated by specifying updates=[(X, new_value)]
.Now I am trying to update only subset of a shared variable:
from theano import tensor as T
from theano import function
import numpy
X = T.shared(numpy.array([0,1,2,3,4]))
Y = T.vector()
f = function([Y], updates=[(X[2:4], Y)] # error occur:
# 'update target must
# be a SharedVariable'
代码将引发错误更新目标必须是SharedVariable",我想这意味着更新目标不能是非共享变量.那么,有什么方法可以编译一个函数来udpate共享变量的子集?
The codes will raise a error "update target must be a SharedVariable", I guess that means update targets can't be non-shared variables. So is there any way to compile a function to just udpate subset of shared variables?
推荐答案
使用 set_subtensor 或 inc_subtensor :
from theano import tensor as T
from theano import function, shared
import numpy
X = shared(numpy.array([0,1,2,3,4]))
Y = T.vector()
X_update = (X, T.set_subtensor(X[2:4], Y))
f = function([Y], updates=[X_update])
f([100,10])
print X.get_value() # [0 1 100 10 4]
Theano FAQ中现在有一个与此有关的页面: http://deeplearning.net /software/theano/tutorial/faq_tutorial.html
There's now a page about this in the Theano FAQ: http://deeplearning.net/software/theano/tutorial/faq_tutorial.html
这篇关于如何在Theano中分配/更新张量共享变量的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!