@@ -24,9 +24,72 @@ def pairwise_loss(self, pairwise_logits: types.Tensor) -> types.Tensor:
2424 suitable for gradient-based optimization.
2525"""
2626extra_args = ""
27+ example = """
28+ 1. With `compile()` API:
29+
30+ ```python
31+ model.compile(
32+ loss=keras_rs.losses.PairwiseSoftZeroOneLoss(),
33+ ...
34+ )
35+ ```
36+
37+ 2. As a standalone function:
38+ 2.1. Unbatched inputs
39+ >>> y_true = np.array([1.0, 0.0, 1.0, 3.0, 2.0])
40+ >>> y_pred = np.array([1.0, 3.0, 2.0, 4.0, 0.8])
41+ >>> pairwise_soft_zero_one_loss = keras_rs.losses.PairwiseSoftZeroOneLoss()
42+ >>> pairwise_soft_zero_one_loss(y_true=y_true, y_pred=y_pred)
43+ 0.86103
44+
45+ 2.2 Batched inputs
46+ 2.2.1 Using default 'auto'/'sum_over_batch_size' reduction.
47+ >>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
48+ >>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
49+ >>> pairwise_soft_zero_one_loss = keras_rs.losses.PairwiseSoftZeroOneLoss()
50+ >>> pairwise_soft_zero_one_loss(y_true=y_true, y_pred=y_pred)
51+ 0.46202
52+
53+ 2.2.2. With masked inputs (useful for ragged inputs)
54+ >>> y_true = {
55+ ... "labels": np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]]),
56+ ... "mask": np.array(
57+ ... [[True, True, True, True], [True, True, False, False]]
58+ ... ),
59+ ... }
60+ >>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
61+ >>> pairwise_soft_zero_one_loss(y_true=y_true, y_pred=y_pred)
62+ 0.29468
63+
64+ 2.2.3 With `sample_weight`
65+ >>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
66+ >>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
67+ >>> sample_weight = np.array(
68+ ... [[2.0, 3.0, 1.0, 1.0], [2.0, 1.0, 0.0, 0.0]]
69+ ... )
70+ >>> pairwise_soft_zero_one_loss = keras_rs.losses.PairwiseSoftZeroOneLoss()
71+ >>> pairwise_soft_zero_one_loss(
72+ ... y_true=y_true, y_pred=y_pred, sample_weight=sample_weight
73+ ... )
74+ 0.40478
75+
76+ 2.2.4 Using `'none'` reduction.
77+ >>> y_true = np.array([[1.0, 0.0, 1.0, 3.0], [0.0, 1.0, 2.0, 3.0]])
78+ >>> y_pred = np.array([[1.0, 3.0, 2.0, 4.0], [1.0, 1.8, 2.0, 3.0]])
79+ >>> pairwise_soft_zero_one_loss = keras_rs.losses.PairwiseSoftZeroOneLoss(
80+ ... reduction="none"
81+ ... )
82+ >>> pairwise_soft_zero_one_loss(y_true=y_true, y_pred=y_pred)
83+ [
84+ [0.8807971 , 0., 0.73105854, 0.43557024],
85+ [0., 0.31002545, 0.7191075 , 0.61961967]
86+ ]
87+ """
88+
2789PairwiseSoftZeroOneLoss .__doc__ = pairwise_loss_subclass_doc_string .format (
2890 loss_name = "soft zero-one loss" ,
2991 formula = formula ,
3092 explanation = explanation ,
3193 extra_args = extra_args ,
94+ example = example ,
3295)
0 commit comments