Save and reset parameters of multilayer networks in theano
<p>We can save and load object in python using <code>six.moves.cPickle</code>. </p>

<p>I saved and reset the parameters for LeNet using the following code.</p>

<pre><code># save model
# params = layer3.params + layer2.params + layer1.params + layer0.params
import six.moves.cPickle as pickle
f = file('best_cnnmodel.save', 'wb')
pickle.dump(params, f, protocol=pickle.HIGHEST_PROTOCOL)
f.close()

# reset parameters
model_file = file('best_cnnmodel.save', 'rb')
params = pickle.load(model_file)
model_file.close()
layer3.W.set_value(params[0].get_value())
layer3.b.set_value(params[1].get_value())
layer2.W.set_value(params[2].get_value())
layer2.b.set_value(params[3].get_value())
layer1.W.set_value(params[4].get_value())
layer1.b.set_value(params[5].get_value())
layer0.W.set_value(params[6].get_value())
layer0.b.set_value(params[7].get_value())
</code></pre>

<p>The code seems to be ok for LeNet. But it is not elegant. For deep networks, I can not save models using this code. What can I do in this case?</p>

<p>You can consider to use json format. It is human readable and easy to work with.</p>

<p>Here is an example:</p>

<h1>Prepare the data</h1>

<pre><code>import json


data = {
    'L1' : { 'W': layer1.W, 'b': layer1.b },
    'L2' : { 'W': layer2.W, 'b': layer2.b },
    'L3' : { 'W': layer3.W, 'b': layer3.b },
}
json_data = json.dumps(data)
</code></pre>

<p>The <code>json_data</code> looks like this: </p>

<pre><code>{"L2": {"b": 2, "W": 17}, "L3": {"b": 2, "W": 10}, "L1": {"b": 2, "W": 1}}
</code></pre>

<h1>Unpack the data</h1>

<pre><code>params = json.loads(json_data)

for k, v in params.items():
    level = int(k[1:])
    # assume you save the layer in an array, but you can use 
    # different way to store and reference the layers
    layer = layers[level]
    layer.W = v['W']
    layer.b = v['b']
</code></pre>

python save load theano conv-neural-network