Usage of regularizers

正则化器允许在优化过程中对层参数或层活动施加惩罚. 这些惩罚并入网络优化的损失函数中.

处罚是按层进行的. 确切的API将取决于该层,但是DenseConv1DConv2DConv3D具有统一的API.

这些层公开了3个关键字参数:

  • kernel_regularizer :实例keras.regularizers.Regularizer
  • bias_regularizer :实例keras.regularizers.Regularizer
  • activity_regularizerkeras.regularizers.Regularizer实例

Example

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

Available penalties

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

Developing new regularizers

接受权重矩阵并返回损耗贡献张量的任何函数都可以用作正则化器,例如:

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,
                kernel_regularizer=l1_reg))

另外,您可以以面向对象的方式编写正则化器. 有关示例,请参见keras / regularizers.py模块.