我看到一个错别字

Theano TypeError

我正在读取 jpg 图像,然后将它们重塑为张量。我将图像投射为 float32:

def load(folder,table):
X=[]

train = pd.read_csv(table)

for i,img_id in enumerate(train['Image']):

    img = io.imread(folder+img_id[2:])

    X.append(img)

X = np.array(X)/255.
X = X.astype(np.float32)
X = X.reshape(-1, 1, 225, 225)
return X

但是,我遇到了这个错误

TypeError: ('Bad input argument to theano function with name "/Users/mas/PycharmProjects/Whale/nolearn_convnet/Zahraa5/lib/python2.7/site-packages/nolearn/lasagne/base.py:435"  at index 1(0-based)', 'TensorType(int32, vector) cannot store a value of dtype float32 without risking loss of precision. If you do not mind this loss, you can: 1) explicitly cast your data to int32, or 2) set "allow_input_downcast=True" when calling "function".', array([[ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       ..., 
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],
       [ 0.,  0.,  0., ...,  0.,  0.,  0.],

       [ 0.,  0.,  0., ...,  0.,  0.,  0.]], dtype=float32))

这是一个cross-post to the theano-users mailing list

Doug 在那里提供了答案:

The theano variable you are using is defined as integer, but you passed in a float, hence the error 'TensorType(int32, vector) cannot store a value of dtype float32...'. You can either modify your data loading code to cast it as int32, or change the symbolic variable to something that supports float32.

所以某处你有一行看起来像:

x = T.ivector()

x = T.vector(dtype='int32')

看来您需要将其更改为类似

的内容
x = T.tensor4()

其中 dtype 已更改为等于 theano.config.floatX,维度已更改为 4 以匹配 X 的 4 维性质。

如果你没弄明白,我也遇到了类似的错误,下面是我的修复方法: 将您的 y 转换为 int32。 x值可以是floatx,但是在nolearn中y必须是int32才能分类。