Error saving parameters for CNN

129 views
Skip to first unread message

Joe Blair

unread,
May 20, 2016, 9:14:44 AM5/20/16
to lasagne-users
Hi all, 

Im trying to save the parameters for my network so I can reload them and use for further training/ classification. 

as suggested by MNSIT i am doing:

with np.load('model.npz') as f:

     param_values = [f['arr_%d' % i] for i in range(len(f.files))]

lasagne.layers.set_all_param_values(output, param_values)


Where output is:


output = lasagne.layers.DenseLayer(l_dropout, num_units=3, nonlinearity = lasagne.nonlinearities.softmax)


I manage to save the parameters into model.npz, however upon loading, i  seem to get the error:


ValueError: mismatch: got 1 values to set 14 parameters


Perhaps of note is that 14 is the number of layers I have. does this mean I need to save and load the parameters for each layer? Or i am doing something wrong?


Best, 


Joe




Jan Schlüter

unread,
May 20, 2016, 12:39:23 PM5/20/16
to lasagne-users
The idea of that example code is to create a .npz file with entries arr_0, arr_1, and so on, storing all the parameters of your network. If you get an error on loading that it only loaded one value, probably you're doing something wrong when saving the model. Can you load your model.npz in a Python terminal (f = np.load('model.npz')) and see what arrays are in there (print f.files)? Are there 14 arrays or just one? If it's only one, what's its shape (print f['arr_0'].shape)?
My crystal ball tells me you forgot the asterisk in the example code for saving: np.savez('model.npz', *lasagne.layers.get_all_param_values(network))
Without the asterisk it will convert the list of parameters into a single numpy array of dtype object, with the asterisk it will unwrap the list into separate arguments so they're saved as separate arrays.

Best, Jan

Joe Blair

unread,
May 20, 2016, 1:26:58 PM5/20/16
to lasagne-users
With an asterisk it seems to work just fine. I am able to JUST save the output layer, and reload the whole network which is what I had hoped to do.

Thanks!

bell...@stethome.com

unread,
Jul 2, 2018, 8:33:34 AM7/2/18
to lasagne-users
If you have forgotten this asterisk and re-save the model take too long, you can still fix it, unwrapping the outputted list by np.load:

def load_model():

with np.load("model.npz") as f:
param_values = [f['arr_%d' % i] for i in range(len(f.files))]
    return param_values[0]


On Friday, 20 May 2016 18:39:23 UTC+2, Jan Schlüter wrote:
Reply all
Reply to author
Forward
0 new messages