python - Save and reset parameters of multilayer networks in theano -


we can save , load object in python using six.moves.cpickle.

i saved , reset parameters lenet using following code.

# save model # params = layer3.params + layer2.params + layer1.params + layer0.params import six.moves.cpickle 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 ok lenet. not elegant. deep networks, can not save models using code. can in case?

you can consider use json format. human readable , easy work with.

here example:

prepare 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 this:

{"l2": {"b": 2, "w": 17}, "l3": {"b": 2, "w": 10}, "l1": {"b": 2, "w": 1}} 

unpack data

params = json.loads(json_data)  k, v in params.items():     level = int(k[1:])     # assume save layer in array, can use      # different way store , reference layers     layer = layers[level]     layer.w = v['w']     layer.b = v['b'] 

Comments