Search code examples
gpucpusharedtheano

Different results of CPU and GPU with Theano


I have the following piece of code:

import theano
import theano.tensor as T
import numpy as np

x = theano.shared(np.asarray([1, 2, 3], dtype=theano.config.floatX), borrow=True)
y = T.cast(x, 'int32')
print 'type of y: ', type(y)
print 'type of y.owner.inputs[0]: ', type(y.owner.inputs[0])
print 'value of y: ', y.owner.inputs[0].get_value(borrow=True)

Run with CPU

$ THEANO_FLAGS=mode=FAST_RUN,device=cpu,floatX=float32 python test_share.py
type of y:  <class 'theano.tensor.var.TensorVariable'>
type of y.owner.inputs[0]:  <class 'theano.tensor.sharedvar.TensorSharedVariable'>
value of y:  [ 1.  2.  3.]

Run with GPU

$ THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python test_share.py
Using gpu device 0: GeForce 310M
type of y:  <class 'theano.tensor.var.TensorVariable'>
type of y.owner.inputs[0]:  <class 'theano.tensor.var.TensorVariable'>
value of y:
Traceback (most recent call last):
  File "test_share.py", line 10, in <module>
    print 'value of y: ', y.owner.inputs[0].get_value(borrow=True)
AttributeError: 'TensorVariable' object has no attribute 'get_value'

How can I get the same results as CPU?


Solution

  • The method you are using to access child nodes in the computation graph, .owner.inputs[0], is not appropriate for cross-platform code in general.

    You call the shared variable x so the correct way to access x's value is to use x.get_value().

    This code should run the same on CPU and GPU:

    import theano
    import theano.tensor as T
    import numpy as np
    
    x = theano.shared(np.asarray([1, 2, 3], dtype=theano.config.floatX), borrow=True)
    y = T.cast(x, 'int32')
    print 'type of y: ', type(y)
    print 'type of x: ', type(x)
    print 'value of x: ', x.get_value(borrow=True)
    

    If you want to see the result of applying the symbolic cast operation to x, the symbolic result of which you call y, then you could do this:

    print 'value of y: ', y.eval()