Is there any way to update only a subset of model parameters when using model.fit() procedure? For example, update only the weights corresponding to model.layers[1].W
I have tried a roundabout way: extract all the weights prior to model.fit(). Then set the weights of all other parameters back to the previous value. However, this is extremely slow. Would be great if there were alternate methods to perform this.
It will be useful in a number of contexts like complex network architectures with multiple input streams, multiple output streams, and multiple objectives. It's conceivable that you might want to use one objective to train only a particular part of the network, and hold weights in other parts of the network constant. A popular example would be many architectures like actor-critic in reinforcement learning.
Example: See the multi-input multi-output twitter example in this page http://keras.io/getting-started/functional-api-guide/ How can we train only the dense_1, dense_2 layers using error in main_output channel. Thanks!
This is a good question. I need too.
On Sun, Jun 26, 2016 at 5:07 PM Aravind Rajeswaran [email protected]
wrote:
Is there any way to update only a subset of model parameters when using
model.fit() procedure? For example, update only the weights corresponding
to model.layers[0].WI have tried a roundabout way: extract all the weights prior to
model.fit(). Then set the weights of all other parameters back to the
previous value. However, this is extremely slow. Would be great if there
were alternate methods to perform this.It will be useful in a number of contexts like complex network
architectures with multiple input streams, multiple output streams, and
multiple objectives. It's conceivable that you might want to use one
objective to train only a particular part of the network, and hold weights
in other parts of the network constant. A popular example would be many
architectures like actor-critic in reinforcement learning.—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/fchollet/keras/issues/3075, or mute the thread
https://github.com/notifications/unsubscribe/ACchnRj6wedD1Zwh7C8FP5qFXD66AlQsks5qPuoggaJpZM4I-pMf
.
Sungjin (James) Kim, PhD
Postdoc, CCB in Harvard
[Web] http://aspuru.chem.harvard.edu/james-sungjin-kim/
[Linkedin] https://www.linkedin.com/in/jamessungjinkim
[Facebook] https://www.facebook.com/jamessungjin.kim
[alternative email] jamessungjin.[email protected]
Have a look at the FAQ: How can I "freeze" Keras layers?, maybe that's already of help.
@ChristianThomae It's definitely related, but I have a question. Is the "freeze" permanent for a model, or can it be changed anytime?
For example, I might want to train only layer1 weights for a while, and then train layer3 weights for a while. So after sometime, can I make layer1 frozen and layer3 trainable? Thanks!
It can be changed dynamically, but you have to compile twice essentially. The example in the FAQ should be exactly what you're looking for -- two Models with shared weights, but different parameters for training. From the second half of the FAQ:
Additionally, you can set the trainable property of a layer to True or False after instantiation. For this to take effect, you will need to call compile() on your model after modifying the trainable property.
Really do check out the code example, I think it's exactly what you're looking for.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs, but feel free to re-open it if needed.
The links to "How to freeze a layer in Keras" are NOT related, as the question refers to a subset of weights WITHIN a layer.
This should be very easy to figure out but somehow it is not. Any progress on this?
You should use a custom layer that creates different weight tensors for the different parts of your weights that you do want or do not want to update. You can then pass different values for the trainable argument to the weight construction calls (add_weights).
@fchollet: What if I have multiple losses, say loss1 and loss2, and I want the weights from my customed layer is trainable for loss1 but not trainable for loss2?
@thanhnguyentang I am interested in this too. Did you find a solution?
U can switch trainable flag of layers in between epochs. Also writing custom loop for epochs using train_on_batch will give you finer control.
But when I add tensorflow Variables in between the same cannot be done. Can somebody help out?
Most helpful comment
The links to "How to freeze a layer in Keras" are NOT related, as the question refers to a subset of weights WITHIN a layer.
This should be very easy to figure out but somehow it is not. Any progress on this?