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

Set specific weights to non-trainable instead of the whole layer #47597

Closed
iliaschalkidis opened this issue Mar 5, 2021 · 12 comments
Closed
Assignees
Labels
comp:keras Keras related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.4 for issues related to TF 2.4 type:support Support issues

Comments

@iliaschalkidis
Copy link

I'm trying to find a way to turn specific weights (variables) from trainable to non-trainable. For example, let's say I load a pre-trained model that includes a Dense layer and want to keep the W matrix trainable while disabling the bias term.

I see only examples of how to turn a whole layer:

import tensorflow as tf

# Make a model with 2 layers
layer1 = tf.keras.layers.Dense(3, activation="relu")
layer2 = tf.keras.layers.Dense(3, activation="sigmoid")
model = tf.keras.Sequential([tf.keras.Input(shape=(3,)), layer1, layer2])

# Freeze the first layer
layer1.trainable = False

model.summary()

Returns:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 3)                 12        
_________________________________________________________________
dense_1 (Dense)              (None, 3)                 12        
=================================================================
Total params: 24
Trainable params: 12
Non-trainable params: 12
_________________________________________________________________

What if, I want to do something like:

import tensorflow as tf

# Make a model with 2 layers
layer1 = tf.keras.layers.Dense(3, activation="relu")
layer2 = tf.keras.layers.Dense(3, activation="sigmoid")
model = tf.keras.Sequential([tf.keras.Input(shape=(3,)), layer1, layer2])

# Freeze the first layer
layer1.bias.trainable = False

model.summary()

This returns the error:

layer1.bias.trainable = False.  AttributeError: can't set attribute
@amahendrakar
Copy link
Contributor

@iliaschalkidis,
Could you please take a look at this similar StackOverflow query and let us know if this is what you were looking for. Thanks!

@amahendrakar amahendrakar added comp:keras Keras related issues stat:awaiting response Status - Awaiting response from author type:support Support issues labels Mar 8, 2021
@iliaschalkidis
Copy link
Author

iliaschalkidis commented Mar 10, 2021

Hi @amahendrakar,

Unfortunately not. In this example, we just do not use a bias term at all. In my case, I just want to pick up a specific weight, for example, the bias term or the W matrix of a Dense layer, and turn it to no trainable, not ignore/delete it.

Thanks.

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label Mar 12, 2021
@amahendrakar
Copy link
Contributor

@ymodak,
I was able to reproduce the issue with TF v2.3, TF v2.4 and TF-nightly. Please find the gist of it here. Thanks!

@amahendrakar amahendrakar added the TF 2.4 for issues related to TF 2.4 label Mar 16, 2021
@amahendrakar amahendrakar assigned ymodak and unassigned amahendrakar Mar 16, 2021
@ymodak
Copy link
Contributor

ymodak commented Mar 16, 2021

Perhaps you can set arg use_bias=False in the layers. See gist.
Thanks!

@ymodak ymodak added the stat:awaiting response Status - Awaiting response from author label Mar 16, 2021
@iliaschalkidis
Copy link
Author

Hi @ymodak, @amahendrakar already proposed this solution and this is not the case. If we set use_bias=False, then the layer does not include a bias term. My questions is how to turn any variable, such as a bias term, to non trainable, not omit the parameter.

Thanks.

@tensorflowbutler tensorflowbutler removed the stat:awaiting response Status - Awaiting response from author label Mar 19, 2021
@ymodak ymodak added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Mar 22, 2021
@alieskici
Copy link

It's a vital function and it's interesting that it still doesn't.

@don-tpanic
Copy link

don-tpanic commented Oct 19, 2021

@ymodak
Any progress on this? thanks!

@jvishnuvardhan
Copy link
Contributor

jvishnuvardhan commented Nov 25, 2021

@iliaschalkidis This is more of a feature (and not a bug with TF/keras)

If you still interested in this feature, open the issue as a feature in keras-team/keras repo as Keras development moved there to focus entirely on Keras. Also, mention more details about your use case where you would like to have this functionality.

Keras development moved to another repository to focus on only keras. Could you please repost this issue on keras-team/keras repo.

@jvishnuvardhan jvishnuvardhan added stat:awaiting response Status - Awaiting response from author and removed stat:awaiting tensorflower Status - Awaiting response from tensorflower labels Nov 25, 2021
@google-ml-butler
Copy link

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Thank you.

@google-ml-butler google-ml-butler bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Dec 2, 2021
@google-ml-butler
Copy link

Closing as stale. Please reopen if you'd like to work on this further.

@google-ml-butler
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

@edmondja
Copy link

I almost had the same problem and I solved it this way :
conv1.non_trainable_weights.append(conv1.trainable_weights.pop(0)).

It was useful for me in order to prevent depthwise weights to train in a SeparableConv1D.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:keras Keras related issues stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author TF 2.4 for issues related to TF 2.4 type:support Support issues
Projects
None yet
Development

No branches or pull requests

9 participants