python 2.7 - Can not update a subset of shared tensor variable after a cast -
i have following code:
import theano.tensor 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 compiling fine (where u numpy array of dtype float64).
to prevent future type error want cast shared tensor words float32 (or theano.config.floatx equivalent have set floatx float32 in config file).
i add words = t.cast(words, dtype = theano.config.floatx) , following error:
typeerror: ('update target must sharedvariable', elemwise{cast{float32}}.0).
i not understand why. according question, using set_subtensor should allow me update subset of shared variable.
how can cast shared tensor while being able update it?
the problem trying update symbolic variable, not 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 thing updating, words 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 words no longer shared variable, symbolic variable that, when executed, compute cast values in shared variable theano.config.floatx.
the dtype of shared variable determined value assigned it. need change type of u:
u = np.array([[1, 2, 3], [4, 5, 6]], dtype=theano.config.floatx) or cast using numpy instead of symbolically:
u = np.dtype(theano.config.floatx).type(u)
Comments
Post a Comment