The error:
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,).')
Please advise how to fix?
The code and data can be downloaded on this link: http://u.163.com/axfWJ81e and enter this code: QU90WxTZ
And here is my code:
# -*- 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))
The problem is that you tell Theano Y is a matrix of floating point values but the value you provide for Y is a vector of integers.
It's not entirely clear which is correct, but I suspect you intend Y to be a vector of integers and to use the 1-hot variant of cross entropy. If so, the problem might be fixed by changing the Theano definition of Y to
Y = T.lvector()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With