Save and reset parameters of multilayer networks in theano
Question: We can save and load object in python using `six.moves.cPickle`.

I saved and reset the parameters for LeNet using the following 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())
    

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?


Answer: You can consider to use json format. It is human readable and easy to work
with.

Here is an example:

# Prepare the data

    
    
    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)
    

The `json_data` looks like this:

    
    
    {"L2": {"b": 2, "W": 17}, "L3": {"b": 2, "W": 10}, "L1": {"b": 2, "W": 1}}
    

# Unpack the data

    
    
    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']
    

