-
Notifications
You must be signed in to change notification settings - Fork 19.4k
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
How to change regularization parameters during training? #4813
Comments
Hi Alexander, The hyperparameters are built-in to the training function when you compile. Editing the model after compilation won't do anything to affect your current training. You will see the same issue if you try to change learning rates or other hyperparameters. The way to modify hyperparameters during training is to use backend variables in the training function and update those variables during training. The L1L2Regularizer isn't using variables but it should be. Instantiate but hold a reference to the regularizer.
During training, update the variable reg.l2 Might want to add a pull request to make l1l2 into variables. Cheers, |
Hi Ben! Thank you very much. It works :) (this is updated message, previously I reported it didn't, but I just drove weights to zero with big initial l2) I do it to implement MacKay's scheduler if you interested: https://www.youtube.com/watch?v=vEPQNwxd1Y4&index.. By the way, changing L1L2Regularizer we would need to change get_config method too
|
Great catch, Alexander. I always forget about get_config. Just put in a PR with a new flag "use_variables" (default=False). If True, it will use variables in L1L2Regularizer instead of constants. I left it as a flag just in case there is some barely-noticeable performance benefit to using a constant. |
hi, and then use it as an input to my custom loss function such as: how can I access this variable during training to change its value? |
Your custom loss function needs to work on You can get and change the value using
|
Hi, I've been tacking inspiration from this conversation and came up with this solution that works extremely well: 1st: extend the Regularizer with a custom l1l2 regularizer class (do not call it L1L2 as in serialization ,aka when you save and reload your model, shadowing does not work): it should go something like this:
2nd: Add your custom object so that when you might want to export your model you won't have any issue in reloading it.
3rd: update your variable using the custom object set_l1_l2 method by accessing the object from the model keras model.
Done. But wait, can't I access the variables from Yes you could but I warmly suggest you not to do so: Why? because at declaration time your variable scope will be depending on whether you've defined the L1L2_m within a layer (a convolutional layer for example Conv1). Using the object reference method |
I'd be highly interested in the MacKay's scheduler you mentioned. Did you manage to implement it? Best regards. |
Hi all,
I am trying to implement flexible regularization scheduler. Instantiate layer like this:
x = Convolution2D(... W_regularizer=l2(10)...)
and later change regularization:
model.layers[1].W_regularizer = l2(0)
I can verify the layer's settings changed:
but this has no effect during following training whether I compile model anew or not. Where is a caveat?
The text was updated successfully, but these errors were encountered: