PairwiseSoftZeroOneLoss
类keras_rs.losses.PairwiseSoftZeroOneLoss(temperature: float = 1.0, **kwargs: Any)
计算真实标签与预测分数之间的成对软零一损失。此损失函数专为排序任务设计,其目标是正确排序每个列表中的项目。它通过比较每个列表中的项目对来计算损失,对真实标签较高的项目预测分数却低于真实标签较低的项目的情况进行惩罚。
对于 y_pred
中的每个预测分数列表 s
以及 y_true
中相应的真实标签列表 y
,损失按如下方式计算
loss = sum_{i} sum_{j} I(y_i > y_j) * (1 - sigmoid(s_i - s_j))
其中
y_i
和 y_j
分别是项目 i
和 j
的真实标签。s_i
和 s_j
分别是项目 i
和 j
的预测分数。I(y_i > y_j)
是一个指示函数,如果 y_i > y_j
则等于 1,否则等于 0。(1 - sigmoid(s_i - s_j))
表示软零一损失,它用一个平滑、可微的函数近似理想的零一损失(如果 s_i < s_j
则为 1,否则为 0)。这使得它适合基于梯度的优化。参数
"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"
。None
,这意味着使用 keras.backend.floatx()
。除非设置为不同值(通过 keras.backend.set_floatx()
),否则 keras.backend.floatx()
为 "float32"
。如果提供了 keras.DTypePolicy
,则将使用 compute_dtype
。示例
使用 compile()
API
model.compile(
loss=keras_rs.losses.PairwiseSoftZeroOneLoss(),
...
)
作为独立函数处理未批处理输入
>>> 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_soft_zero_one_loss = keras_rs.losses.PairwiseSoftZeroOneLoss()
>>> pairwise_soft_zero_one_loss(y_true=y_true, y_pred=y_pred)
0.86103
使用默认 '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_soft_zero_one_loss = keras_rs.losses.PairwiseSoftZeroOneLoss()
>>> pairwise_soft_zero_one_loss(y_true=y_true, y_pred=y_pred)
0.46202
处理带掩码的输入(对不规则输入很有用)
>>> 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_soft_zero_one_loss(y_true=y_true, y_pred=y_pred)
0.29468
使用 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_soft_zero_one_loss = keras_rs.losses.PairwiseSoftZeroOneLoss()
>>> pairwise_soft_zero_one_loss(
... y_true=y_true, y_pred=y_pred, sample_weight=sample_weight
... )
0.40478
使用 '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_soft_zero_one_loss = keras_rs.losses.PairwiseSoftZeroOneLoss(
... reduction="none"
... )
>>> pairwise_soft_zero_one_loss(y_true=y_true, y_pred=y_pred)
[
[0.8807971 , 0., 0.73105854, 0.43557024],
[0., 0.31002545, 0.7191075 , 0.61961967]
]
call
方法PairwiseSoftZeroOneLoss.call(y_true: Any, y_pred: Any)
计算成对损失。
参数
(list_size)
,对于批处理输入形状为 (batch_size, list_size)
。如果项目的标签为 -1,则在损失计算中会被忽略。如果它是字典,应包含两个键:"labels"
和 "mask"
。"mask"
可用于在损失计算中忽略元素,即不会与这些项目形成对。请注意,最终掩码是传入的掩码与 labels >= 0
的逻辑 and
。(list_size)
,对于批处理输入形状为 (batch_size, list_size)
。应与 y_true
形状相同。返回
损失值。