In the Theano deep learning tutorial, y is a shared variable that is casted:
y = theano.shared(numpy.asarray(data, dtype=theano.config.floatX))
y = theano.tensor.cast(y, 'int32')
I later want to set a new value for y.
For GPU this works:
y.owner.inputs[0].owner.inputs[0].set_value(np.asarray(data2, dtype=theano.config.floatX))
For CPU this works:
y.owner.inputs[0].set_value(np.asarray(data2, dtype=theano.config.floatX))
Why does this require a different syntax between GPU and CPU? I would like my code to work for both cases, am I doing it wrong?
This is a very similar problem to that described in another StackOverflow question.
The problem is that you are using a symbolic cast operation which turns the shared variable into a symbolic variable.
The solution is to cast the shared variable's value rather than the shared variable itself.
Instead of
Use
Navigating the Theano computational graph via the
owner
attribute is considered bad form. If you want to alter the shared variable's value, maintain a Python reference to the shared variable and set its value directly.So, with y being just a shared variable, and not a symbolic variable, you can now just do:
Note that the casting is happening in numpy again, instead of Theano.