nolearn icon indicating copy to clipboard operation
nolearn copied to clipboard

remove('trainable') Lasagne's command doesn't work in nolearn

Open algila opened this issue 8 years ago • 6 comments

I would ask to keep working the Lasagne's command remove('trainable') needed after loading pretrained models to train those again in a modified model. I realised that this is not working due to the error message received and no mention into documentation. Appear to me that the function 'fit' doesn't know how to manage it, but please sorry if I'm wrong

algila avatar Nov 03 '16 07:11 algila

Could you post a minimal code example that reproduces the error? From your description, it is not quite clear to me what you did. Thank you.

BenjaminBossan avatar Nov 05 '16 14:11 BenjaminBossan

I have a CNN perfectly working and trained. At the end of the training I save the parameters into a file named "file_modelCNN". Next step I want to upload the parameters from that file to train again a CNN having the same convolutional layers of the previous but different number of dense layers. This in order to keep freeze the weitghs of the convolutional layers. Lasagne provide the comment remove('trainable') that is working in a Lasagne NN but appear to me that it is not working in a NN created with nolearn NeuralNet. Here after the code:

with open('./file_modelCNN' , 'rb') as f: modelCNN = pickle.load(f) cnn['conv1'].params[cnn['conv1'].W].remove("trainable") cnn.load_params_from(modelCNN) #load from a file the weight and bias on cnn architecture cnn.initialize()

and the error message cnn['conv1'].params[cnn['conv1'].W].remove("trainable") TypeError: 'NeuralNet' object has no attribute 'getitem'

To support the understanding I put hereafter the same code written for a Lasagne NN that appear to work.

`# Carica i pesi del modello dal file model = pickle.load(open('./vgg16.pkl')) weights = model['param values'] # list of network weight tensors classes = model['synset words']

for key,val in network.iteritems(): if (key is not 'fc6') and (key is not 'fc7') and (key is not 'fc8'): if not ( ('dropout' in key) or ('pool' in key) or ('prob' in key) or ('input' in key)): network[key].params[network[key].W].remove("trainable") network[key].params[network[key].b].remove("trainable")`

many thanks

algila avatar Nov 05 '16 15:11 algila

The error is that you try to access the network's layers by using brackets, which does not work: cnn['conv1']. Instead, the layers are saved in the layers_ attribute. This could work: cnn.layers_['dense1'].params[cnn.layers_['dense1'].W].remove("trainable").

BenjaminBossan avatar Nov 05 '16 15:11 BenjaminBossan

Sorry it still doesn't work, I report a bigger portion of the code

cnn = NeuralNet(
        layers=[
        ('input', layers.InputLayer),
        ('conv1', layers.Conv2DLayer),      
        ('pool1', layers.MaxPool2DLayer),  
        ('conv2', layers.Conv2DLayer),
        ('pool2', layers.MaxPool2DLayer),
        ('conv3', layers.Conv2DLayer),
        ('dropout1', layers.DropoutLayer),
        ('dense', layers.DenseLayer),
        ('dropout2', layers.DropoutLayer),
        #('dense2', layers.DenseLayer),
        #('dropout3', layers.DropoutLayer),
        ('output', layers.DenseLayer),
        ],
#removed code because not needed for the issue
    )

#cnn.fit(train,target) # train the net1 model for n epochs
#cnn.save_params_to('file_modelCNN') #save to a file

with open('./file_modelCNN' , 'rb') as f:
     modelCNN = pickle.load(f)
cnn.layers_['conv1'].params[cnn.layers_['conv1'].W].remove("trainable")`

and also the error message

cnn.layers_['conv1'].params[cnn.layers_['conv1'].W].remove("trainable")
AttributeError: 'NeuralNet' object has no attribute 'layers_

algila avatar Nov 06 '16 00:11 algila

initialize the net first: cnn.initialize().

BenjaminBossan avatar Nov 07 '16 21:11 BenjaminBossan

This solve the issue, many thanks

algila avatar Nov 15 '16 06:11 algila