首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >西亚诺的多层CNN,Lenet5

西亚诺的多层CNN,Lenet5
EN

Stack Overflow用户
提问于 2017-02-09 16:55:49
回答 1查看 146关注 0票数 0

我是深度学习的新手,我遇到了一个问题。我正在使用Theano进行图像识别,我想使用训练好的模型创建一个预测系统。我参考了LeNet5 Convolutional Neural Networks (LeNet)并训练了自己的数据,现在我想使用训练好的模型来预测新图像。在Classifying MNIST digits using Logistic Regression中,它描述了筛选训练模型的方法,但它只是一个逻辑回归,而不是一个多层的CNN。以同样的方式我保存了每一层,但我不能用它来预测。请帮帮我!下面是我的代码:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2017-02-10 08:40:45

保存模型的方法有很多。我经常使用的方法是对每一层的权重和偏置进行腌制(顺序由你决定):

代码语言:javascript
复制
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()

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

代码语言:javascript
复制
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()
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42132086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档