Save Theano model not working for MLP network

I am trying to save the model obtained while training the Perceptron multilayer network built using Theano at http://deeplearning.net/tutorial/code/mlp.py using the code shown in the Logistic regression at http://deeplearning.net/tutorial /code/logistic_sgd.py in particular

# save the best model
with open('best_model.pkl', 'w') as f:
cPickle.dump(classifier, f)

but i get

... loading data ... building a model ... school age 1, minibus 74/74, verification error 38.333333% epoch 1, minibatch 74/74, test error on the best model 41.666667% Traceback (last last call): File "mlp .py ", line 423, in test_mlp () File" mlp.py ", line 406, in test_mlp cPickle.dump (classifier, f, protocol = cPickle.HIGHEST_PROTOCOL) cPickle.PicklingError: Unable to pickle: the search attribute is built-in . Failed method

Since I also encountered this problem with the convolution network, my question is: is there a general way to store the model in Theano so that it can be reused for forecasting?

EDIT As suggested in the comments I'm using now

cPickle.dump((classifier.hiddenLayer.params,classifier.logRegressionLayer.params), f)

to save and

classifier.hiddenLayer.W = cPickle.load(open('best_model_mlp.pkl'))[0][0]

() ,

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] , . ?

+4
2

. , 'logistic_sgd.py ', 'mlp.py', 'classifier.params'

'classifier.params' - , , . ( ).

+3

. 'classifier.params', y_pred input. - .

:

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)

, .

0

Source: https://habr.com/ru/post/1618448/


All Articles