【发布时间】:2016-06-02 08:10:37
【问题描述】:
我正在尝试使用 http://deeplearning.net/tutorial/code/logistic_sgd.py 的逻辑回归器中显示的代码,特别是使用 http://deeplearning.net/tutorial/code/mlp.py 中显示的代码来保存在使用 Theano 构建的多层感知器网络训练期间获得的模型
# save the best model
with open('best_model.pkl', 'w') as f:
cPickle.dump(classifier, f)
但我得到的是
... 加载数据 ... 构建模型 ... 训练 epoch 1, 小批量 74/74,验证错误 38.333333 % epoch 1,minibatch 74/74,最佳模型的测试错误 41.666667 % Traceback(最近一次调用最后):文件“mlp.py”,第 423 行,在 test_mlp() 文件“mlp.py”,第 406 行,在 test_mlp cPickle.dump(classifier, f, protocol=cPickle.HIGHEST_PROTOCOL) cPickle.PicklingError: Can't pickle : 属性 查找 内置.instancemethod 失败
由于我在卷积网络中也遇到了这个问题,我的问题是:有一种通用的方法可以将模型存储在 Theano 中,以便重复用于预测?
编辑 正如我现在使用的 cmets 中所建议的那样
cPickle.dump((classifier.hiddenLayer.params,classifier.logRegressionLayer.params), f)
用于保存和
classifier.hiddenLayer.W = cPickle.load(open('best_model_mlp.pkl'))[0][0]
用于在定义为的分类器中设置hiddenLayer的权重(例如)
x = T.matrix('x')
classifier = MLP(
rng=rng,
input=x,
n_in = 28*28,
n_hidden= 500,
n_out=10
)
但是当我调用这个函数时
predict_model = theano.function(
inputs=[classifier.input],
outputs=classifier.logRegressionLayer.y_pred,
)
我总是将 [0] 作为预测,即使使用训练有素的网络也是如此。 我在设置或保存参数时仍然做错了吗?
【问题讨论】:
标签: pickle theano prediction