错误:
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,).’)
请告知如何解决?
代码和数据可以在此链接上下载:
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))