Skip to content

Commit e62add5

Browse files
authored
Beautifier losses doc (#2062)
* Beautifier losses doc
1 parent c083f28 commit e62add5

File tree

9 files changed

+79
-71
lines changed

9 files changed

+79
-71
lines changed

tensorflow_addons/losses/contrastive.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def contrastive_loss(
3333
by the margin constant for the samples of different labels.
3434
3535
The euclidean distances `y_pred` between two embedding matrices
36-
`a` and `b` with shape [batch_size, hidden_size] can be computed
36+
`a` and `b` with shape `[batch_size, hidden_size]` can be computed
3737
as follows:
3838
3939
```python
@@ -44,14 +44,14 @@ def contrastive_loss(
4444
See: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
4545
4646
Args:
47-
y_true: 1-D integer `Tensor` with shape [batch_size] of
47+
y_true: 1-D integer `Tensor` with shape `[batch_size]` of
4848
binary labels indicating positive vs negative pair.
49-
y_pred: 1-D float `Tensor` with shape [batch_size] of
49+
y_pred: 1-D float `Tensor` with shape `[batch_size]` of
5050
distances between two embedding matrices.
5151
margin: margin term in the loss definition.
5252
5353
Returns:
54-
contrastive_loss: 1-D float `Tensor` with shape [batch_size].
54+
contrastive_loss: 1-D float `Tensor` with shape `[batch_size]`.
5555
"""
5656
y_pred = tf.convert_to_tensor(y_pred)
5757
y_true = tf.dtypes.cast(y_true, y_pred.dtype)
@@ -71,12 +71,12 @@ class ContrastiveLoss(LossFunctionWrapper):
7171
See: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf
7272
7373
We expect labels `y_true` to be provided as 1-D integer `Tensor`
74-
with shape [batch_size] of binary integer labels. And `y_pred` must be
75-
1-D float `Tensor` with shape [batch_size] of distances between two
74+
with shape `[batch_size]` of binary integer labels. And `y_pred` must be
75+
1-D float `Tensor` with shape `[batch_size]` of distances between two
7676
embedding matrices.
7777
7878
The euclidean distances `y_pred` between two embedding matrices
79-
`a` and `b` with shape [batch_size, hidden_size] can be computed
79+
`a` and `b` with shape `[batch_size, hidden_size]` can be computed
8080
as follows:
8181
8282
```python

tensorflow_addons/losses/focal_loss.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,25 @@ class SigmoidFocalCrossEntropy(LossFunctionWrapper):
4646
1.9097870e-04,
4747
2.0559824e-05]
4848
```
49-
Usage with tf.keras API:
49+
50+
Usage with `tf.keras` API:
5051
5152
```python
5253
model = tf.keras.Model(inputs, outputs)
5354
model.compile('sgd', loss=tfa.losses.SigmoidFocalCrossEntropy())
5455
```
5556
56-
Args
57-
alpha: balancing factor, default value is 0.25
58-
gamma: modulating factor, default value is 2.0
57+
Args:
58+
alpha: balancing factor, default value is 0.25.
59+
gamma: modulating factor, default value is 2.0.
5960
6061
Returns:
6162
Weighted loss float `Tensor`. If `reduction` is `NONE`, this has the same
6263
shape as `y_true`; otherwise, it is scalar.
6364
6465
Raises:
6566
ValueError: If the shape of `sample_weight` is invalid or value of
66-
`gamma` is less than zero
67+
`gamma` is less than zero.
6768
"""
6869

6970
@typechecked
@@ -105,7 +106,7 @@ def sigmoid_focal_crossentropy(
105106
best use-cases of focal loss is its usage in object detection where the
106107
imbalance between the background class and other classes is extremely high.
107108
108-
Args
109+
Args:
109110
y_true: true targets tensor.
110111
y_pred: predictions tensor.
111112
alpha: balancing factor.

tensorflow_addons/losses/giou_loss.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ class GIoULoss(LossFunctionWrapper):
4040
loss = gl(boxes1, boxes2)
4141
print('Loss: ', loss.numpy()) # Loss: [1.07500000298023224, 1.9333333373069763]
4242
```
43-
Usage with tf.keras API:
43+
44+
Usage with `tf.keras` API:
4445
4546
```python
4647
model = tf.keras.Model(inputs, outputs)

tensorflow_addons/losses/kappa_loss.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@
2525

2626
@tf.keras.utils.register_keras_serializable(package="Addons")
2727
class WeightedKappaLoss(tf.keras.losses.Loss):
28-
"""Implements the Weighted Kappa loss function.
28+
r"""Implements the Weighted Kappa loss function.
2929
3030
Weighted Kappa loss was introduced in the
3131
[Weighted kappa loss function for multi-class classification
3232
of ordinal data in deep learning]
3333
(https://www.sciencedirect.com/science/article/abs/pii/S0167865517301666).
3434
Weighted Kappa is widely used in Ordinal Classification Problems.
35-
The loss value lies in [-inf, log 2], where log 2
36-
means the random prediction.
35+
The loss value lies in $ [-\infty, \log 2] $, where $ \log 2 $
36+
means the random prediction.
3737
3838
Usage:
3939
@@ -67,18 +67,17 @@ def __init__(
6767
dtype: Optional[tf.DType] = tf.float32,
6868
reduction: str = tf.keras.losses.Reduction.NONE,
6969
):
70-
"""Creates a `WeightedKappa` instance.
70+
r"""Creates a `WeightedKappaLoss` instance.
7171
7272
Args:
7373
num_classes: Number of unique classes in your dataset.
7474
weightage: (Optional) Weighting to be considered for calculating
7575
kappa statistics. A valid value is one of
76-
['linear', 'quadratic']. Defaults to `quadratic` since it's
77-
mostly used.
76+
['linear', 'quadratic']. Defaults to 'quadratic'.
7877
name: (Optional) String name of the metric instance.
7978
epsilon: (Optional) increment to avoid log zero,
80-
so the loss will be log(1 - k + epsilon), where k belongs to
81-
[-1, 1], usually you can use the default value which is 1e-6.
79+
so the loss will be $ \log(1 - k + \epsilon) $, where $ k $ lies
80+
in $ [-1, 1] $. Defaults to 1e-6.
8281
dtype: (Optional) Data type of the metric result.
8382
Defaults to `tf.float32`.
8483
Raises:

tensorflow_addons/losses/lifted.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def lifted_struct_loss(
3131
"""Computes the lifted structured loss.
3232
3333
Args:
34-
labels: 1-D tf.int32 `Tensor` with shape [batch_size] of
34+
labels: 1-D tf.int32 `Tensor` with shape `[batch_size]` of
3535
multiclass integer labels.
3636
embeddings: 2-D float `Tensor` of embedding vectors. Embeddings should
3737
not be l2 normalized.

tensorflow_addons/losses/metric_learning.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ def pairwise_distance(feature: TensorLike, squared: bool = False):
2525
output[i, j] = || feature[i, :] - feature[j, :] ||_2
2626
2727
Args:
28-
feature: 2-D Tensor of size [number of data, feature dimension].
28+
feature: 2-D Tensor of size `[number of data, feature dimension]`.
2929
squared: Boolean, whether or not to square the pairwise distances.
3030
3131
Returns:
32-
pairwise_distances: 2-D Tensor of size [number of data, number of data].
32+
pairwise_distances: 2-D Tensor of size `[number of data, number of data]`.
3333
"""
3434
pairwise_distances_squared = tf.math.add(
3535
tf.math.reduce_sum(tf.math.square(feature), axis=[1], keepdims=True),
@@ -74,10 +74,10 @@ def angular_distance(feature: TensorLike):
7474
output[i, j] = 1 - cosine_similarity(feature[i, :], feature[j, :])
7575
7676
Args:
77-
feature: 2-D Tensor of size [number of data, feature dimension].
77+
feature: 2-D Tensor of size `[number of data, feature dimension]`.
7878
7979
Returns:
80-
angular_distances: 2-D Tensor of size [number of data, number of data].
80+
angular_distances: 2-D Tensor of size `[number of data, number of data]`.
8181
"""
8282
# normalize input
8383
feature = tf.math.l2_normalize(feature, axis=1)

tensorflow_addons/losses/quantiles.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class PinballLoss(LossFunctionWrapper):
9494
print('Loss: ', loss.numpy()) # Loss: 0.475
9595
```
9696
97-
Usage with the `compile` API:
97+
Usage with the `tf.keras` API:
9898
9999
```python
100100
model = tf.keras.Model(inputs, outputs)

tensorflow_addons/losses/sparsemax_loss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def sparsemax_loss(
3333
Computes the generalized multi-label classification loss for the sparsemax
3434
function. The implementation is a reformulation of the original loss
3535
function such that it uses the sparsemax properbility output instead of the
36-
internal \tau variable. However, the output is identical to the original
36+
internal $ \tau $ variable. However, the output is identical to the original
3737
loss function.
3838
3939
[1]: https://arxiv.org/abs/1602.02068

tensorflow_addons/losses/triplet.py

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ def _masked_maximum(data, mask, dim=1):
2626
"""Computes the axis wise maximum over chosen elements.
2727
2828
Args:
29-
data: 2-D float `Tensor` of size [n, m].
30-
mask: 2-D Boolean `Tensor` of size [n, m].
29+
data: 2-D float `Tensor` of shape `[n, m]`.
30+
mask: 2-D Boolean `Tensor` of shape `[n, m]`.
3131
dim: The dimension over which to compute the maximum.
3232
3333
Returns:
@@ -48,8 +48,8 @@ def _masked_minimum(data, mask, dim=1):
4848
"""Computes the axis wise minimum over chosen elements.
4949
5050
Args:
51-
data: 2-D float `Tensor` of size [n, m].
52-
mask: 2-D Boolean `Tensor` of size [n, m].
51+
data: 2-D float `Tensor` of shape `[n, m]`.
52+
mask: 2-D Boolean `Tensor` of shape `[n, m]`.
5353
dim: The dimension over which to compute the minimum.
5454
5555
Returns:
@@ -74,33 +74,36 @@ def triplet_semihard_loss(
7474
margin: FloatTensorLike = 1.0,
7575
distance_metric: Union[str, Callable] = "L2",
7676
) -> tf.Tensor:
77-
"""Computes the triplet loss with semi-hard negative mining.
77+
r"""Computes the triplet loss with semi-hard negative mining.
78+
79+
Usage:
80+
81+
>>> y_true = tf.convert_to_tensor([0, 0])
82+
>>> y_pred = tf.convert_to_tensor([[0.0, 1.0], [1.0, 0.0]])
83+
>>> tfa.losses.triplet_semihard_loss(y_true, y_pred, distance_metric="L2")
84+
<tf.Tensor: shape=(), dtype=float32, numpy=2.4142137>
85+
86+
>>> # Calling with callable `distance_metric`
87+
>>> distance_metric = lambda x: tf.linalg.matmul(x, x, transpose_b=True)
88+
>>> tfa.losses.triplet_semihard_loss(y_true, y_pred, distance_metric=distance_metric)
89+
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>
7890
7991
Args:
80-
y_true: 1-D integer `Tensor` with shape [batch_size] of
92+
y_true: 1-D integer `Tensor` with shape `[batch_size]` of
8193
multiclass integer labels.
8294
y_pred: 2-D float `Tensor` of embedding vectors. Embeddings should
8395
be l2 normalized.
8496
margin: Float, margin term in the loss definition.
85-
distance_metric: str or function, determines distance metric:
86-
"L2" for l2-norm distance
87-
"squared-L2" for squared l2-norm distance
88-
"angular" for cosine similarity
89-
A custom function returning a 2d adjacency
90-
matrix of a chosen distance metric can
91-
also be passed here. e.g.
92-
93-
def custom_distance(batch):
94-
batch = 1 - batch @ batch.T
95-
return batch
96-
97-
triplet_semihard_loss(batch, labels,
98-
distance_metric=custom_distance
99-
)
97+
distance_metric: `str` or a `Callable` that determines distance metric.
98+
Valid strings are "L2" for l2-norm distance,
99+
"squared-L2" for squared l2-norm distance,
100+
and "angular" for cosine similarity.
100101
102+
A `Callable` should take a batch of embeddings as input and
103+
return the pairwise distance matrix.
101104
102105
Returns:
103-
triplet_loss: float scalar with dtype of y_pred.
106+
triplet_loss: float scalar with dtype of `y_pred`.
104107
"""
105108

106109
labels, embeddings = y_true, y_pred
@@ -207,33 +210,37 @@ def triplet_hard_loss(
207210
soft: bool = False,
208211
distance_metric: Union[str, Callable] = "L2",
209212
) -> tf.Tensor:
210-
"""Computes the triplet loss with hard negative and hard positive mining.
213+
r"""Computes the triplet loss with hard negative and hard positive mining.
214+
215+
Usage:
216+
217+
>>> y_true = tf.convert_to_tensor([0, 0])
218+
>>> y_pred = tf.convert_to_tensor([[0.0, 1.0], [1.0, 0.0]])
219+
>>> tfa.losses.triplet_hard_loss(y_true, y_pred, distance_metric="L2")
220+
<tf.Tensor: shape=(), dtype=float32, numpy=1.0>
221+
222+
>>> # Calling with callable `distance_metric`
223+
>>> distance_metric = lambda x: tf.linalg.matmul(x, x, transpose_b=True)
224+
>>> tfa.losses.triplet_hard_loss(y_true, y_pred, distance_metric=distance_metric)
225+
<tf.Tensor: shape=(), dtype=float32, numpy=0.0>
211226
212227
Args:
213-
y_true: 1-D integer `Tensor` with shape [batch_size] of
228+
y_true: 1-D integer `Tensor` with shape `[batch_size]` of
214229
multiclass integer labels.
215230
y_pred: 2-D float `Tensor` of embedding vectors. Embeddings should
216231
be l2 normalized.
217232
margin: Float, margin term in the loss definition.
218233
soft: Boolean, if set, use the soft margin version.
219-
distance_metric: str or function, determines distance metric:
220-
"L2" for l2-norm distance
221-
"squared-L2" for squared l2-norm distance
222-
"angular" for cosine similarity
223-
A custom function returning a 2d adjacency
224-
matrix of a chosen distance metric can
225-
also be passed here. e.g.
226-
227-
def custom_distance(batch):
228-
batch = 1 - batch @ batch.T
229-
return batch
230-
231-
triplet_semihard_loss(batch, labels,
232-
distance_metric=custom_distance
233-
)
234+
distance_metric: `str` or a `Callable` that determines distance metric.
235+
Valid strings are "L2" for l2-norm distance,
236+
"squared-L2" for squared l2-norm distance,
237+
and "angular" for cosine similarity.
238+
239+
A `Callable` should take a batch of embeddings as input and
240+
return the pairwise distance matrix.
234241
235242
Returns:
236-
triplet_loss: float scalar with dtype of y_pred.
243+
triplet_loss: float scalar with dtype of `y_pred`.
237244
"""
238245
labels, embeddings = y_true, y_pred
239246

@@ -311,7 +318,7 @@ class TripletSemiHardLoss(LossFunctionWrapper):
311318
See: https://arxiv.org/abs/1503.03832.
312319
313320
We expect labels `y_true` to be provided as 1-D integer `Tensor` with shape
314-
[batch_size] of multi-class integer labels. And embeddings `y_pred` must be
321+
`[batch_size]` of multi-class integer labels. And embeddings `y_pred` must be
315322
2-D float `Tensor` of l2 normalized embedding vectors.
316323
317324
Args:
@@ -348,7 +355,7 @@ class TripletHardLoss(LossFunctionWrapper):
348355
See: https://arxiv.org/pdf/1703.07737.
349356
350357
We expect labels `y_true` to be provided as 1-D integer `Tensor` with shape
351-
[batch_size] of multi-class integer labels. And embeddings `y_pred` must be
358+
`[batch_size]` of multi-class integer labels. And embeddings `y_pred` must be
352359
2-D float `Tensor` of l2 normalized embedding vectors.
353360
354361
Args:

0 commit comments

Comments
 (0)