Reputation: 2744
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?
Upvotes: 1
Views: 2000
Reputation: 34177
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
y = theano.shared(numpy.asarray(data, dtype=theano.config.floatX))
y = theano.tensor.cast(y, 'int32')
Use
y = theano.shared(numpy.asarray(data, dtype='int32'))
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:
y.set_value(np.asarray(data2, dtype='int32'))
Note that the casting is happening in numpy again, instead of Theano.
Upvotes: 4