Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
algila opened this issue Nov 3, 2016 · 6 comments
Open

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

algila opened this issue Nov 3, 2016 · 6 comments

Comments

@algila
Copy link

algila commented Nov 3, 2016

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

@BenjaminBossan
Copy link
Collaborator

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.

@algila
Copy link
Author

algila commented Nov 5, 2016

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

@BenjaminBossan
Copy link
Collaborator

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").

@algila
Copy link
Author

algila commented Nov 6, 2016

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_

@BenjaminBossan
Copy link
Collaborator

initialize the net first: cnn.initialize().

@algila
Copy link
Author

algila commented Nov 15, 2016

This solve the issue, many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants