How to store and read nolearn.lasagne NeuralNet models using pickle

348 views Asked by At

How do I store the weights and biases in nolearn.lasagne NeuralNet model? From the documentation, I can't see how to access the NeuralNet's weights and biases and store them.

1

There are 1 answers

0
Shubham Bansal On

To save the entire nolearn model ( training history, parameters and architecture), you can do this :

import cPickle as pickle
sys.setrecursionlimit(10000) # you may need this if the network is large
with open("model_file", 'wb') as f:
     pickle.dump(nolearnnet , f, -1)

Please note that incase you train your model on GPU and pickle it using the above but want to unpickle it on CPU ( or vice versa) , this won't work. In that case you should just save the parameter values , which you can do like this:

weights = lasagne.layers.get_all_param_values(nolearnnet.get_all_layers()[-1])

And now you can save these weights . When you want to load them into another nolearn model, you can just do the following:

lasagne.layers.set_all_param_values(nolearnnet2.get_all_layers()[-1], weights)

It may help to refer to this discussion : https://groups.google.com/forum/#!topic/lasagne-users/BbG95R6SZ0I