Deep learning 在Theano腌制多层CNN,Lenet5

Deep learning 在Theano腌制多层CNN,Lenet5,deep-learning,theano,conv-neural-network,image-recognition,Deep Learning,Theano,Conv Neural Network,Image Recognition,我刚开始深入学习,我遇到了一个问题。 我正在使用Theano进行图像识别,我想用经过训练的模型创建一个预测系统。 我参考了LeNet5并训练了我自己的数据,现在我想使用训练过的模型来预测新的图像。 书中描述了pickle训练模型的方法,但它只是一个逻辑回归,而不是多层CNN。以同样的方式我保存了每一层,但我不能用它来预测。 请帮帮我! 这是我的密码: def predict(): """ An example of how to load a trained model and use it t

我刚开始深入学习,我遇到了一个问题。 我正在使用Theano进行图像识别,我想用经过训练的模型创建一个预测系统。 我参考了LeNet5并训练了我自己的数据,现在我想使用训练过的模型来预测新的图像。 书中描述了pickle训练模型的方法,但它只是一个逻辑回归,而不是多层CNN。以同样的方式我保存了每一层,但我不能用它来预测。 请帮帮我! 这是我的密码:

def predict():
"""
An example of how to load a trained model and use it
to predict labels.
"""

# load the saved model
#x = Data
x = T.matrix('x')
Data = x.reshape((1, 1, 32, 32))
layer0
layer1
layer2_input = layer1.output.flatten(2)
layer2
layer3

# compile a predictor function
predict_model = theano.function([layer0.input],
    layer0.output)
    #inputs=[layer0.input],
    #outputs=layer3.y_pred)

# We can test it on some examples from test test
#dataset='facedata_cross_6_2_6.pkl.gz'
#datasets = load_data(dataset)
#test_set_x, test_set_y = datasets[2]
#test_set_x = test_set_x.get_value()
#reshape=np.reshape(test_set_x[26],(28,28))
#plt.imshow(reshape)

predicted_values = predict_model(Data)
print("Predicted values for the first 10 examples in test set:")
print(predicted_values)

保存模型的方法有很多种。我经常使用的方法是通过酸洗每层的重量和偏差(顺序由您决定):

然后,对于预测系统,创建相同的模型体系结构,并使用保存的模型作为初始值(与保存的顺序相同):


保存模型的方法有很多种。我经常使用的方法是通过酸洗每层的重量和偏差(顺序由您决定):

然后,对于预测系统,创建相同的模型体系结构,并使用保存的模型作为初始值(与保存的顺序相同):


成功了!伙计,你太棒了!非常感谢。我使用的代码类似于,#layer0=pickle.load(open('best_model_layer0.pkl'))#layer1=pickle.load(open('best_model_layer1.pkl'))#layer2=pickle.load(open('best_model_layer2.pkl')))#layer3=pickle.load(open('best#model_layer3=layer3.pkl')),但每次它都预测为[0]。你的代码解决了所有问题!好吧。。很高兴它能起作用:)如果您认为这是一个可接受的答案,您可以单击我答案中的“可接受符号”(“v”复选符号),通知未来的读者我想这样做,但我的声誉还不够:(哇,说真的吗?我认为接受答案不需要最低的声誉,但这没问题!伙计,你太棒了!谢谢!我使用的代码是,#layer0=pickle.load(open('best#model_ulayer0.pkl'))#layer1=pickle.load(open('best#model_layer1.pkl'))#layer2=pickle.load(open('best#model#layer2.pkl'))#=pickle.load(open)('best_model_layer3.pkl'))但每次它都预测为[0]。您的代码解决了所有问题!好的..很高兴它能工作:)如果您认为这是一个可接受的答案,您可以在我的答案中单击可接受的符号('v'check symbol'),通知未来的读者我想知道,但我的声誉还不够:(哇,说真的?我认为接受答案不需要最低的声誉,但没关系
f = file('Models/bestmodel.pickle','wb')
cPickle.dump(layer0.W.get_value(borrow=True),f,protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(layer1.W.get_value(borrow=True),f,protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(layer2.W.get_value(borrow=True),f,protocol=cPickle.HIGHEST_PROTOCOL)
...
cPickle.dump(layer0.b.get_value(borrow=True),f,protocol=cPickle.HIGHEST_PROTOCOL)            
cPickle.dump(layer1.b.get_value(borrow=True),f,protocol=cPickle.HIGHEST_PROTOCOL)
cPickle.dump(layer2.b.get_value(borrow=True),f,protocol=cPickle.HIGHEST_PROTOCOL)
...
f.close()
f=file('Models/bestmodel.pickle','rb')
layer0.W.set_value(cPickle.load(f), borrow=True)
layer1.W.set_value(cPickle.load(f), borrow=True)
layer2.W.set_value(cPickle.load(f), borrow=True)
...
layer0.b.set_value(cPickle.load(f), borrow=True)
layer1.b.set_value(cPickle.load(f), borrow=True)
layer2.b.set_value(cPickle.load(f), borrow=True)
...
f.close()