From efb8b10982c05f270c7c7623a79ab269fe00f960 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Mon, 31 Aug 2020 00:02:50 +0530 Subject: [PATCH 01/20] updated contrastive.py --- tensorflow_addons/losses/contrastive.py | 35 ++++++++++++++++++------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/tensorflow_addons/losses/contrastive.py b/tensorflow_addons/losses/contrastive.py index c6a9e9f826..28f394dfb5 100644 --- a/tensorflow_addons/losses/contrastive.py +++ b/tensorflow_addons/losses/contrastive.py @@ -15,10 +15,9 @@ """Implements contrastive loss.""" import tensorflow as tf - +from typeguard import typechecked from tensorflow_addons.utils.keras_utils import LossFunctionWrapper from tensorflow_addons.utils.types import TensorLike, Number -from typeguard import typechecked @tf.keras.utils.register_keras_serializable(package="Addons") @@ -36,10 +35,18 @@ def contrastive_loss( `a` and `b` with shape `[batch_size, hidden_size]` can be computed as follows: - ```python - # y_pred = \sqrt (\sum_i (a[:, i] - b[:, i])^2) - y_pred = tf.linalg.norm(a - b, axis=1) - ``` + >>> a = tf.constant([[1, 2], + ... [3, 4], + ... [5, 6]], dtype=tf.float16) + >>> b = tf.constant([[5, 9], + ... [3, 6], + ... [1, 8]], dtype=tf.float16) + >>> y_pred = tf.linalg.norm(a - b, axis=1) + + + See: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf @@ -79,10 +86,18 @@ class ContrastiveLoss(LossFunctionWrapper): `a` and `b` with shape `[batch_size, hidden_size]` can be computed as follows: - ```python - # y_pred = \sqrt (\sum_i (a[:, i] - b[:, i])^2) - y_pred = tf.linalg.norm(a - b, axis=1) - ``` + >>> a = tf.constant([[1, 2], + ... [3, 4], + ... [5, 6]], dtype=tf.float16) + >>> b = tf.constant([[5, 9], + ... [3, 6], + ... [1, 8]], dtype=tf.float16) + >>> y_pred = tf.linalg.norm(a - b, axis=1) + + + Args: margin: `Float`, margin term in the loss definition. From ed45e85ce3636464bc7a3d9c8473602d8bb3467f Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Mon, 31 Aug 2020 00:03:11 +0530 Subject: [PATCH 02/20] updated focal_loss.py --- tensorflow_addons/losses/focal_loss.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/tensorflow_addons/losses/focal_loss.py b/tensorflow_addons/losses/focal_loss.py index 8f29951277..bf98643dff 100644 --- a/tensorflow_addons/losses/focal_loss.py +++ b/tensorflow_addons/losses/focal_loss.py @@ -16,10 +16,9 @@ import tensorflow as tf import tensorflow.keras.backend as K - +from typeguard import typechecked from tensorflow_addons.utils.keras_utils import LossFunctionWrapper from tensorflow_addons.utils.types import FloatTensorLike, TensorLike -from typeguard import typechecked @tf.keras.utils.register_keras_serializable(package="Addons") @@ -37,22 +36,17 @@ class SigmoidFocalCrossEntropy(LossFunctionWrapper): Usage: - ```python - fl = tfa.losses.SigmoidFocalCrossEntropy() - loss = fl( - y_true = [[1.0], [1.0], [0.0]], - y_pred = [[0.97], [0.91], [0.03]]) - print('Loss: ', loss.numpy()) # Loss: [6.8532745e-06, - 1.9097870e-04, - 2.0559824e-05] - ``` + >>> fl = tfa.losses.SigmoidFocalCrossEntropy() + >>> loss = fl( + ... y_true = [[1.0], [1.0], [0.0]], + ... y_pred = [[0.97], [0.91], [0.03]]) + >>> print('Loss: ', loss.numpy()) + Usage with `tf.keras` API: - ```python - model = tf.keras.Model(inputs, outputs) - model.compile('sgd', loss=tfa.losses.SigmoidFocalCrossEntropy()) - ``` + >>> model = tf.keras.Model() + >>> model.compile('sgd', loss=tfa.losses.SigmoidFocalCrossEntropy()) Args: alpha: balancing factor, default value is 0.25. From a898b40eb9297c71ee84b2eb4b8b31daf213de35 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Mon, 31 Aug 2020 00:03:27 +0530 Subject: [PATCH 03/20] updated giou_loss.py --- tensorflow_addons/losses/giou_loss.py | 28 +++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/losses/giou_loss.py b/tensorflow_addons/losses/giou_loss.py index e789fcacb1..dc9ee5ec60 100644 --- a/tensorflow_addons/losses/giou_loss.py +++ b/tensorflow_addons/losses/giou_loss.py @@ -14,11 +14,11 @@ # ============================================================================== """Implements GIoU loss.""" +from typing import Optional import tensorflow as tf +from typeguard import typechecked from tensorflow_addons.utils.keras_utils import LossFunctionWrapper from tensorflow_addons.utils.types import TensorLike -from typing import Optional -from typeguard import typechecked @tf.keras.utils.register_keras_serializable(package="Addons") @@ -33,20 +33,18 @@ class GIoULoss(LossFunctionWrapper): Usage: - ```python - gl = tfa.losses.GIoULoss() - boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) - boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) - loss = gl(boxes1, boxes2) - print('Loss: ', loss.numpy()) # Loss: [1.07500000298023224, 1.9333333373069763] + >>> gl = tfa.losses.GIoULoss() + >>> boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) + >>> boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) + >>> loss = gl(boxes1, boxes2) + >>> print('Loss: ', loss.numpy()) + ``` Usage with `tf.keras` API: - ```python - model = tf.keras.Model(inputs, outputs) - model.compile('sgd', loss=tfa.losses.GIoULoss()) - ``` + >>> model = tf.keras.Model() + >>> model.compile('sgd', loss=tfa.losses.GIoULoss()) Args: mode: one of ['giou', 'iou'], decided to calculate GIoU or IoU loss. @@ -63,7 +61,8 @@ def __init__( @tf.keras.utils.register_keras_serializable(package="Addons") -def giou_loss(y_true: TensorLike, y_pred: TensorLike, mode: str = "giou") -> tf.Tensor: +def giou_loss(y_true: TensorLike, y_pred: TensorLike, + mode: str = "giou") -> tf.Tensor: """Implements the GIoU loss function. GIoU loss was first introduced in the @@ -93,7 +92,8 @@ def giou_loss(y_true: TensorLike, y_pred: TensorLike, mode: str = "giou") -> tf. return 1 - giou -def _calculate_giou(b1: TensorLike, b2: TensorLike, mode: str = "giou") -> tf.Tensor: +def _calculate_giou(b1: TensorLike, b2: TensorLike, + mode: str = "giou") -> tf.Tensor: """ Args: b1: bounding box. The coordinates of the each bounding box in boxes are From 1e66c19785751dde027b9dfeb50c772aeb52590a Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Mon, 31 Aug 2020 00:03:41 +0530 Subject: [PATCH 04/20] updated kappa_loss.py --- tensorflow_addons/losses/kappa_loss.py | 37 +++++++++++++------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/tensorflow_addons/losses/kappa_loss.py b/tensorflow_addons/losses/kappa_loss.py index c041cb8b01..7982f7bd62 100644 --- a/tensorflow_addons/losses/kappa_loss.py +++ b/tensorflow_addons/losses/kappa_loss.py @@ -37,24 +37,23 @@ class WeightedKappaLoss(tf.keras.losses.Loss): Usage: - ```python - kappa_loss = WeightedKappaLoss(num_classes=4) - y_true = tf.constant([[0, 0, 1, 0], [0, 1, 0, 0], - [1, 0, 0, 0], [0, 0, 0, 1]]) - y_pred = tf.constant([[0.1, 0.2, 0.6, 0.1], [0.1, 0.5, 0.3, 0.1], - [0.8, 0.05, 0.05, 0.1], [0.01, 0.09, 0.1, 0.8]]) - loss = kappa_loss(y_true, y_pred) - print('Loss: ', loss.numpy()) # Loss: -1.1611923 - ``` + >>> kappa_loss = tfa.losses.WeightedKappaLoss(num_classes=4) + >>> y_true = tf.constant([[0, 0, 1, 0], [0, 1, 0, 0], + ... [1, 0, 0, 0], [0, 0, 0, 1]]) + >>> y_pred = tf.constant([[0.1, 0.2, 0.6, 0.1], [0.1, 0.5, 0.3, 0.1], + ... [0.8, 0.05, 0.05, 0.1], [0.01, 0.09, 0.1, 0.8]]) + >>> loss = kappa_loss(y_true, y_pred) + >>> print('Loss: ', loss.numpy()) + Usage with `tf.keras` API: - ```python - # outputs should be softmax results - # if you want to weight the samples, just multiply the outputs - # by the sample weight. - model = tf.keras.Model(inputs, outputs) - model.compile('sgd', loss=tfa.losses.WeightedKappa(num_classes=4)) - ``` + + >>> model = tf.keras.Model() + >>> model.compile('sgd', loss=tfa.losses.WeightedKappa(num_classes=4)) + < outputs should be softmax results + if you want to weight the samples, just multiply the outputs + by the sample weight.> + """ @typechecked @@ -111,8 +110,8 @@ def __init__( self.weight_mat = (col_mat - row_mat) ** 2 def call(self, y_true, y_pred): - y_true = tf.cast(y_true, dtype=self.col_label_vec.dtype) - y_pred = tf.cast(y_pred, dtype=self.weight_mat.dtype) + y_true = tf.cast(y_true, self.col_label_vec.dtype) + y_pred = tf.cast(y_pred, self.weight_mat.dtype) batch_size = tf.shape(y_true)[0] cat_labels = tf.matmul(y_true, self.col_label_vec) cat_label_mat = tf.tile(cat_labels, [1, self.num_classes]) @@ -126,7 +125,7 @@ def call(self, y_true, y_pred): pred_dist = tf.reduce_sum(y_pred, axis=0, keepdims=True) w_pred_dist = tf.matmul(self.weight_mat, pred_dist, transpose_b=True) denominator = tf.reduce_sum(tf.matmul(label_dist, w_pred_dist)) - denominator /= tf.cast(batch_size, dtype=denominator.dtype) + denominator /= tf.cast(batch_size, denominator.dtype) loss = tf.math.divide_no_nan(numerator, denominator) return tf.math.log(loss + self.epsilon) From ed2292109fad7a5098cad036dc4c72c74bad27c3 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Mon, 31 Aug 2020 00:04:06 +0530 Subject: [PATCH 05/20] updated npairs.py --- tensorflow_addons/losses/npairs.py | 72 +++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/tensorflow_addons/losses/npairs.py b/tensorflow_addons/losses/npairs.py index 1693123505..094e4b0ff1 100644 --- a/tensorflow_addons/losses/npairs.py +++ b/tensorflow_addons/losses/npairs.py @@ -16,8 +16,8 @@ import tensorflow as tf -from tensorflow_addons.utils.types import TensorLike from typeguard import typechecked +from tensorflow_addons.utils.types import TensorLike @tf.keras.utils.register_keras_serializable(package="Addons") @@ -33,10 +33,21 @@ def npairs_loss(y_true: TensorLike, y_pred: TensorLike) -> tf.Tensor: The similarity matrix `y_pred` between two embedding matrices `a` and `b` with shape `[batch_size, hidden_size]` can be computed as follows: - ```python - # y_pred = a * b^T - y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) - ``` + + >>> a = tf.constant([[1, 2], + ... [3, 4], + ... [5, 6]], dtype=tf.float16) + >>> b = tf.constant([[5, 9], + ... [3, 6], + ... [1, 8]], dtype=tf.float16) + >>> y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) + + See: http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf @@ -89,10 +100,19 @@ def npairs_multilabel_loss(y_true: TensorLike, y_pred: TensorLike) -> tf.Tensor: The similarity matrix `y_pred` between two embedding matrices `a` and `b` with shape `[batch_size, hidden_size]` can be computed as follows: - ```python - # y_pred = a * b^T - y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) - ``` + >>> a = tf.constant([[1, 2], + ... [3, 4], + ... [5, 6]], dtype=tf.float16) + >>> b = tf.constant([[5, 9], + ... [3, 6], + ... [1, 8]], dtype=tf.float16) + >>> y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) + + See: http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf @@ -139,10 +159,19 @@ class NpairsLoss(tf.keras.losses.Loss): The similarity matrix `y_pred` between two embedding matrices `a` and `b` with shape `[batch_size, hidden_size]` can be computed as follows: - ```python - # y_pred = a * b^T - y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) - ``` + >>> a = tf.constant([[1, 2], + ... [3, 4], + ... [5, 6]], dtype=tf.float16) + >>> b = tf.constant([[5, 9], + ... [3, 6], + ... [1, 8]], dtype=tf.float16) + >>> y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) + + See: http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf @@ -184,10 +213,19 @@ class NpairsMultilabelLoss(tf.keras.losses.Loss): The similarity matrix `y_pred` between two embedding matrices `a` and `b` with shape `[batch_size, hidden_size]` can be computed as follows: - ```python - # y_pred = a * b^T - y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) - ``` + >>> a = tf.constant([[1, 2], + ... [3, 4], + ... [5, 6]], dtype=tf.float16) + >>> b = tf.constant([[5, 9], + ... [3, 6], + ... [1, 8]], dtype=tf.float16) + >>> y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) + + See: http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf From 3e34a9bdfe92ff05fd8d9dfc4656f4550fe146cc Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Mon, 31 Aug 2020 00:04:22 +0530 Subject: [PATCH 06/20] updated quantiles.py --- tensorflow_addons/losses/quantiles.py | 31 +++++++++++---------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/tensorflow_addons/losses/quantiles.py b/tensorflow_addons/losses/quantiles.py index 2aad531294..037e37c2d9 100644 --- a/tensorflow_addons/losses/quantiles.py +++ b/tensorflow_addons/losses/quantiles.py @@ -35,14 +35,11 @@ def pinball_loss( See: https://en.wikipedia.org/wiki/Quantile_regression Usage: - ```python - loss = pinball_loss([0., 0., 1., 1.], [1., 1., 1., 0.], tau=.1) - # loss = max(0.1 * (y_true - y_pred), (0.1 - 1) * (y_true - y_pred)) - # = (0.9 + 0.9 + 0 + 0.1) / 4 - - print('Loss: ', loss.numpy()) # Loss: 0.475 - ``` + >>> loss = tfa.losses.pinball_loss([0., 0., 1., 1.], + ... [1., 1., 1., 0.], tau=.1) + >>> print('Loss: ', loss.numpy()) + Args: y_true: Ground truth values. shape = `[batch_size, d0, .. dN]` @@ -84,22 +81,18 @@ class PinballLoss(LossFunctionWrapper): See: https://en.wikipedia.org/wiki/Quantile_regression Usage: - ```python - pinball = tfa.losses.PinballLoss(tau=.1) - loss = pinball([0., 0., 1., 1.], [1., 1., 1., 0.]) - - # loss = max(0.1 * (y_true - y_pred), (0.1 - 1) * (y_true - y_pred)) - # = (0.9 + 0.9 + 0 + 0.1) / 4 - print('Loss: ', loss.numpy()) # Loss: 0.475 - ``` + >>> pinball = tfa.losses.PinballLoss(tau=.1) + + >>> loss = pinball([0., 0., 1., 1.], [1., 1., 1., 0.]) + >>> print('Loss: ', loss.numpy()) + Usage with the `tf.keras` API: - ```python - model = tf.keras.Model(inputs, outputs) - model.compile('sgd', loss=tfa.losses.PinballLoss(tau=.1)) - ``` + >>> model = tf.keras.Model() + >>> model.compile('sgd', loss=tfa.losses.PinballLoss(tau=.1)) + Args: tau: (Optional) Float in [0, 1] or a tensor taking values in [0, 1] and From 0e6a5c99e8e7471477137ee896cec502fd424cbc Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Tue, 1 Sep 2020 10:35:03 +0530 Subject: [PATCH 07/20] reformatting giou_loss.py --- tensorflow_addons/losses/giou_loss.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/losses/giou_loss.py b/tensorflow_addons/losses/giou_loss.py index dc9ee5ec60..10856e712f 100644 --- a/tensorflow_addons/losses/giou_loss.py +++ b/tensorflow_addons/losses/giou_loss.py @@ -61,8 +61,7 @@ def __init__( @tf.keras.utils.register_keras_serializable(package="Addons") -def giou_loss(y_true: TensorLike, y_pred: TensorLike, - mode: str = "giou") -> tf.Tensor: +def giou_loss(y_true: TensorLike, y_pred: TensorLike, mode: str = "giou") -> tf.Tensor: """Implements the GIoU loss function. GIoU loss was first introduced in the @@ -92,8 +91,7 @@ def giou_loss(y_true: TensorLike, y_pred: TensorLike, return 1 - giou -def _calculate_giou(b1: TensorLike, b2: TensorLike, - mode: str = "giou") -> tf.Tensor: +def _calculate_giou(b1: TensorLike, b2: TensorLike, mode: str = "giou") -> tf.Tensor: """ Args: b1: bounding box. The coordinates of the each bounding box in boxes are From 16df78319439982f41a92fb518e0e13d1da5676e Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Tue, 1 Sep 2020 18:02:09 +0530 Subject: [PATCH 08/20] updated testable docs contrastive.py --- tensorflow_addons/losses/contrastive.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/losses/contrastive.py b/tensorflow_addons/losses/contrastive.py index 28f394dfb5..54f63d0591 100644 --- a/tensorflow_addons/losses/contrastive.py +++ b/tensorflow_addons/losses/contrastive.py @@ -42,11 +42,12 @@ def contrastive_loss( ... [3, 6], ... [1, 8]], dtype=tf.float16) >>> y_pred = tf.linalg.norm(a - b, axis=1) + >>> y_pred - - + + <... Note: constants a & b have been used purely for + example purposes and have no significant value ...> See: http://yann.lecun.com/exdb/publis/pdf/hadsell-chopra-lecun-06.pdf @@ -93,11 +94,12 @@ class ContrastiveLoss(LossFunctionWrapper): ... [3, 6], ... [1, 8]], dtype=tf.float16) >>> y_pred = tf.linalg.norm(a - b, axis=1) + >>> y_pred - - + + <... Note: constants a & b have been used purely for + example purposes and have no significant value ...> Args: margin: `Float`, margin term in the loss definition. From 4d57abdb0018fc5cb787e3ce4032b6c5e3b60f16 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Tue, 1 Sep 2020 18:03:11 +0530 Subject: [PATCH 09/20] updated doctests focal_loss.py --- tensorflow_addons/losses/focal_loss.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/losses/focal_loss.py b/tensorflow_addons/losses/focal_loss.py index bf98643dff..3e1ad2863b 100644 --- a/tensorflow_addons/losses/focal_loss.py +++ b/tensorflow_addons/losses/focal_loss.py @@ -40,8 +40,9 @@ class SigmoidFocalCrossEntropy(LossFunctionWrapper): >>> loss = fl( ... y_true = [[1.0], [1.0], [0.0]], ... y_pred = [[0.97], [0.91], [0.03]]) - >>> print('Loss: ', loss.numpy()) - + >>>loss + Usage with `tf.keras` API: From d7d6a167b9449956cda06df82777d427e5a87052 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Tue, 1 Sep 2020 18:04:03 +0530 Subject: [PATCH 10/20] updated testdocs giou_loss.py --- tensorflow_addons/losses/giou_loss.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/losses/giou_loss.py b/tensorflow_addons/losses/giou_loss.py index 10856e712f..6f508609bb 100644 --- a/tensorflow_addons/losses/giou_loss.py +++ b/tensorflow_addons/losses/giou_loss.py @@ -37,9 +37,8 @@ class GIoULoss(LossFunctionWrapper): >>> boxes1 = tf.constant([[4.0, 3.0, 7.0, 5.0], [5.0, 6.0, 10.0, 7.0]]) >>> boxes2 = tf.constant([[3.0, 4.0, 6.0, 8.0], [14.0, 14.0, 15.0, 15.0]]) >>> loss = gl(boxes1, boxes2) - >>> print('Loss: ', loss.numpy()) - - ``` + >>> loss + Usage with `tf.keras` API: From 3bef6eef16d3cabbfa7e33ef167c78831c36b0c6 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Tue, 1 Sep 2020 18:04:49 +0530 Subject: [PATCH 11/20] updated testdocs kappa_loss.py --- tensorflow_addons/losses/kappa_loss.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tensorflow_addons/losses/kappa_loss.py b/tensorflow_addons/losses/kappa_loss.py index 7982f7bd62..862cc5212c 100644 --- a/tensorflow_addons/losses/kappa_loss.py +++ b/tensorflow_addons/losses/kappa_loss.py @@ -43,16 +43,17 @@ class WeightedKappaLoss(tf.keras.losses.Loss): >>> y_pred = tf.constant([[0.1, 0.2, 0.6, 0.1], [0.1, 0.5, 0.3, 0.1], ... [0.8, 0.05, 0.05, 0.1], [0.01, 0.09, 0.1, 0.8]]) >>> loss = kappa_loss(y_true, y_pred) - >>> print('Loss: ', loss.numpy()) - + >>> loss + Usage with `tf.keras` API: >>> model = tf.keras.Model() - >>> model.compile('sgd', loss=tfa.losses.WeightedKappa(num_classes=4)) - < outputs should be softmax results + >>> model.compile('sgd', loss=tfa.losses.WeightedKappaLoss(num_classes=4)) + + <... outputs should be softmax results if you want to weight the samples, just multiply the outputs - by the sample weight.> + by the sample weight ...> """ From 7fb5bfb7147cd7cac7e059b0fa0dc7d4a7c9b5b0 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Tue, 1 Sep 2020 18:05:30 +0530 Subject: [PATCH 12/20] updated testdocs npairs.py --- tensorflow_addons/losses/npairs.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/tensorflow_addons/losses/npairs.py b/tensorflow_addons/losses/npairs.py index 094e4b0ff1..a761e6de48 100644 --- a/tensorflow_addons/losses/npairs.py +++ b/tensorflow_addons/losses/npairs.py @@ -33,8 +33,6 @@ def npairs_loss(y_true: TensorLike, y_pred: TensorLike) -> tf.Tensor: The similarity matrix `y_pred` between two embedding matrices `a` and `b` with shape `[batch_size, hidden_size]` can be computed as follows: - >>> a = tf.constant([[1, 2], ... [3, 4], ... [5, 6]], dtype=tf.float16) @@ -42,12 +40,14 @@ def npairs_loss(y_true: TensorLike, y_pred: TensorLike) -> tf.Tensor: ... [3, 6], ... [1, 8]], dtype=tf.float16) >>> y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) + >>> y_pred - + + <... Note: constants a & b have been used purely for + example purposes and have no significant value ...> See: http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf @@ -107,12 +107,14 @@ def npairs_multilabel_loss(y_true: TensorLike, y_pred: TensorLike) -> tf.Tensor: ... [3, 6], ... [1, 8]], dtype=tf.float16) >>> y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) + >>> y_pred - + + <... Note: constants a & b have been used purely for + example purposes and have no significant value ...> See: http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf @@ -166,12 +168,14 @@ class NpairsLoss(tf.keras.losses.Loss): ... [3, 6], ... [1, 8]], dtype=tf.float16) >>> y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) + >>> y_pred - + + <... Note: constants a & b have been used purely for + example purposes and have no significant value ...> See: http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf @@ -220,12 +224,14 @@ class NpairsMultilabelLoss(tf.keras.losses.Loss): ... [3, 6], ... [1, 8]], dtype=tf.float16) >>> y_pred = tf.matmul(a, b, transpose_a=False, transpose_b=True) + >>> y_pred - + + <... Note: constants a & b have been used purely for + example purposes and have no significant value ...> See: http://www.nec-labs.com/uploads/images/Department-Images/MediaAnalytics/papers/nips16_npairmetriclearning.pdf From 91dd450ce12b121e35b690ab5041a9dcbac3c3f6 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Tue, 1 Sep 2020 18:07:08 +0530 Subject: [PATCH 13/20] updated testdocs quantiles.py --- tensorflow_addons/losses/quantiles.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/losses/quantiles.py b/tensorflow_addons/losses/quantiles.py index 037e37c2d9..513ffd2398 100644 --- a/tensorflow_addons/losses/quantiles.py +++ b/tensorflow_addons/losses/quantiles.py @@ -38,8 +38,8 @@ def pinball_loss( >>> loss = tfa.losses.pinball_loss([0., 0., 1., 1.], ... [1., 1., 1., 0.], tau=.1) - >>> print('Loss: ', loss.numpy()) - + >>> loss + Args: y_true: Ground truth values. shape = `[batch_size, d0, .. dN]` @@ -83,16 +83,14 @@ class PinballLoss(LossFunctionWrapper): Usage: >>> pinball = tfa.losses.PinballLoss(tau=.1) - >>> loss = pinball([0., 0., 1., 1.], [1., 1., 1., 0.]) - >>> print('Loss: ', loss.numpy()) - + >>> loss + Usage with the `tf.keras` API: >>> model = tf.keras.Model() >>> model.compile('sgd', loss=tfa.losses.PinballLoss(tau=.1)) - Args: tau: (Optional) Float in [0, 1] or a tensor taking values in [0, 1] and From ff0846aadd8d26f086fee81ce29f2e89afaad46e Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Wed, 2 Sep 2020 12:06:47 +0530 Subject: [PATCH 14/20] minor changes to formatting --- tensorflow_addons/losses/focal_loss.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/losses/focal_loss.py b/tensorflow_addons/losses/focal_loss.py index 3e1ad2863b..916144e916 100644 --- a/tensorflow_addons/losses/focal_loss.py +++ b/tensorflow_addons/losses/focal_loss.py @@ -40,7 +40,7 @@ class SigmoidFocalCrossEntropy(LossFunctionWrapper): >>> loss = fl( ... y_true = [[1.0], [1.0], [0.0]], ... y_pred = [[0.97], [0.91], [0.03]]) - >>>loss + >>> loss From 51d8eb6786559a106a318eb0767c7c53927d494f Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Wed, 2 Sep 2020 21:07:05 +0530 Subject: [PATCH 15/20] fixing multi-line error --- tensorflow_addons/losses/contrastive.py | 14 ++++++-------- tensorflow_addons/losses/focal_loss.py | 7 +++---- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/tensorflow_addons/losses/contrastive.py b/tensorflow_addons/losses/contrastive.py index 54f63d0591..aa6a50421c 100644 --- a/tensorflow_addons/losses/contrastive.py +++ b/tensorflow_addons/losses/contrastive.py @@ -43,8 +43,8 @@ def contrastive_loss( ... [1, 8]], dtype=tf.float16) >>> y_pred = tf.linalg.norm(a - b, axis=1) >>> y_pred - + <... Note: constants a & b have been used purely for example purposes and have no significant value ...> @@ -88,15 +88,13 @@ class ContrastiveLoss(LossFunctionWrapper): as follows: >>> a = tf.constant([[1, 2], - ... [3, 4], - ... [5, 6]], dtype=tf.float16) + ... [3, 4],[5, 6]], dtype=tf.float16) >>> b = tf.constant([[5, 9], - ... [3, 6], - ... [1, 8]], dtype=tf.float16) + ... [3, 6],[1, 8]], dtype=tf.float16) >>> y_pred = tf.linalg.norm(a - b, axis=1) >>> y_pred - + <... Note: constants a & b have been used purely for example purposes and have no significant value ...> diff --git a/tensorflow_addons/losses/focal_loss.py b/tensorflow_addons/losses/focal_loss.py index 916144e916..2e2028651b 100644 --- a/tensorflow_addons/losses/focal_loss.py +++ b/tensorflow_addons/losses/focal_loss.py @@ -38,11 +38,10 @@ class SigmoidFocalCrossEntropy(LossFunctionWrapper): >>> fl = tfa.losses.SigmoidFocalCrossEntropy() >>> loss = fl( - ... y_true = [[1.0], [1.0], [0.0]], - ... y_pred = [[0.97], [0.91], [0.03]]) + ... y_true = [[1.0], [1.0], [0.0]],y_pred = [[0.97], [0.91], [0.03]]) >>> loss - + Usage with `tf.keras` API: From 4d5a3bc120f54e284a68c2f67a3df9080d1caf52 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Fri, 4 Sep 2020 08:18:11 +0530 Subject: [PATCH 16/20] adding empty line inbetween imports --- tensorflow_addons/losses/contrastive.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/losses/contrastive.py b/tensorflow_addons/losses/contrastive.py index aa6a50421c..7f26d51ee9 100644 --- a/tensorflow_addons/losses/contrastive.py +++ b/tensorflow_addons/losses/contrastive.py @@ -16,6 +16,7 @@ import tensorflow as tf from typeguard import typechecked + from tensorflow_addons.utils.keras_utils import LossFunctionWrapper from tensorflow_addons.utils.types import TensorLike, Number From 0fdb5fadd2532dcfb98695b9cd43b63b4c2d8de8 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Fri, 4 Sep 2020 08:19:48 +0530 Subject: [PATCH 17/20] adding empty line inbetween imports --- tensorflow_addons/losses/focal_loss.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/losses/focal_loss.py b/tensorflow_addons/losses/focal_loss.py index 2e2028651b..cf74dc7ccf 100644 --- a/tensorflow_addons/losses/focal_loss.py +++ b/tensorflow_addons/losses/focal_loss.py @@ -17,6 +17,7 @@ import tensorflow as tf import tensorflow.keras.backend as K from typeguard import typechecked + from tensorflow_addons.utils.keras_utils import LossFunctionWrapper from tensorflow_addons.utils.types import FloatTensorLike, TensorLike From ad89d2746b6f0948c54fb52c66e4d637d884c4cb Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Fri, 4 Sep 2020 08:20:39 +0530 Subject: [PATCH 18/20] reformatting imports --- tensorflow_addons/losses/giou_loss.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tensorflow_addons/losses/giou_loss.py b/tensorflow_addons/losses/giou_loss.py index 6f508609bb..b6e2ad6568 100644 --- a/tensorflow_addons/losses/giou_loss.py +++ b/tensorflow_addons/losses/giou_loss.py @@ -15,8 +15,10 @@ """Implements GIoU loss.""" from typing import Optional + import tensorflow as tf from typeguard import typechecked + from tensorflow_addons.utils.keras_utils import LossFunctionWrapper from tensorflow_addons.utils.types import TensorLike From 92b99f15d98a5918e154d5a3780ce4c6b4213c1c Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Fri, 4 Sep 2020 08:21:38 +0530 Subject: [PATCH 19/20] reverting code formatting changes --- tensorflow_addons/losses/kappa_loss.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/losses/kappa_loss.py b/tensorflow_addons/losses/kappa_loss.py index 862cc5212c..717267d2e3 100644 --- a/tensorflow_addons/losses/kappa_loss.py +++ b/tensorflow_addons/losses/kappa_loss.py @@ -111,8 +111,8 @@ def __init__( self.weight_mat = (col_mat - row_mat) ** 2 def call(self, y_true, y_pred): - y_true = tf.cast(y_true, self.col_label_vec.dtype) - y_pred = tf.cast(y_pred, self.weight_mat.dtype) + y_true = tf.cast(y_true, dtype=self.col_label_vec.dtype) + y_pred = tf.cast(y_pred, dtype=self.weight_mat.dtype) batch_size = tf.shape(y_true)[0] cat_labels = tf.matmul(y_true, self.col_label_vec) cat_label_mat = tf.tile(cat_labels, [1, self.num_classes]) @@ -126,7 +126,7 @@ def call(self, y_true, y_pred): pred_dist = tf.reduce_sum(y_pred, axis=0, keepdims=True) w_pred_dist = tf.matmul(self.weight_mat, pred_dist, transpose_b=True) denominator = tf.reduce_sum(tf.matmul(label_dist, w_pred_dist)) - denominator /= tf.cast(batch_size, denominator.dtype) + denominator /= tf.cast(batch_size, dtype=denominator.dtype) loss = tf.math.divide_no_nan(numerator, denominator) return tf.math.log(loss + self.epsilon) From 1f7e9cb64204b6caf13f6df9da818c8bd0eafba8 Mon Sep 17 00:00:00 2001 From: Harsh188 Date: Fri, 4 Sep 2020 08:22:58 +0530 Subject: [PATCH 20/20] reformatting imports --- tensorflow_addons/losses/npairs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/losses/npairs.py b/tensorflow_addons/losses/npairs.py index a761e6de48..c7acc64992 100644 --- a/tensorflow_addons/losses/npairs.py +++ b/tensorflow_addons/losses/npairs.py @@ -15,8 +15,8 @@ """Implements npairs loss.""" import tensorflow as tf - from typeguard import typechecked + from tensorflow_addons.utils.types import TensorLike