类型错误 "Bad input argument to theano function"

TypeError "Bad input argument to theano function"

错误:

TypeError: ('Bad input argument to theano function with name "c2.py:77" at index 1(0-based)', 'Wrong number of dimensions: expected 2, got 1 with shape (128L,).')

请指教如何解决?

代码和数据可以在这个link上下载: http://u.163.com/axfWJ81e 并输入此代码:QU90WxTZ

这是我的代码:

# -*- coding: utf-8 -*-
import os
import pandas as pd
import theano
from theano import tensor as T
import numpy as np

def normalizeX(X):
    return X / 255.0
data = pd.read_csv("digits3a.csv")
trX = normalizeX(data.values[:, 1:].astype(float))
trY = data.values[:, 0]
data = pd.read_csv("digits3b.csv")
teX = normalizeX(data.values.astype(float))

def floatX(X):
    return np.asarray(X, dtype=theano.config.floatX)

def init_weights(shape):
    return theano.shared(floatX(np.random.randn(*shape) * 0.01))

def model(X, w):
    return T.nnet.softmax(T.dot(X, w))

X = T.fmatrix()
Y = T.fmatrix()
w = init_weights((784, 10))
py_x = model(X, w)
y_pred = T.argmax(py_x, axis=1)
cost = T.mean(T.nnet.categorical_crossentropy(py_x, Y))
gradient = T.grad(cost=cost, wrt=w)
update = [[w, w - gradient * 0.05]]
train = theano.function(inputs=[X, Y], outputs=cost, updates=update, allow_input_downcast=True)
predict = theano.function(inputs=[X], outputs=y_pred, allow_input_downcast=True)

for i in range(10):
    for start, end in zip(range(0, len(trX), 128), range(128, len(trX), 128)):
        cost = train(trX[start:end], trY[start:end])
    print i, np.mean(np.argmax(teY, axis=1) == predict(teX))

问题是你告诉 Theano Y 是一个浮点值矩阵,但你为 Y 提供的值是一个整数向量。

尚不完全清楚哪个是正确的,但我怀疑您打算将 Y 设为整数向量并使用交叉熵的 1-hot 变体。如果是这样,可以通过将 Y 的 Theano 定义更改为

来解决问题
Y = T.lvector()