【发布时间】:2016-02-22 01:37:37
【问题描述】:
我正在尝试一个非常基本的 Theano Logistic 回归模型示例,在训练网络之后,我想测试一些图像以了解它们是如何分类的。训练和测试的代码可以在http://deeplearning.net/tutorial/code/logistic_sgd.py找到。事实上,我尝试修改的唯一部分是 predict() 函数,如下所示:
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:形状不匹配:x 有 28 列(和 28 行)但 y 有 784 行(和 10 列)应用导致错误的节点:Dot22(x, W) 输入类型:[TensorType(float64, matrix), TensorType(float64, matrix)] 输入形状:[(28, 28), (784, 10)] 输入步幅:[(224, 8), (80, 8)] 输入值:['未显示', '未显示']
那么,将图像(在我的情况下为 28x28)提供给 Theano 进行预测的正确方法是什么?
【问题讨论】: