如何从共享变量支持的 theano 张量变量中获取值?
How to get value from a theano tensor variable backed by a shared variable?
我有一个通过转换共享变量创建的 theano 张量变量。如何提取原始值或铸造值? (我需要它,这样我就不必随身携带原始 shared/numpy 值。)
>>> 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.])
get_value
仅适用于共享变量。 TensorVariables
是一般表达式,因此可能需要额外的输入才能确定它们的值(假设您设置了 y = x + z
,其中 z
是另一个张量变量。您需要指定 z
才能够计算 y
)。您可以创建一个函数来提供此输入,也可以使用 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.})
,例如)
我有一个通过转换共享变量创建的 theano 张量变量。如何提取原始值或铸造值? (我需要它,这样我就不必随身携带原始 shared/numpy 值。)
>>> 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.])
get_value
仅适用于共享变量。 TensorVariables
是一般表达式,因此可能需要额外的输入才能确定它们的值(假设您设置了 y = x + z
,其中 z
是另一个张量变量。您需要指定 z
才能够计算 y
)。您可以创建一个函数来提供此输入,也可以使用 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.})
,例如)