Usage of regularizers

Regularizers allow to apply penalties on layer parameters or layer activity during optimization. These penalties are incorporated in the loss function that the network optimizes.

The penalties are applied on a per-layer basis. The exact API will depend on the layer, but the layers Dense, Conv1D, Conv2D and Conv3D have a unified API.

These layers expose 3 keyword arguments:

  • kernel_regularizer: instance of keras.regularizers.Regularizer
  • bias_regularizer: instance of keras.regularizers.Regularizer
  • activity_regularizer: instance of keras.regularizers.Regularizer

Example

  1. from keras import regularizers
  2. model.add(Dense(64, input_dim=64,
  3. kernel_regularizer=regularizers.l2(0.01),
  4. activity_regularizer=regularizers.l1(0.01)))

Available penalties

  1. keras.regularizers.l1(0.)
  2. keras.regularizers.l2(0.)
  3. keras.regularizers.l1_l2(l1=0.01, l2=0.01)

Developing new regularizers

Any function that takes in a weight matrix and returns a loss contribution tensor can be used as a regularizer, e.g.:

  1. from keras import backend as K
  2. def l1_reg(weight_matrix):
  3. return 0.01 * K.sum(K.abs(weight_matrix))
  4. model.add(Dense(64, input_dim=64,
  5. kernel_regularizer=l1_reg))

Alternatively, you can write your regularizers in an object-oriented way;see the keras/regularizers.py module for examples.