【问题标题】:Save Theano model doens't work for MLP netTheano 模型不适用于 MLP 网络
【发布时间】: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


    【解决方案1】:

    我今天也遇到了同样的问题。我不知道为什么这适用于 'logistic_sgd.py' 而不是 'mlp.py' 但你可以做的就是存储 'classifier.params'

    'classifier.params' 是您完成培训后唯一需要的元素。从这个参数预测你的类应该不难(这已经在你的代码中了)。

    【讨论】:

    • 我试过了,但我认为我仍然遇到问题,如 EDIT 中所述
    • 我唯一的建议是使用 Lasagne python 库来处理 Theano,它允许通过简单地调用 'get_params()' 和 'set_params' 来保存和加载。
    【解决方案2】:

    我也遇到了这个问题并找到了这个解决方案。 即使只需要'classifier.params'y_predinput 也需要初始化。简单的方法是通过 pickle 存储它们并重新加载它们。

    保存:

    with open('best_model.pkl', 'wb') as f:
        cPickle.dump((classifier.params, classifier.logRegressionLayer.y_pred, 
                     classifier.input), f)
    

    预测函数:

    def predict(dataset, n_hidden, n_in, n_out):
        datasets = load_data(dataset)
        test_set_x, test_set_y = datasets[2]
        test_set_x = test_set_x.get_value()
        test_set_y = test_set_y.eval()
    
        rng = numpy.random.RandomState(1234)
        x = T.matrix('x')
    
        # Declare MLP classifier
        classifier = MLP(
            rng=rng,
            input=x,
            n_in=n_in,
            n_hidden=n_hidden,
            n_out=n_out
        )
    
        # load the saved model
        classifier.params, classifier.logRegressionLayer.y_pred,
            classifier.input = cPickle.load(open('best_model.pkl'))
    
        predict_model = theano.function(
            inputs=[classifier.input],*emphasized text*
            outputs=classifier.logRegressionLayer.y_pred)
    
        print("Expected values: ", test_set_y[:10])
        predicted_values = predict_model(test_set_x[:10])
        print("Predicted values:", predicted_values)
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-09-12
      • 2016-12-07
      • 2013-04-22
      • 1970-01-01
      • 2022-06-20
      • 2016-10-05
      • 2013-10-09
      • 2012-04-29
      • 1970-01-01
      相关资源
      最近更新 更多