6

我正在使用 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.]]))

谢谢你的时间!

4

3 回答 3

5

我遇到了类似的错误,并且能够通过添加.theanorc包含以下两行的文件来解决它:

[global]

floatX = float32

这似乎解决了一切。但是,您的问题显示了一个稍微不同的错误。但我认为值得一试。

于 2015-01-16T22:25:59.767 回答
2

这个答案来自Theano-users google group

您将x变量定义为:

x=T.vector(dtype=theano.config.floatX)

这是一个向量(即它只有一维)。

x_inp = np.matrix('2;1',dtype=dt)

创建一个矩阵,而不是一个向量。

Theano 图是强类型的,你必须定义好的维数。只需使用:

x_inp = np.asarray([2,1]) 

我实际上最终将x和定义b为矩阵。

于 2014-04-08T07:53:35.570 回答
1

错误看起来不言自明;你有没有尝试过:

dt = np.dtype(np.float32) 

??

于 2014-04-07T14:07:50.993 回答