How to save / serialize a trained model in anano?

I saved the model as described in loading and saving .

# saving trained model f = file('models/simple_model.save', 'wb') cPickle.dump(ca, f, protocol=cPickle.HIGHEST_PROTOCOL) f.close() 

ca is a trained auto encoder. This is an instance of the ca class. From the script in which I create and save the model, I can call the calls ca.get_reconstructed_input(...) and ca.get_hidden_values(...) without any problems.

In another script, I am trying to load a trained model.

 # loading the trained model model_file = file('models/simple_model.save', 'rb') ca = cPickle.load(model_file) model_file.close() 

I get the following error.

 ca = cPickle.load(model_file) 

AttributeError: the 'module' object does not have the 'cA' attribute

+7
python theano serialization save loading
source share
2 answers

All class definitions of pickled objects must be known by the script that performs the scattering. More about this in other StackOverflow questions (for example, the AttributeError: 'module' object does not have the 'newperson' attribute ).

Your code is correct if you import cA . Given the error you get, this may not be the case. Make sure you use from cA import cA , not just import cA

Alternatively, your model is defined by its parameters, so instead you can simply determine the parameter values). There are two ways to do this, depending on your point of view.

  • Save the general Anano variables. Here we assume that ca.params is a regular Python list of instances of the Theano shared variable.

     cPickle.dump(ca.params, f, protocol=cPickle.HIGHEST_PROTOCOL) 
  • Save the numpy arrays stored inside Theano shared variables.

     cPickle.dump([param.get_value() for param in ca.params], f, protocol=cPickle.HIGHEST_PROTOCOL) 

When you want to load the model, you will need to reinitialize the parameters. For example, create a new instance of class cA , then either

 ca.params = cPickle.load(f) ca.W, ca.b, ca.b_prime = ca.params 

or

 ca.params = [theano.shared(param) for param in cPickle.load(f)] ca.W, ca.b, ca.b_prime = ca.params 

Note that you need to set the params field and separate parameter fields.

+10
source share

One alternative way to save the model is to save its weight and architecture, and then download the same thing as we do to prepare for the CNN trip:

 def save_model(model): model_json = model.to_json() open('cifar10_architecture.json', 'w').write(model_json) model.save_weights('cifar10_weights.h5', overwrite=True) 

source / ref: https://blog.rescale.com/neural-networks-using-keras-on- rescale /

0
source share

All Articles