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


from keras import regularizers
model.add(Dense(64, input_dim=64,

Available penalties


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.:

from keras import backend as K

def l1_reg(weight_matrix):
    return 0.01 * K.sum(K.abs(weight_matrix))

model.add(Dense(64, input_dim=64,

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