Keras 3 API 文档 / 层 API / 层权重约束

层权重约束

约束的使用

keras.constraints 模块中的类允许在训练期间对模型参数设置约束(例如非负性)。它们是每次梯度更新(使用 fit() 时)后应用于目标变量的逐变量投影函数。

确切的 API 将取决于具体的层,但 DenseConv1DConv2DConv3D 等层拥有统一的 API。

这些层提供了两个关键字参数

  • 用于主权重矩阵的 kernel_constraint
  • 用于偏置的 bias_constraint
from keras.constraints import max_norm
model.add(Dense(64, kernel_constraint=max_norm(2.)))

可用的权重约束

[源]

Constraint

keras.constraints.Constraint()

权重约束的基类。

Constraint 实例就像一个无状态函数。继承此类的用户应该覆盖 __call__() 方法,该方法接受单个权重参数,并返回该参数的投影版本(例如归一化或裁剪后的)。约束可以通过 kernel_constraintbias_constraint 参数与各种 Keras 层一起使用。

这里是一个非负权重约束的简单示例

>>> class NonNegative(keras.constraints.Constraint):
...
...  def __call__(self, w):
...    return w * ops.cast(ops.greater_equal(w, 0.), dtype=w.dtype)
>>> weight = ops.convert_to_tensor((-1.0, 1.0))
>>> NonNegative()(weight)
[0.,  1.]

在层中的使用

>>> keras.layers.Dense(4, kernel_constraint=NonNegative())

[源]

MaxNorm

keras.constraints.MaxNorm(max_value=2, axis=0)

MaxNorm 权重约束。

约束连接到每个隐藏单元的权重,使其范数小于或等于期望值。

也可通过快捷函数 keras.constraints.max_norm 获取。

参数

  • max_value: 连接权重的最大范数值。
  • axis: 整数,计算权重范数的轴。例如,在 Dense 层中,权重矩阵的形状为 (input_dim, output_dim),将 axis 设置为 0 以约束每个长度为 (input_dim,) 的权重向量。在 data_format="channels_last"Conv2D 层中,权重张量的形状为 (rows, cols, input_depth, output_depth),将 axis 设置为 [0, 1, 2] 以约束每个大小为 (rows, cols, input_depth) 的滤波器张量的权重。

[源]

MinMaxNorm

keras.constraints.MinMaxNorm(min_value=0.0, max_value=1.0, rate=1.0, axis=0)

MinMaxNorm 权重约束。

约束连接到每个隐藏单元的权重,使其范数位于下限和上限之间。

参数

  • min_value: 连接权重的最小范数。
  • max_value: 连接权重的最大范数。
  • rate: 强制约束的速率:权重将被重新缩放,以得到 (1 - rate) * norm + rate * norm.clip(min_value, max_value)。实际上,这意味着 rate=1.0 表示严格执行约束,而 rate<1.0 表示权重在每一步都会被重新缩放,以缓慢地移向期望区间内的值。
  • axis: 整数,计算权重范数的轴。例如,在 Dense 层中,权重矩阵的形状为 (input_dim, output_dim),将 axis 设置为 0 以约束每个长度为 (input_dim,) 的权重向量。在 data_format="channels_last"Conv2D 层中,权重张量的形状为 (rows, cols, input_depth, output_depth),将 axis 设置为 [0, 1, 2] 以约束每个大小为 (rows, cols, input_depth) 的滤波器张量的权重。

[源]

NonNeg

keras.constraints.NonNeg()

约束权重为非负值。


[源]

UnitNorm

keras.constraints.UnitNorm(axis=0)

约束连接到每个隐藏单元的权重具有单位范数。

参数

  • axis: 整数,计算权重范数的轴。例如,在 Dense 层中,权重矩阵的形状为 (input_dim, output_dim),将 axis 设置为 0 以约束每个长度为 (input_dim,) 的权重向量。在 data_format="channels_last"Conv2D 层中,权重张量的形状为 (rows, cols, input_depth, output_depth),将 axis 设置为 [0, 1, 2] 以约束每个大小为 (rows, cols, input_depth) 的滤波器张量的权重。

创建自定义权重约束

权重约束可以是任何可调用对象,它接受一个张量并返回形状和 dtype 相同的张量。通常,您会将约束实现为 keras.constraints.Constraint 的子类。

这里是一个简单的例子:一个约束,强制权重张量平均围绕某个特定值居中。

from keras import ops

class CenterAround(keras.constraints.Constraint):
  """Constrains weight tensors to be centered around `ref_value`."""

  def __init__(self, ref_value):
    self.ref_value = ref_value

  def __call__(self, w):
    mean = ops.mean(w)
    return w - mean + self.ref_value

  def get_config(self):
    return {'ref_value': self.ref_value}

或者,您也可以实现 get_config 方法和类方法 from_config,以便支持序列化——就像任何 Keras 对象一样。请注意,在上面的例子中,我们不需要实现 from_config,因为类的构造函数参数与 get_config 返回的配置中的键相同。在这种情况下,默认的 from_config 运行良好。