Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global Weight Decay in Keras

Is there a way to set a global weight decay in Keras?

I know about the layer wise one using regularizers(https://keras.io/regularizers/), but I could not find any information about a way to set a global weight decay.

like image 223
Thomas Pinetz Avatar asked Dec 21 '16 10:12

Thomas Pinetz


People also ask

How do you set weight decay in Keras?

To get global weight decay in keras regularizers have to be added to every layer in the model. In my models these layers are batch normalization (beta/gamma regularizer) and dense/convolutions (W_regularizer/b_regularizer) layers. Layer wise regularization is described here: (https://keras.io/regularizers/).

What is weight decay in CNN?

Weight Decay, or Regularization, is a regularization technique applied to the weights of a neural network. We minimize a loss function compromising both the primary loss function and a penalty on the Norm of the weights: L n e w ( w ) = L o r i g i n a l ( w ) + λ w T w.

What is L2 weight decay?

Weight decay is also known as L2 regularization, because it penalizes weights according to their L2 norm. In weight decay technique, the objective function of minimizing the prediction loss on the training data is replaced with the new objective function, minimizing the sum of the prediction loss and the penalty term.

What is a good weight decay?

In the tests we ran, the best learning rate with L2 regularization was 1e-6 (with a maximum learning rate of 1e-3) while 0.3 was the best value for weight decay (with a learning rate of 3e-3).


1 Answers

There is no way to directly apply a "global" weight decay to a whole keras model at once.

However, as I describe here, you can employ weight decay on a model by looping through its layers and manually applying the regularizers on appropriate layers. Here's the relevant code snippet:

model = keras.applications.ResNet50(include_top=True, weights='imagenet')
alpha = 0.00002  # weight decay coefficient

for layer in model.layers:
    if isinstance(layer, keras.layers.Conv2D) or isinstance(layer, keras.layers.Dense):
        layer.add_loss(lambda layer=layer: keras.regularizers.l2(alpha)(layer.kernel))
    if hasattr(layer, 'bias_regularizer') and layer.use_bias:
        layer.add_loss(lambda layer=layer: keras.regularizers.l2(alpha)(layer.bias))
like image 101
jake Avatar answered Sep 21 '22 14:09

jake