我想要一個Theano Logistic迴歸模型的一個非常基本的例子,並且在訓練網絡之後,我想測試一些圖像以查看它們如何分類。培訓和測試代碼可在http://deeplearning.net/tutorial/code/logistic_sgd.py找到。事實上,我試圖修改的唯一部分是預測()函數如下:餵食圖像到Theano引發ShapeMismatch錯誤
def predict():
"""
An example of how to load a trained model and use it
to predict labels.
"""
# load the saved model
classifier = cPickle.load(open('best_model.pkl'))
# compile a predictor function
predict_model = theano.function(
inputs=[classifier.input],
outputs=classifier.y_pred)
# We can test it on some examples from test test
#dataset='mnist.pkl.gz'
#datasets = load_data(dataset)
#test_set_x, test_set_y = datasets[2]
#test_set_x = test_set_x.get_value()
img = Image.open('index.png','r')
shared_x = theano.shared(numpy.asarray(img,dtype=theano.config.floatX))
predicted_values = predict_model(shared_x.get_value())
print ("Predicted values for the first 10 examples in test set:")
print predicted_values
我跟着這裏找到http://blog.eduardovalle.com/2015/08/25/input-images-theano/一些提示,但顯然我有,因爲我所得到的問題是:
ValueError: Shape mismatch: x has 28 cols (and 28 rows) but y has 784 rows (and 10 cols) Apply node that caused the error: Dot22(x, W) Inputs types: [TensorType(float64, matrix), TensorType(float64, matrix)] Inputs shapes: [(28, 28), (784, 10)] Inputs strides: [(224, 8), (80, 8)] Inputs values: ['not shown', 'not shown']
那麼將圖像(以我的情況爲28x28)提供給Theano進行預測的正確方法是什麼?