如何访问theano.tensor.var.TensorVariable?
假设我有一个大小为(1152, 10)的矩阵w,长得像这样:
>>> w.get_value(True)
array([[-0.03824838, -0.02033614, 0.040734 , ..., 0.01585871,
0.04165901, 0.01058411],
[-0.00626427, 0.00891617, 0.01286055, ..., 0.00184506,
-0.01282589, -0.00209718],
[ 0.00457122, -0.01036582, 0.02780926, ..., 0.01269533,
-0.00953711, -0.00271188],
...,
[ 0.00592541, -0.00267455, 0.02258315, ..., -0.00788802,
0.02260087, -0.01107418],
[-0.02363299, 0.02963436, 0.02735142, ..., -0.01933786,
-0.03731941, 0.02085613],
[-0.0079082 , 0.01099584, 0.01910999, ..., 0.00122137,
-0.006866 , -0.01500945]])
还有一个大小为(1152, 1)的输入,长得像这样:
>> input.get_value(True)
array([ 0., 0., 0., ..., 0., 0., 0.])
现在我想计算它们的点乘,像这样:
>> result = theano.tensor.dot(image, w)
结果是:
>>> result
dot.0
>>> type(result)
<class 'theano.tensor.var.TensorVariable'>
>>> type(image)
<class 'theano.tensor.sharedvar.TensorSharedVariable'>
>>> type(classifier.W)
<class 'theano.tensor.sharedvar.TensorSharedVariable'>
那theano.tensor.dot是返回一个符号表达式而不是具体的数值吗?
1 个回答
6
一句话:是的。
要查看操作的结果,可以使用 result.eval()
下面是一个简单的示例:
import numpy as np
import theano
from theano import tensor as T
w_values = np.random.randn(1152, 10).astype(theano.config.floatX)
input_values = np.random.randn(1152, 1).astype(theano.config.floatX)
w = theano.shared(w_values, 'w')
input = theano.shared(input_values, 'input')
result = T.dot(input.T, w)
print(result.eval())