2015-07-11 109 views
11

我有一个从施放共享变量创建的theano张量变量。我怎样才能提取原始或铸造值? (我需要让我不必随身携带的原始共享/ numpy的值。)如何从共享变量支持的theano张量变量中获取值?

>>> x = theano.shared(numpy.asarray([1, 2, 3], dtype='float')) 
>>> y = theano.tensor.cast(x, 'int32') 
>>> y.get_value(borrow=True) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
AttributeError: 'TensorVariable' object has no attribute 'get_value' 
# whereas I can do this against the original shared variable 
>>> x.get_value(borrow=True) 
array([ 1., 2., 3.]) 

回答

13

get_value仅适用于共享变量。 TensorVariables是一般的表达,因此可能需要为了能够确定其价值的额外输入(想象一下,你设置y = x + z,其中z是另一张量变量,你需要能够计算y前指定z)。您可以创建一个函数来提供此输入或使用eval方法在字典中提供它。

在你的情况,y只取决于x,所以你可以做

import theano 
import theano.tensor as T 

x = theano.shared(numpy.asarray([1, 2, 3], dtype='float32')) 
y = T.cast(x, 'int32') 
y.eval() 

,你应该看到的结果

array([1, 2, 3], dtype=int32) 

(在的情况下y = x + z,你就必须做例如,y.eval({z : 3.})