Can not update a subset of shared tensor variable after a cast

百般思念 提交于 2019-12-10 10:58:32

问题


I have the following code:

import theano.tensor as T

Words = theano.shared(value = U, name = 'Words')
zero_vec_tensor = T.vector()
zero_vec = np.zeros(img_w, dtype = theano.config.floatX)
set_zero = theano.function([zero_vec_tensor], updates=[(Words, T.set_subtensor(Words[0,:], zero_vec_tensor))])

Which is compiling fine (where U is a numpy array of dtype float64).

To prevent future type error I want to cast my shared tensor Words into float32 (or theano.config.floatX which is equivalent as I have set floatX to float32 in the config file).

I so add Words = T.cast(Words, dtype = theano.config.floatX) and then I get the following error:

TypeError: ('update target must be a SharedVariable', Elemwise{Cast{float32}}.0).

I do not understand why. According to this question, using set_subtensor should allow me to update a subset of the shared variable.

How can I cast my shared tensor while being able to update it?


回答1:


The problem is that you are trying to update a symbolic variable, not a shared variable.

U = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
Words = theano.shared(value=U, name='Words')
zero_vec_tensor = T.vector()
set_zero = theano.function([zero_vec_tensor], updates=[(Words, T.set_subtensor(Words[0, :], zero_vec_tensor))])

works fine because the thing you are updating, Words is a shared variable.

U = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.float64)
Words = theano.shared(value=U, name='Words')
Words = T.cast(Words, dtype = theano.config.floatX)
zero_vec_tensor = T.vector()
set_zero = theano.function([zero_vec_tensor], updates=[(Words, T.set_subtensor(Words[0, :], zero_vec_tensor))])

does not work because now Words is no longer a shared variable, it is a symbolic variable that, when executed, will compute a cast the values in the shared variable to theano.config.floatX.

The dtype of a shared variable is determined by the value assigned to it. So you probably just need to change the type of U:

U = np.array([[1, 2, 3], [4, 5, 6]], dtype=theano.config.floatX)

Or cast it using numpy instead of symbolically:

U = np.dtype(theano.config.floatX).type(U)


来源:https://stackoverflow.com/questions/30627302/can-not-update-a-subset-of-shared-tensor-variable-after-a-cast

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!