KerasRS / API文档 / 损失函数 / PairwiseHingeLoss

PairwiseHingeLoss

[源]

PairwiseHingeLoss

keras_rs.losses.PairwiseHingeLoss(temperature: float = 1.0, **kwargs: Any)

计算真实标签与预测分数之间的成对 Hinge 损失。此损失函数专为排序任务设计,其目标是正确排列每个列表中的项目。它通过比较每个列表中的项目对来计算损失,对真实标签较高的项目预测分数却低于真实标签较低的项目的场景进行惩罚。

对于 y_pred 中的每个预测分数列表 sy_true 中相应的真实标签列表 y,损失计算如下:

loss = sum_{i} sum_{j} I(y_i > y_j) * max(0, 1 - (s_i - s_j))

其中

  • y_iy_j 分别是项目 ij 的真实标签。
  • s_is_j 分别是项目 ij 的预测分数。
  • I(y_i > y_j) 是一个指示函数,当 y_i > y_j 时等于 1,否则等于 0。
  • max(0, 1 - (s_i - s_j)) 是 Hinge 损失,它对当 y_i > y_j 时分数差 s_i - s_j 不够大的情况进行惩罚。

参数

  • reduction:应用于损失的归约类型。在几乎所有情况下,应为 "sum_over_batch_size"。支持的选项有 "sum""sum_over_batch_size""mean""mean_with_sample_weight"None"sum" 对损失求和,"sum_over_batch_size""mean" 对损失求和并除以样本大小,"mean_with_sample_weight" 对损失求和并除以样本权重的总和。"none"None 不执行任何聚合。默认为 "sum_over_batch_size"
  • name:损失实例的可选名称。
  • dtype:损失计算的数据类型。默认为 None,表示使用 keras.backend.floatx()。除非设置为其他值(通过 keras.backend.set_floatx()),否则 keras.backend.floatx()"float32"。如果提供了 keras.DTypePolicy,则将使用 compute_dtype

示例

使用 compile() API

model.compile(
    loss=keras_rs.losses.PairwiseHingeLoss(),
    ...
)

作为具有非批处理输入的独立函数

>>> y_true = np.array([1.0, 0.0, 1.0, 3.0, 2.0])
>>> y_pred = np.array([1.0, 3.0, 2.0, 4.0, 0.8])
>>> pairwise_hinge_loss = keras_rs.losses.PairwiseHingeLoss()
>>> pairwise_hinge_loss(y_true=y_true, y_pred=y_pred)
2.32000

使用默认的 'auto'/'sum_over_batch_size' 归约的批处理输入

>>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
>>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
>>> pairwise_hinge_loss = keras_rs.losses.PairwiseHingeLoss()
>>> pairwise_hinge_loss(y_true=y_true, y_pred=y_pred)
0.75

带有掩码输入(对不规则输入有用)

>>> y_true = {
...     "labels": np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]]),
...     "mask": np.array(
...         [[True, True, True, True], [True, True, False, False]]
...     ),
... }
>>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
>>> pairwise_hinge_loss(y_true=y_true, y_pred=y_pred)
0.64999

使用 sample_weight

>>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
>>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
>>> sample_weight = np.array(
...     [[2.0, 3.0, 1.0, 1.0], [2.0, 1.0, 0.0, 0.0]]
... )
>>> pairwise_hinge_loss = keras_rs.losses.PairwiseHingeLoss()
>>> pairwise_hinge_loss(
...     y_true=y_true, y_pred=y_pred, sample_weight=sample_weight
... )
1.02499

使用 'none' 归约

>>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
>>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
>>> pairwise_hinge_loss = keras_rs.losses.PairwiseHingeLoss(
...     reduction="none"
... )
>>> pairwise_hinge_loss(y_true=y_true, y_pred=y_pred)
[[3. , 0. , 2. , 0.], [0., 0.20000005, 0.79999995, 0.]]

[源]

call 方法

PairwiseHingeLoss.call(y_true: Any, y_pred: Any)

计算成对损失。

参数

  • y_true:张量或字典。真实值。如果为张量,对于非批处理输入,形状为 (list_size);对于批处理输入,形状为 (batch_size, list_size)。如果项目的标签为 -1,则在损失计算中将被忽略。如果为字典,应包含两个键:"labels""mask""mask" 可用于忽略损失计算中的元素,即不会与这些项目形成对。请注意,最终的掩码是传递的掩码与 labels >= 0and 运算结果。
  • y_pred:张量。预测值,对于非批处理输入,形状为 (list_size);对于批处理输入,形状为 (batch_size, list_size)。应与 y_true 的形状相同。

返回值

损失。