【发布时间】:2014-05-19 20:03:36
【问题描述】:
我正在使用 numpy 1.9 和 Theano 的最新版本编写此代码,但我收到一个无法修复的错误。我怀疑这可能是我声明变量类型的方式,但我无法解决它。我很欣赏你的建议。我想用一个向量产生一个矩阵并将它与一个偏差相加。
import theano.tensor as T
from theano import function
import numpy as np
import pprint
def test_theano_matrix():
pp = pprint.PrettyPrinter(indent=3)
W= T.fmatrix()
x=T.fvector()
b= T.fvector()
y = T.dot(W,x) + b
lin_func = function([W,x,b],y)
dt = np.dtype(np.float)
w_inp = np.matrix('1 0;0 1',dtype=dt)
x_inp = np.matrix('2;1',dtype=dt)
b_inp = np.matrix('0;0',dtype=dt)
lin_func(w_inp,x_inp,b_inp)
if __name__ == '__main__':
test_theano_matrix()
我收到以下错误:
raise TypeError(err_msg, data)
TypeError: ('Bad input argument to theano function at index 0(0-based)',
'TensorType(float32, matrix) cannot store a value of dtype float64 without risking loss of precision. If you do not mind this loss, you can: 1) explicitly cast your data to float32, or 2) set "allow_input_downcast=True" when calling "function".', matrix([[ 1., 0.],[ 0., 1.]]))
感谢您的宝贵时间!
【问题讨论】:
-
为什么不把
dt = np.type(np.float)改成np.float64呢?此外,回溯明确告诉您可以自己解决问题的两种方法... -
它将错误更改为
TypeError: ('Bad input argument to theano function at index 1(0-based)', 'Wrong number of dimensions: expected 1, got 2 with shape (2, 1).') -
再次,回溯告诉您问题...第一个错误(在索引 0 处)已通过使用正确的 dtype 修复。现在,它抱怨索引 1 处的参数
x_inp,它告诉您形状错误。 -
W_inp 是 2x2 矩阵,x_inp 是 2x1,所以我不明白它为什么抱怨形状。
标签: python numpy neural-network theano