【问题标题】:Different results of CPU and GPU with TheanoCPU 和 GPU 使用 Theano 的不同结果
【发布时间】:2015-12-24 14:53:57
【问题描述】:

我有以下代码:

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)

使用 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.]

使用 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'

如何才能获得与 CPU 相同的结果?

【问题讨论】:

    标签: gpu cpu shared theano


    【解决方案1】:

    您用于访问计算图中子节点的方法.owner.inputs[0] 通常不适用于跨平台代码。

    您调用共享变量x,因此访问x 值的正确方法是使用x.get_value()

    这段代码应该在 CPU 和 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)
    

    如果您想查看将符号转换操作应用于x 的结果,即您调用y 的符号结果,那么您可以这样做:

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

    【讨论】:

      猜你喜欢
      • 2016-07-16
      • 2014-10-19
      • 2015-07-24
      • 2017-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2015-10-01
      相关资源
      最近更新 更多