From 7f8d0d61e39638bfa2c7420915828554df8734a9 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 20:30:40 +0500 Subject: [PATCH 001/259] update metrics cohen kappa --- tensorflow_addons/metrics/cohens_kappa.py | 41 ++++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cb473530b7..a09bd15f83 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,28 +38,29 @@ class CohenKappa(Metric): Usage: - ```python - actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) - preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) - weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) - - m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - m.update_state(actuals, preds) - print('Final result: ', m.result().numpy()) # Result: 0.61904764 - - # To use this with weights, sample_weight argument can be used. - m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - m.update_state(actuals, preds, sample_weight=weights) - print('Final result: ', m.result().numpy()) # Result: 0.37209308 - ``` - + >>> python + >>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) + >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) + >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) + + >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> m.update_state(actuals, preds) + >>> print('Final result: ', m.result().numpy()) + >>> # Result: 0.61904764 + + >>> # To use this with weights, sample_weight argument can be used. + >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> m.update_state(actuals, preds, sample_weight=weights) + >>> print('Final result: ', m.result().numpy()) + >>> # Result: 0.37209308 + Usage with tf.keras API: - ```python - model = tf.keras.models.Model(inputs, outputs) - model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) - model.compile('sgd', loss='mse') - ``` + >>> python + >>> model = tf.keras.models.Model(inputs, outputs) + >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) + >>> model.compile('sgd', loss='mse') + """ @typechecked From 26f8c36a05ef1095c87cb5c80bf98c44a1b61bbf Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 20:51:22 +0500 Subject: [PATCH 002/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index a09bd15f83..f78d56851e 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -53,14 +53,14 @@ class CohenKappa(Metric): >>> m.update_state(actuals, preds, sample_weight=weights) >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.37209308 - + Usage with tf.keras API: >>> python >>> model = tf.keras.models.Model(inputs, outputs) >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) >>> model.compile('sgd', loss='mse') - + """ @typechecked From d18438269b15a7526f1781cd2a24a5d4e372f672 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:05:13 +0500 Subject: [PATCH 003/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index f78d56851e..cf4ebe43e3 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,7 +38,7 @@ class CohenKappa(Metric): Usage: - >>> python + >>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) @@ -56,7 +56,6 @@ class CohenKappa(Metric): Usage with tf.keras API: - >>> python >>> model = tf.keras.models.Model(inputs, outputs) >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) >>> model.compile('sgd', loss='mse') From 19c9fff403bcd7b4ef4596dc9df5b20c767bbce2 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:09:33 +0500 Subject: [PATCH 004/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cf4ebe43e3..266429caa2 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,13 +45,13 @@ class CohenKappa(Metric): >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds) - >>> print('Final result: ', m.result().numpy()) + >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds, sample_weight=weights) - >>> print('Final result: ', m.result().numpy()) + >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.37209308 Usage with tf.keras API: From e2ce1181503fea7e989c8272ca36636a276bf1dd Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:30:54 +0500 Subject: [PATCH 005/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 9702b030ea..d69cee12f1 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -38,14 +38,13 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: Usage: - ```python - actuals = tf.constant([1, 1, 0, 0, 1, 0, 1, 0, 0, 1], - dtype=tf.int32) - predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], - dtype=tf.int32) - result = hamming_distance(actuals, predictions) - print('Hamming distance: ', result.numpy()) - ``` + >>> actuals = tf.constant([1, 1, 0, 0, 1, 0, 1, 0, 0, 1], + ... dtype=tf.int32) + >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], + ... dtype=tf.int32) + >>> result = hamming_distance(actuals, predictions) + ... print('Hamming distance: ', result.numpy()) + """ result = tf.not_equal(actuals, predictions) not_eq = tf.reduce_sum(tf.cast(result, tf.float32)) From 9ad346f70b4c67385e864d265e1f24e1a4fa7a09 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:34:10 +0500 Subject: [PATCH 006/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 266429caa2..a6f3f40fd7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,14 +45,14 @@ class CohenKappa(Metric): >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds) - >>> print('Final result: ', m.result().numpy()) - >>> # Result: 0.61904764 + >>> # print('Final result: ', m.result().numpy()) + >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds, sample_weight=weights) - >>> print('Final result: ', m.result().numpy()) - >>> # Result: 0.37209308 + >>> # print('Final result: ', m.result().numpy()) + >>> # Final result: 0.37209308 Usage with tf.keras API: From 58fd7d0a56ae8bf94decfb3d6ba7c6b6676c54f1 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:40:19 +0500 Subject: [PATCH 007/259] Update matthews_correlation_coefficient.py --- .../matthews_correlation_coefficient.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index e6afa53f61..a9ae447889 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -42,18 +42,18 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ((TP + FP) * (TP + FN) * (TN + FP ) * (TN + FN))^(1/2) Usage: - ```python - actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]], - dtype=tf.float32) - preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], - dtype=tf.float32) - # Matthews correlation coefficient - mcc = MatthewsCorrelationCoefficient(num_classes=1) - mcc.update_state(actuals, preds) - print('Matthews correlation coefficient is:', - mcc.result().numpy()) - # Matthews correlation coefficient is : -0.33333334 - ``` + + >>> actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]], + ... dtype=tf.float32) + >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], + ... dtype=tf.float32) + >>> # Matthews correlation coefficient + >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) + >>> mcc.update_state(actuals, preds) + >>> print('Matthews correlation coefficient is:', + ... mcc.result().numpy()) + >>> # Matthews correlation coefficient is : -0.33333334 + """ @typechecked From cf3ef63a7039fbb24813e127ab95f8f7d16a47e3 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:46:15 +0500 Subject: [PATCH 008/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index d13a355b33..1d0dc6f153 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -46,30 +46,29 @@ class MultiLabelConfusionMatrix(Metric): - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - ```python - # multilabel confusion matrix - y_true = tf.constant([[1, 0, 1], [0, 1, 0]], - dtype=tf.int32) - y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], - dtype=tf.int32) - output = MultiLabelConfusionMatrix(num_classes=3) - output.update_state(y_true, y_pred) - print('Confusion matrix:', output.result().numpy()) - - # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - [[0 1] [1 0]]] - - # if multiclass input is provided - y_true = tf.constant([[1, 0, 0], [0, 1, 0]], - dtype=tf.int32) - y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], - dtype=tf.int32) - output = MultiLabelConfusionMatrix(num_classes=3) - output.update_state(y_true, y_pred) - print('Confusion matrix:', output.result().numpy()) - - # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] - ``` + >>> # multilabel confusion matrix + >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], + ... dtype=tf.int32) + >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], + ... dtype=tf.int32) + >>> output = MultiLabelConfusionMatrix(num_classes=3) + >>> output.update_state(y_true, y_pred) + >>> print('Confusion matrix:', output.result().numpy()) + + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] + ... [[0 1] [1 0]]] + + >>> # if multiclass input is provided + >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], + ... dtype=tf.int32) + >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], + ... dtype=tf.int32) + >>> output = MultiLabelConfusionMatrix(num_classes=3) + >>> output.update_state(y_true, y_pred) + >>> print('Confusion matrix:', output.result().numpy()) + + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + """ @typechecked From 3f885d7d1f01c9ca84cdc2967dfdb7c5fae0de7d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:48:58 +0500 Subject: [PATCH 009/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 8ae78fc543..ba4c0ce486 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -61,13 +61,13 @@ class RSquare(Metric): of the same metric. Usage: - ```python - actuals = tf.constant([1, 4, 3], dtype=tf.float32) - preds = tf.constant([2, 4, 4], dtype=tf.float32) - result = tf.keras.metrics.RSquare() - result.update_state(actuals, preds) - print('R^2 score is: ', r1.result().numpy()) # 0.57142866 - ``` + + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) + >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) + >>> result = tf.keras.metrics.RSquare() + >>> result.update_state(actuals, preds) + >>> print('R^2 score is: ', r1.result().numpy()) # 0.57142866 + """ @typechecked From a83932b1402a3ce2b5169c32c3273eebb19052c6 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:53:40 +0500 Subject: [PATCH 010/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index ba4c0ce486..3725deeb66 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -66,8 +66,9 @@ class RSquare(Metric): >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> result = tf.keras.metrics.RSquare() >>> result.update_state(actuals, preds) - >>> print('R^2 score is: ', r1.result().numpy()) # 0.57142866 - + >>> print('R^2 score is: ', r1.result().numpy()) + >>> # R^2 score is: 0.57142866 + """ @typechecked From 1eee83871514ac537d59eba6eb3ed6ab0f1c5e8d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:18:30 +0500 Subject: [PATCH 011/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index a6f3f40fd7..c1446876e8 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds) + >>> # m.update_state(actuals, preds) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds, sample_weight=weights) + >>> # m.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.37209308 From 8c963b1b9dfa48fda324aa0f4075634cd9641a96 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:23:44 +0500 Subject: [PATCH 012/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 1d0dc6f153..a50b442137 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> output.update_state(y_true, y_pred) - >>> print('Confusion matrix:', output.result().numpy()) + >>> # output.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] ... [[0 1] [1 0]]] @@ -64,8 +64,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> output.update_state(y_true, y_pred) - >>> print('Confusion matrix:', output.result().numpy()) + >>> # output.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From d370fed313f749ef14eeb9a878ce9ca2aaa8eb3a Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:25:14 +0500 Subject: [PATCH 013/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index a9ae447889..23850b981f 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -50,7 +50,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> # Matthews correlation coefficient >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) - >>> print('Matthews correlation coefficient is:', + >>> # print('Matthews correlation coefficient is:', ... mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 From 8d3789f20501f71a65051d2863a8f63512eb5ed3 Mon Sep 17 00:00:00 2001 From: Sean Morgan Date: Thu, 3 Sep 2020 09:28:57 -0700 Subject: [PATCH 014/259] Update README.md (#2148) --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c483809120..dbbfe85811 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ what it was tested against. | TensorFlow Addons | TensorFlow | Python | |:----------------------- |:---|:---------- | | tfa-nightly | 2.2, 2.3 | 3.5, 3.6, 3.7, 3.8 | -| tensorflow-addons-0.11.1 | 2.2, 2.3 |3.5, 3.6, 3.7, 3.8 | +| tensorflow-addons-0.11.2 | 2.2, 2.3 |3.5, 3.6, 3.7, 3.8 | | tensorflow-addons-0.10.0 | 2.2 |3.5, 3.6, 3.7, 3.8 | | tensorflow-addons-0.9.1 | 2.1, 2.2 |3.5, 3.6, 3.7 | | tensorflow-addons-0.8.3 | 2.1 |3.5, 3.6, 3.7 | @@ -107,7 +107,7 @@ is compiled differently. A typical example of this would be `conda`-installed Te | TensorFlow Addons | TensorFlow | Compiler | cuDNN | CUDA | |:----------------------- |:---- |:---------|:---------|:---------| | tfa-nightly | 2.3 | GCC 7.3.1 | 7.6 | 10.1 | -| tensorflow-addons-0.11.1 | 2.3 | GCC 7.3.1 | 7.6 | 10.1 | +| tensorflow-addons-0.11.2 | 2.3 | GCC 7.3.1 | 7.6 | 10.1 | | tensorflow-addons-0.10.0 | 2.2 | GCC 7.3.1 | 7.6 | 10.1 | | tensorflow-addons-0.9.1 | 2.1 | GCC 7.3.1 | 7.6 | 10.1 | | tensorflow-addons-0.8.3 | 2.1 | GCC 7.3.1 | 7.6 | 10.1 | From d466cb8ec3cb8de325b9cb7f48a773f5bbeec808 Mon Sep 17 00:00:00 2001 From: Harshith MohanKumar <47203161+Harsh188@users.noreply.github.com> Date: Fri, 4 Sep 2020 09:09:12 +0530 Subject: [PATCH 015/259] #2066 doctest update losses (#2138) * updated contrastive.py * updated focal_loss.py * updated giou_loss.py * updated kappa_loss.py * updated npairs.py * updated quantiles.py * reformatting giou_loss.py * updated testable docs contrastive.py * updated doctests focal_loss.py * updated testdocs giou_loss.py * updated testdocs kappa_loss.py * updated testdocs npairs.py * updated testdocs quantiles.py * minor changes to formatting * fixing multi-line error * adding empty line inbetween imports * adding empty line inbetween imports * reformatting imports * reverting code formatting changes * reformatting imports --- tensorflow_addons/losses/contrastive.py | 34 ++++++++--- tensorflow_addons/losses/focal_loss.py | 23 +++----- tensorflow_addons/losses/giou_loss.py | 25 ++++---- tensorflow_addons/losses/kappa_loss.py | 32 +++++----- tensorflow_addons/losses/npairs.py | 78 +++++++++++++++++++------ tensorflow_addons/losses/quantiles.py | 29 ++++----- 6 files changed, 133 insertions(+), 88 deletions(-) diff --git a/tensorflow_addons/losses/contrastive.py b/tensorflow_addons/losses/contrastive.py index c6a9e9f826..7f26d51ee9 100644 --- a/tensorflow_addons/losses/contrastive.py +++ b/tensorflow_addons/losses/contrastive.py @@ -15,10 +15,10 @@ """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 +36,19 @@ 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) + >>> 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 @@ -79,10 +88,17 @@ 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) + >>> 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. diff --git a/tensorflow_addons/losses/focal_loss.py b/tensorflow_addons/losses/focal_loss.py index 8f29951277..cf74dc7ccf 100644 --- a/tensorflow_addons/losses/focal_loss.py +++ b/tensorflow_addons/losses/focal_loss.py @@ -16,10 +16,10 @@ 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 +37,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]]) + >>> loss + 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. diff --git a/tensorflow_addons/losses/giou_loss.py b/tensorflow_addons/losses/giou_loss.py index e789fcacb1..b6e2ad6568 100644 --- a/tensorflow_addons/losses/giou_loss.py +++ b/tensorflow_addons/losses/giou_loss.py @@ -14,11 +14,13 @@ # ============================================================================== """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 +35,17 @@ 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) + >>> loss + 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. diff --git a/tensorflow_addons/losses/kappa_loss.py b/tensorflow_addons/losses/kappa_loss.py index c041cb8b01..717267d2e3 100644 --- a/tensorflow_addons/losses/kappa_loss.py +++ b/tensorflow_addons/losses/kappa_loss.py @@ -37,24 +37,24 @@ 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) + >>> loss + 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.WeightedKappaLoss(num_classes=4)) + + <... outputs should be softmax results + if you want to weight the samples, just multiply the outputs + by the sample weight ...> + """ @typechecked diff --git a/tensorflow_addons/losses/npairs.py b/tensorflow_addons/losses/npairs.py index 1693123505..c7acc64992 100644 --- a/tensorflow_addons/losses/npairs.py +++ b/tensorflow_addons/losses/npairs.py @@ -15,9 +15,9 @@ """Implements npairs loss.""" import tensorflow as tf +from typeguard import typechecked from tensorflow_addons.utils.types import TensorLike -from typeguard import typechecked @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) + >>> 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 @@ -89,10 +100,21 @@ 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) + >>> 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 @@ -139,10 +161,21 @@ 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) + >>> 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 @@ -184,10 +217,21 @@ 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) + >>> 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 diff --git a/tensorflow_addons/losses/quantiles.py b/tensorflow_addons/losses/quantiles.py index 2aad531294..513ffd2398 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) + >>> loss + Args: y_true: Ground truth values. shape = `[batch_size, d0, .. dN]` @@ -84,22 +81,16 @@ 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.]) + >>> loss + 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 e5d641dfad4d64f3d07d5ea769908aa8f498a304 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:20:40 +0500 Subject: [PATCH 016/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index d69cee12f1..5b52a15e25 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> result = hamming_distance(actuals, predictions) - ... print('Hamming distance: ', result.numpy()) + >>> print('Hamming distance: ', result.numpy()) """ result = tf.not_equal(actuals, predictions) From c68e59ef1ab912c57da0b944322ee33bca76b37c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:31:36 +0500 Subject: [PATCH 017/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 5b52a15e25..8519ccdf2c 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> result = hamming_distance(actuals, predictions) - >>> print('Hamming distance: ', result.numpy()) + >>> #print('Hamming distance: ', result.numpy()) """ result = tf.not_equal(actuals, predictions) From 4f5f77029a51f65cfea87d00025403b7cb6cf2d5 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:44:29 +0500 Subject: [PATCH 018/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 3725deeb66..1388c861e0 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,7 +64,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tf.keras.metrics.RSquare() + >>> result = tf.keras.metrics.r_square.RSquare() >>> result.update_state(actuals, preds) >>> print('R^2 score is: ', r1.result().numpy()) >>> # R^2 score is: 0.57142866 From fbb15516c62d086db8922277d26544431f1115b6 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:27 +0500 Subject: [PATCH 019/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 23850b981f..533ed42a74 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -50,8 +50,9 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> # Matthews correlation coefficient >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) + >>> # uncomment the lines below to check the result >>> # print('Matthews correlation coefficient is:', - ... mcc.result().numpy()) + ... # mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 79e155df7f264be5baa8f07f5ae24ee73e3e6506 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:33 +0500 Subject: [PATCH 020/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index a50b442137..e9b53e1ca5 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,7 +56,7 @@ class MultiLabelConfusionMatrix(Metric): >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - ... [[0 1] [1 0]]] + ... #[[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], From b9404ddf56b6947034d7687999a1aada55727315 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:39 +0500 Subject: [PATCH 021/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 1388c861e0..310cb674df 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,7 +64,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tf.keras.metrics.r_square.RSquare() + >>> result = tfa.metrics.r_square.RSquare() >>> result.update_state(actuals, preds) >>> print('R^2 score is: ', r1.result().numpy()) >>> # R^2 score is: 0.57142866 From 119236051edf2b2c0e6dbadd62d2ea56a746359c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:57:23 +0500 Subject: [PATCH 022/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 533ed42a74..674b0e4440 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,8 +51,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', - ... # mcc.result().numpy()) + >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 83be56f60625689abb06255e23fd89497a0e9d8b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 22:14:54 +0500 Subject: [PATCH 023/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index e9b53e1ca5..da10e306b4 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,8 +55,7 @@ class MultiLabelConfusionMatrix(Metric): >>> # output.update_state(y_true, y_pred) >>> # print('Confusion matrix:', output.result().numpy()) - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - ... #[[0 1] [1 0]]] + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], From 6d972897b5e466a8c7fce39fefd5f02e95cf1ad5 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 22:28:26 +0500 Subject: [PATCH 024/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 310cb674df..2b941e5318 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,9 +64,9 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) - >>> print('R^2 score is: ', r1.result().numpy()) + >>> result = tfa.metrics.r_square.RSquare() + >>> # Uncomment the lines below to see the result + >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 """ From 6441acdd53efba3a78a4df6e96242da4c2be82dd Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 23:39:03 +0500 Subject: [PATCH 025/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 2b941e5318..bd0a5792b6 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,7 +64,8 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() + >>> result = tfa.metrics.r_square.RSquare() + >>> result.update_state(actuals, preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From eae4f29da93f032244276f6db977e7d4367bc07d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 12:36:12 +0500 Subject: [PATCH 026/259] keras Api example --- tensorflow_addons/metrics/cohens_kappa.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index c1446876e8..ab6f52b01b 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -56,9 +56,11 @@ class CohenKappa(Metric): Usage with tf.keras API: - >>> model = tf.keras.models.Model(inputs, outputs) - >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) - >>> model.compile('sgd', loss='mse') + >>> inputs = tf.keras.Input(shape=(10,)) + >>> x = tf.keras.layers.Dense(10)(inputs) + >>> outputs = tf.keras.layers.Dense(1)(x) + >>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs) + >>> model.compile('sgd', loss='mse', metrics=[tfm.CohenKappa(num_classes=3, sparse_labels=True)]) """ From cc0f689000a9611f945faef8e03bd61fc406e83b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 12:48:25 +0500 Subject: [PATCH 027/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index ab6f52b01b..b39c52298e 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -60,7 +60,7 @@ class CohenKappa(Metric): >>> x = tf.keras.layers.Dense(10)(inputs) >>> outputs = tf.keras.layers.Dense(1)(x) >>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs) - >>> model.compile('sgd', loss='mse', metrics=[tfm.CohenKappa(num_classes=3, sparse_labels=True)]) + >>> model.compile('sgd', loss='mse', metrics=[tfa.metrics.CohenKappa(num_classes=3, sparse_labels=True)]) """ From c137b16d000c59f37e1c944d02391aa4ea70c7d1 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 13:09:00 +0500 Subject: [PATCH 028/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index bd0a5792b6..f6b60d4687 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -60,15 +60,15 @@ class RSquare(Metric): ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) of the same metric. - Usage: - - >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) - >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) - >>> # Uncomment the lines below to see the result - >>> # print('R^2 score is: ', result(actuals, preds).numpy()) - >>> # R^2 score is: 0.57142866 + Usage: + + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) + >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) + >>> result = tfa.metrics.r_square.RSquare() + >>> result.update_state(actuals, preds) + >>> # Uncomment the lines below to see the result + >>> # print('R^2 score is: ', result(actuals, preds).numpy()) + >>> # R^2 score is: 0.57142866 """ From 27e31470bccd2dc6f64789492690be82d0ea1b96 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 13:17:44 +0500 Subject: [PATCH 029/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index f6b60d4687..5ed8d5c2be 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -45,23 +45,23 @@ def _reduce_average( class RSquare(Metric): """Compute R^2 score. - This is also called the [coefficient of determination - ](https://en.wikipedia.org/wiki/Coefficient_of_determination). - It tells how close are data to the fitted regression line. + This is also called the [coefficient of determination + ](https://en.wikipedia.org/wiki/Coefficient_of_determination). + It tells how close are data to the fitted regression line. - - Highest score can be 1.0 and it indicates that the predictors + - Highest score can be 1.0 and it indicates that the predictors perfectly accounts for variation in the target. - - Score 0.0 indicates that the predictors do not + - Score 0.0 indicates that the predictors do not account for variation in the target. - - It can also be negative if the model is worse. + - It can also be negative if the model is worse. - The sample weighting for this metric implementation mimics the - behaviour of the [scikit-learn implementation - ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) - of the same metric. + The sample weighting for this metric implementation mimics the + behaviour of the [scikit-learn implementation + ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) + of the same metric. Usage: - + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> result = tfa.metrics.r_square.RSquare() From aa6744078291cd08bcfc5b101a1ba00458023b21 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 00:32:20 +0500 Subject: [PATCH 030/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 674b0e4440..9f56f158c5 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -48,7 +48,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], ... dtype=tf.float32) >>> # Matthews correlation coefficient - >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) + >>> mcc = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) >>> # uncomment the lines below to check the result >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) From 91cdf73570612dfece4f3b57154d14ee5879e745 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 07:51:26 +0500 Subject: [PATCH 031/259] uncommented update_state --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index b39c52298e..efed0be879 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # m.update_state(actuals, preds) + >>> m.update_state(actuals, preds) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # m.update_state(actuals, preds, sample_weight=weights) + >>> m.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.37209308 From 9fb5b53ab21f8a4f3b53498e4c554ff0008dedcb Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:20:40 +0500 Subject: [PATCH 032/259] requested_change --- tensorflow_addons/metrics/cohens_kappa.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index efed0be879..2040bc8101 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -43,15 +43,15 @@ class CohenKappa(Metric): >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) - >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds) - >>> # print('Final result: ', m.result().numpy()) + >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. - >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds, sample_weight=weights) - >>> # print('Final result: ', m.result().numpy()) + >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> metric.update_state(actuals, preds, sample_weight=weights) + >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 Usage with tf.keras API: From 04f68e6277c96d27668cd50881a5089f1fa7981e Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:21:25 +0500 Subject: [PATCH 033/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 2040bc8101..b2bd42cba7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,7 +44,7 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> # metric.update_state(y_true = actuals, y_pred = preds) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.61904764 From 87d471188d3c490df7b0af14325b1f65f46173ba Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:25:14 +0500 Subject: [PATCH 034/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 5ed8d5c2be..98d9b735bd 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,8 +64,8 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) + >>> metric = tfa.metrics.r_square.RSquare() + >>> metric.update_state(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From 134f9ef985554f4f9becbb872b0c37cf852e55cf Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:28:35 +0500 Subject: [PATCH 035/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index da10e306b4..7ec50a66de 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -51,9 +51,9 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) - >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> # output.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', output.result().numpy()) + >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> # metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', metric.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -62,9 +62,9 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> # output.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', output.result().numpy()) + >>> metric = MultiLabelConfusionMatrix(num_classes=3) + >>> # metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', metric.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From 3424dc15c282277948f46f0bc8c4209261b0bea7 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:29:59 +0500 Subject: [PATCH 036/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 7ec50a66de..bc5d92d691 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -62,7 +62,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> metric = MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) From 0d568afb5209bec471118f77845f4b6aadda9115 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:32:10 +0500 Subject: [PATCH 037/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 9f56f158c5..9227be1080 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -48,10 +48,10 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], ... dtype=tf.float32) >>> # Matthews correlation coefficient - >>> mcc = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> mcc.update_state(actuals, preds) + >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) + >>> metric.update_state(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) + >>> # print('Matthews correlation coefficient is:', metric.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From db472cb380f40449ba2e8bb62498f41d9e5e25e4 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:46:05 +0500 Subject: [PATCH 038/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 49 +++++++++++++--------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 8519ccdf2c..a55ec61be0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,8 +42,8 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> result = hamming_distance(actuals, predictions) - >>> #print('Hamming distance: ', result.numpy()) + >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> #print('Hamming distance: ', metric.numpy()) """ result = tf.not_equal(actuals, predictions) @@ -83,31 +83,26 @@ def hamming_loss_fn( Usage: - ```python - # multi-class hamming loss - hl = HammingLoss(mode='multiclass', threshold=0.6) - actuals = tf.constant([[1, 0, 0, 0],[0, 0, 1, 0], - [0, 0, 0, 1],[0, 1, 0, 0]], - dtype=tf.float32) - predictions = tf.constant([[0.8, 0.1, 0.1, 0], - [0.2, 0, 0.8, 0], - [0.05, 0.05, 0.1, 0.8], - [1, 0, 0, 0]], - dtype=tf.float32) - hl.update_state(actuals, predictions) - print('Hamming loss: ', hl.result().numpy()) # 0.25 - - # multi-label hamming loss - hl = HammingLoss(mode='multilabel', threshold=0.8) - actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], - [0, 0, 0,1]], dtype=tf.int32) - predictions = tf.constant([[0.82, 0.5, 0.90, 0], - [0, 1, 0.4, 0.98], - [0.89, 0.79, 0, 0.3]], - dtype=tf.float32) - hl.update_state(actuals, predictions) - print('Hamming loss: ', hl.result().numpy()) # 0.16666667 - ``` + >>> # multi-class hamming loss + >>> hl = HammingLoss(mode='multiclass', threshold=0.6) + >>> actuals = tf.constant([[1, 0, 0, 0],[0, 0, 1, 0], + ... [0, 0, 0, 1],[0, 1, 0, 0]], dtype=tf.float32) + >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], + ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], + ... dtype=tf.float32) + >>> hl.update_state(y_true = actuals, y_pred = predictions) + >>> #uncomment the line below to see the result + >>> #print('Hamming loss: ', hl.result().numpy()) # 0.25 + + >>> # multi-label hamming loss + >>> hl = HammingLoss(mode='multilabel', threshold=0.8) + >>> actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], + ... [0, 0, 0,1]], dtype=tf.int32) + >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], + ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) + >>> hl.update_state(y_true = actuals, y_pred = predictions) + >>> #print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + """ if mode not in ["multiclass", "multilabel"]: raise TypeError("mode must be either multiclass or multilabel]") From 61ccc42b8da31a6080beace2eb6538a8f1a102b3 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:47:35 +0500 Subject: [PATCH 039/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index bce8e15509..a123a9f224 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -35,9 +35,9 @@ class GeometricMean(Metric): Usage: - >>> m = tfa.metrics.GeometricMean() - >>> m.update_state([1, 3, 5, 7, 9]) - >>> m.result().numpy() + >>> metric = tfa.metrics.GeometricMean() + >>> metric.update_state([1, 3, 5, 7, 9]) + >>> metric.result().numpy() 3.9362833 """ From 20a74ff38970dc66a7dfdef6832d8f1013ab6756 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:17:35 +0500 Subject: [PATCH 040/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index b2bd42cba7..cce36ef738 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -50,7 +50,7 @@ class CohenKappa(Metric): >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> metric.update_state(actuals, preds, sample_weight=weights) + >>> # metric.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 From 1240bd3fab022a764cb4dc8de7df6ec1258ecc5b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:38:34 +0500 Subject: [PATCH 041/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index a55ec61be0..6776077c2f 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,9 +90,9 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - >>> hl.update_state(y_true = actuals, y_pred = predictions) - >>> #uncomment the line below to see the result - >>> #print('Hamming loss: ', hl.result().numpy()) # 0.25 + >>> # hl.update_state(actuals, predictions) + >>> # uncomment the line below to see the result + >>> # print('Hamming loss: ', hl.result().numpy()) # 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -100,8 +100,8 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - >>> hl.update_state(y_true = actuals, y_pred = predictions) - >>> #print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + >>> # hl.update_state(actuals, predictions) + >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From facd2c959e23154ef8cfaea6d63eb8ac137459c6 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:56:40 +0500 Subject: [PATCH 042/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index bc5d92d691..7cdd9deb26 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -51,7 +51,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) - >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) @@ -62,7 +62,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) From 82ac7ced430d2d5b634debaa2e9903b25818aef2 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:58:48 +0500 Subject: [PATCH 043/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 6776077c2f..b7ccaa85d6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> metric = hamming_distance(actuals, predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From d049d5821e33b23f91763964506191e28ab5d01f Mon Sep 17 00:00:00 2001 From: Tzu-Wei Sung Date: Tue, 8 Sep 2020 01:30:31 -0700 Subject: [PATCH 044/259] Add doctest section (#2151) --- CONTRIBUTING.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 431d5031bb..b6ba2d6bda 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -325,6 +325,24 @@ bazel test -c opt -k \ //tensorflow_addons/... ``` +#### Testing docstrings + +We use [DocTest](https://docs.python.org/3/library/doctest.html) to test code snippets +in Python docstrings. The snippet must be executable Python code. +To enable testing, prepend the line with `>>>` (three left-angle brackets). +Available namespace include `np` for numpy, `tf` for TensorFlow, and `tfa` for TensorFlow Addons. +See [docs_ref](https://www.tensorflow.org/community/contribute/docs_ref) for more details. + +To test docstrings locally, run either +```bash +bash tools/run_cpu_tests.sh +``` +on all files, or +```bash +pytest -v -n auto --durations=25 --doctest-modules /path/to/pyfile +``` +on specific files. + ## About type hints Ideally, we would like all the functions and classes constructors exposed in From fc2c796ac7082da9032c113ddbdb35be5eb107a8 Mon Sep 17 00:00:00 2001 From: Billy Lamberta Date: Wed, 9 Sep 2020 09:07:52 -0700 Subject: [PATCH 045/259] nbfmt tutorial notebooks (#2155) --- docs/tutorials/_template.ipynb | 31 +---- .../average_optimizers_callback.ipynb | 86 ++----------- docs/tutorials/image_ops.ipynb | 113 +++--------------- docs/tutorials/layers_normalizations.ipynb | 28 +---- .../layers_weightnormalization.ipynb | 34 +----- docs/tutorials/losses_triplet.ipynb | 36 +----- docs/tutorials/networks_seq2seq_nmt.ipynb | 90 ++------------ .../optimizers_conditionalgradient.ipynb | 81 ++----------- docs/tutorials/optimizers_lazyadam.ipynb | 28 +---- docs/tutorials/time_stopping.ipynb | 37 +----- docs/tutorials/tqdm_progress_bar.ipynb | 33 +---- 11 files changed, 64 insertions(+), 533 deletions(-) diff --git a/docs/tutorials/_template.ipynb b/docs/tutorials/_template.ipynb index 1f80a64979..feec9c8738 100644 --- a/docs/tutorials/_template.ipynb +++ b/docs/tutorials/_template.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Tce3stUlHN0L" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "tuOe1ymfHZPu" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "qFdPvlXBOdUN" }, "source": [ @@ -47,7 +43,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MfBg1C5NB3X0" }, "source": [ @@ -70,7 +65,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "r6P32iYYV27b" }, "source": [ @@ -80,7 +74,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "xHxb-dlhMIzW" }, "source": [ @@ -92,7 +85,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MUXex9ctTuDB" }, "source": [ @@ -102,7 +94,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "1Eh-iCRVBm0p" }, "source": [ @@ -112,7 +103,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "rEk-ibQkDNtF" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -122,8 +115,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "IqR2PQG4ZaZ0" }, "outputs": [], @@ -135,7 +126,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "UhNtHfuxCGVy" }, "source": [ @@ -149,7 +139,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "2V22fKegUtF9" }, "source": [ @@ -169,7 +158,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "QKp40qS-DGEZ" }, "source": [ @@ -184,8 +172,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "KtylpxOmceaC" }, "outputs": [], @@ -200,7 +186,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "pwdM2pl3RSPb" }, "source": [ @@ -211,8 +196,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "mMOeXVmbdilM" }, "outputs": [], @@ -230,7 +213,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "uabQmjMtRtzs" }, "source": [ @@ -241,8 +223,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "U82B_tH2d294" }, "outputs": [], @@ -254,7 +234,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "TJdqBNBbS78n" }, "source": [ @@ -271,7 +250,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "78HBT9cQXJko" }, "source": [ @@ -286,7 +264,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "YrsKXcPRUvK9" }, "source": [ @@ -307,8 +284,6 @@ "Tce3stUlHN0L" ], "name": "_template.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { diff --git a/docs/tutorials/average_optimizers_callback.ipynb b/docs/tutorials/average_optimizers_callback.ipynb index 7e1c53c642..1e9bae3ea4 100644 --- a/docs/tutorials/average_optimizers_callback.ipynb +++ b/docs/tutorials/average_optimizers_callback.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Tce3stUlHN0L" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "tuOe1ymfHZPu" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MfBg1C5NB3X0" }, "source": [ @@ -62,7 +58,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "xHxb-dlhMIzW" }, "source": [ @@ -74,7 +69,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "o2UNySlpXkbl" }, "source": [ @@ -96,7 +90,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MUXex9ctTuDB" }, "source": [ @@ -107,7 +100,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "scrolled": true + "id": "sXEOqj5cIgyW" }, "outputs": [], "source": [ @@ -118,13 +111,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 35 - }, - "colab_type": "code", - "id": "IqR2PQG4ZaZ0", - "outputId": "49c5f5be-4b1a-4298-e218-5c6c0126b4ff" + "id": "IqR2PQG4ZaZ0" }, "outputs": [], "source": [ @@ -136,8 +123,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "4hnJ2rDpI38-" }, "outputs": [], @@ -149,7 +134,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Iox_HZNNYLEB" }, "source": [ @@ -160,8 +144,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "KtylpxOmceaC" }, "outputs": [], @@ -184,7 +166,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "pwdM2pl3RSPb" }, "source": [ @@ -195,8 +176,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "mMOeXVmbdilM" }, "outputs": [], @@ -217,7 +196,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "iEbhI_eajpJe" }, "source": [ @@ -234,8 +212,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "_Q76K1fNk7Va" }, "outputs": [], @@ -249,7 +225,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "nXlMX4p9qHwg" }, "source": [ @@ -260,8 +235,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "SnvZjt34qEHY" }, "outputs": [], @@ -280,7 +253,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "uabQmjMtRtzs" }, "source": [ @@ -290,7 +262,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "SPmifETHmPix" }, "source": [ @@ -301,13 +272,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 421 - }, - "colab_type": "code", - "id": "Xy8W4LYppadJ", - "outputId": "97bfbf95-d9e7-4c69-99e2-d233e8f54d5a" + "id": "Xy8W4LYppadJ" }, "outputs": [], "source": [ @@ -322,13 +287,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 72 - }, - "colab_type": "code", - "id": "uU2iQ6HAZ6-E", - "outputId": "ec4ae0ad-dd99-4966-d448-270fae1fd0d3" + "id": "uU2iQ6HAZ6-E" }, "outputs": [], "source": [ @@ -342,7 +301,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "lAvhD4unmc6W" }, "source": [ @@ -353,13 +311,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 404 - }, - "colab_type": "code", - "id": "--NIjBp-mhVb", - "outputId": "51510f00-c0cc-4443-cef3-0b1c263e1600" + "id": "--NIjBp-mhVb" }, "outputs": [], "source": [ @@ -374,13 +326,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 72 - }, - "colab_type": "code", - "id": "zRAym9EBmnW9", - "outputId": "ac7dc6d6-c5c6-456f-af8e-a11a211ef357" + "id": "zRAym9EBmnW9" }, "outputs": [], "source": [ @@ -394,7 +340,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "K98lbU07m_Bk" }, "source": [ @@ -405,13 +350,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 329 - }, - "colab_type": "code", - "id": "Ia7ALKefnXWQ", - "outputId": "ecf67a66-baf3-43df-ff9c-da14bc6148fa" + "id": "Ia7ALKefnXWQ" }, "outputs": [], "source": [ @@ -426,13 +365,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 72 - }, - "colab_type": "code", - "id": "EOT2E9NBoeHI", - "outputId": "4117451c-1886-4980-b40e-68591912559e" + "id": "EOT2E9NBoeHI" }, "outputs": [], "source": [ @@ -450,13 +383,10 @@ "Tce3stUlHN0L" ], "name": "average_optimizers_callback.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3", - "language": "python", "name": "python3" } }, diff --git a/docs/tutorials/image_ops.ipynb b/docs/tutorials/image_ops.ipynb index ccbada7ec0..58d9655c77 100644 --- a/docs/tutorials/image_ops.ipynb +++ b/docs/tutorials/image_ops.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "GWEKvPCCxJke" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "l-m8KQ-nxK5l" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "O8FuVCLYxi_l" }, "source": [ @@ -62,7 +58,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "2a5ksOt-xsOl" }, "source": [ @@ -89,7 +84,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "DMbjxr4PyMPF" }, "source": [ @@ -99,7 +93,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "o_QTX_vHGbj7" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -109,13 +105,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "colab_type": "code", - "id": "5hVIKCrhWh4a", - "outputId": "365ae823-365c-4141-9d84-e4ccfb0c85c7" + "id": "5hVIKCrhWh4a" }, "outputs": [], "source": [ @@ -128,7 +118,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Q6Z2rsP8yp2v" }, "source": [ @@ -138,7 +127,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "9gbgJP10z9WO" }, "source": [ @@ -149,13 +137,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 52 - }, - "colab_type": "code", - "id": "IgUsVhBQ6dSg", - "outputId": "20ecbebf-6ed9-4e59-f958-c94b2921ba74" + "id": "IgUsVhBQ6dSg" }, "outputs": [], "source": [ @@ -165,7 +147,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "uheQOL-y0Fj3" }, "source": [ @@ -175,7 +156,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MFGirRRZ0Y9k" }, "source": [ @@ -186,13 +166,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 281 - }, - "colab_type": "code", - "id": "NRlvNQdm1YI8", - "outputId": "1922ef25-a9f8-4fb4-8f60-65402a6fd969" + "id": "NRlvNQdm1YI8" }, "outputs": [], "source": [ @@ -208,7 +182,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "clXQrFVa2nN7" }, "source": [ @@ -219,13 +192,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 281 - }, - "colab_type": "code", - "id": "tbaIkUCS2eNv", - "outputId": "7e2796dd-26d9-4856-fa6e-7b5a084583c5" + "id": "tbaIkUCS2eNv" }, "outputs": [], "source": [ @@ -238,7 +205,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "UwqfpOm--vV2" }, "source": [ @@ -248,7 +214,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "jIa5HnomPds3" }, "source": [ @@ -260,13 +225,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 269 - }, - "colab_type": "code", - "id": "SutWnbRoHl6i", - "outputId": "52333d9d-788d-4c46-df94-f24456abb67f" + "id": "SutWnbRoHl6i" }, "outputs": [], "source": [ @@ -277,7 +236,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Mp6cU7I0-r2h" }, "source": [ @@ -289,13 +247,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 269 - }, - "colab_type": "code", - "id": "9kxUES9sM8Jl", - "outputId": "1340b774-5fbd-4c94-ae26-a35fa3c50546" + "id": "9kxUES9sM8Jl" }, "outputs": [], "source": [ @@ -306,7 +258,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "WjMdSDKlBcPh" }, "source": [ @@ -318,13 +269,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 269 - }, - "colab_type": "code", - "id": "HTh1Qpps8Rg5", - "outputId": "00dd3893-ffbe-4bdd-de3c-9f037651b36b" + "id": "HTh1Qpps8Rg5" }, "outputs": [], "source": [ @@ -335,7 +280,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "O79BrK-bC8oh" }, "source": [ @@ -347,13 +291,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 269 - }, - "colab_type": "code", - "id": "zZBI-9XvBSuh", - "outputId": "452460e3-13ee-4eb7-ef4e-dd8a86c897eb" + "id": "zZBI-9XvBSuh" }, "outputs": [], "source": [ @@ -369,7 +307,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "ruyvVnmCDBgj" }, "source": [ @@ -381,13 +318,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 269 - }, - "colab_type": "code", - "id": "vbCdwGtYChnQ", - "outputId": "430b5cf5-8a0a-497b-fa77-af6792bd5bf7" + "id": "vbCdwGtYChnQ" }, "outputs": [], "source": [ @@ -401,7 +332,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "fdbCDYJkG8Gv" }, "source": [ @@ -413,13 +343,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 269 - }, - "colab_type": "code", - "id": "dG557eQDDtSK", - "outputId": "641b79fd-e23a-4bb5-d162-eb670b2b88d0" + "id": "dG557eQDDtSK" }, "outputs": [], "source": [ @@ -435,7 +359,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "FcLMnSKYPcjA" }, "source": [ @@ -448,13 +371,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 269 - }, - "colab_type": "code", - "id": "-OMh6oeRQaYQ", - "outputId": "4f7e9aca-ed78-4e4d-8c06-6833ec899797" + "id": "-OMh6oeRQaYQ" }, "outputs": [], "source": [ @@ -472,8 +389,6 @@ "colab": { "collapsed_sections": [], "name": "image_ops.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { diff --git a/docs/tutorials/layers_normalizations.ipynb b/docs/tutorials/layers_normalizations.ipynb index 10d6b29c28..2a770fce4e 100644 --- a/docs/tutorials/layers_normalizations.ipynb +++ b/docs/tutorials/layers_normalizations.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "wFPyjGqMQ82Q" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "aNZ7aEDyQIYU" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "uMOmzhPEQh7b" }, "source": [ @@ -62,7 +58,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "cthm5dovQMJl" }, "source": [ @@ -105,7 +100,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "I2XlcXf5WBHb" }, "source": [ @@ -115,7 +109,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "kTlbneoEUKrD" }, "source": [ @@ -125,7 +118,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "_ZQGY_ALnirQ" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -135,8 +130,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "7aGgPZG_WBHg" }, "outputs": [], @@ -148,7 +141,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "u82Gz_gOUPDZ" }, "source": [ @@ -159,8 +151,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "3wso9oidUZZQ" }, "outputs": [], @@ -174,7 +164,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "UTQH56j89POZ" }, "source": [ @@ -193,8 +182,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "aIGjLwYWAm0v" }, "outputs": [], @@ -220,7 +207,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "QMwUfJUib3ka" }, "source": [ @@ -238,8 +224,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "6sLVv-C8f6Kf" }, "outputs": [], @@ -269,7 +253,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "qYdnEocRUCll" }, "source": [ @@ -288,8 +271,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "Fh-Pp_e5UB54" }, "outputs": [], @@ -315,7 +296,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "shvGfnB0WpQQ" }, "source": [ @@ -334,8 +314,6 @@ "colab": { "collapsed_sections": [], "name": "layers_normalizations.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { diff --git a/docs/tutorials/layers_weightnormalization.ipynb b/docs/tutorials/layers_weightnormalization.ipynb index 0de107e390..9a7dc9cbdf 100644 --- a/docs/tutorials/layers_weightnormalization.ipynb +++ b/docs/tutorials/layers_weightnormalization.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Tce3stUlHN0L" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "tuOe1ymfHZPu" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MfBg1C5NB3X0" }, "source": [ @@ -62,7 +58,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "xHxb-dlhMIzW" }, "source": [ @@ -74,7 +69,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "KR01t9v_fxbT" }, "source": [ @@ -94,7 +88,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MUXex9ctTuDB" }, "source": [ @@ -104,7 +97,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "CyWHXw9mQ6mp" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -114,8 +109,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "OywLbs7EXiE_" }, "outputs": [], @@ -128,8 +121,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "KQMhhq1qXiFF" }, "outputs": [], @@ -142,8 +133,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "ULWHqMAnTVZD" }, "outputs": [], @@ -157,7 +146,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "fhM0ieDpSnKh" }, "source": [ @@ -168,8 +156,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "7XZXnBYgRPSk" }, "outputs": [], @@ -191,8 +177,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "UZd6V90eR4Gm" }, "outputs": [], @@ -213,7 +197,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "AA5dti8AS2Y7" }, "source": [ @@ -224,8 +207,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "L8Isjc7W8MEn" }, "outputs": [], @@ -245,7 +226,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "cH1CG9E7S34C" }, "source": [ @@ -256,8 +236,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "EvNKxfaI7vSm" }, "outputs": [], @@ -277,8 +255,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "esmMh-5g7wmp" }, "outputs": [], @@ -298,8 +274,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "yujf2YRbwX55" }, "outputs": [], @@ -325,8 +299,6 @@ "colab": { "collapsed_sections": [], "name": "layers_weightnormalization.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { diff --git a/docs/tutorials/losses_triplet.ipynb b/docs/tutorials/losses_triplet.ipynb index 857d17353c..0130191bd8 100644 --- a/docs/tutorials/losses_triplet.ipynb +++ b/docs/tutorials/losses_triplet.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Tce3stUlHN0L" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "tuOe1ymfHZPu" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MfBg1C5NB3X0" }, "source": [ @@ -62,7 +58,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "xHxb-dlhMIzW" }, "source": [ @@ -77,7 +72,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "bQwBbFVAyHJ_" }, "source": [ @@ -96,7 +90,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "wPJ5521HZHeL" }, "source": [ @@ -107,7 +100,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MUXex9ctTuDB" }, "source": [ @@ -117,7 +109,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "6Vyo25M2ba1P" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -127,8 +121,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "IqR2PQG4ZaZ0" }, "outputs": [], @@ -141,8 +133,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "WH_7-ZYZYblV" }, "outputs": [], @@ -155,7 +145,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "0_D7CZqkv_Hj" }, "source": [ @@ -166,8 +155,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "iXvByj6wcT7d" }, "outputs": [], @@ -189,7 +176,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "KR01t9v_fxbT" }, "source": [ @@ -199,7 +185,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "wvOPPuIKhLJi" }, "source": [ @@ -210,8 +195,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "djpoAvfWNyL5" }, "outputs": [], @@ -233,7 +216,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "HYE-BxhOzFQp" }, "source": [ @@ -244,8 +226,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "NxfYhtiSzHf-" }, "outputs": [], @@ -260,8 +240,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "TGBYNGxgVDrj" }, "outputs": [], @@ -276,8 +254,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "1Y--0tK69SXf" }, "outputs": [], @@ -290,8 +266,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "dqSuLdVZGNrZ" }, "outputs": [], @@ -316,7 +290,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "VAtj_m6Z_Uwe" }, "source": [ @@ -326,7 +299,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Y4rjlG9rlbVA" }, "source": [ @@ -342,8 +314,6 @@ "colab": { "collapsed_sections": [], "name": "losses_triplet.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { diff --git a/docs/tutorials/networks_seq2seq_nmt.ipynb b/docs/tutorials/networks_seq2seq_nmt.ipynb index f935b6d11d..ac40502943 100644 --- a/docs/tutorials/networks_seq2seq_nmt.ipynb +++ b/docs/tutorials/networks_seq2seq_nmt.ipynb @@ -4,8 +4,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "f9ySOjrcc0Yp" }, "outputs": [], @@ -18,8 +16,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "bl9GdT7h0Hxk" }, "outputs": [], @@ -40,7 +36,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "WhwgQAn50EZp" }, "source": [ @@ -65,7 +60,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "ip0n8178Fuwm" }, "source": [ @@ -85,7 +79,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "YNiadLKNLleD" }, "source": [ @@ -95,7 +88,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "82GcQTsGf414" }, "source": [ @@ -113,7 +105,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "vKBON2miJ_g1" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons\n", @@ -124,8 +118,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "5OIlpST_6ga-" }, "outputs": [], @@ -140,13 +132,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "colab_type": "code", - "id": "co6-YpBwL-4d", - "outputId": "6571961c-8f50-4333-9b1d-5eb1a157f4f8" + "id": "co6-YpBwL-4d" }, "outputs": [], "source": [ @@ -177,7 +163,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "q7gjUT_9XSoj" }, "source": [ @@ -190,8 +175,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "6ZIu-TNqKFsd" }, "outputs": [], @@ -315,7 +298,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "5nDIELt9RH-w" }, "source": [ @@ -330,13 +312,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 119 - }, - "colab_type": "code", - "id": "GMxdlVU1X8yI", - "outputId": "f4977f48-dbe9-4323-ec2a-a9b0cf8b1895" + "id": "GMxdlVU1X8yI" }, "outputs": [], "source": [ @@ -358,7 +334,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Cfb66QxWYr6A" }, "source": [ @@ -369,8 +344,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "3oq60MBPSanQ" }, "outputs": [], @@ -392,8 +365,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "XH5oSRNeSc1s" }, "outputs": [], @@ -406,7 +377,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "KdM37lNBGXAj" }, "source": [ @@ -417,8 +387,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "EfiBUJM2Et6C" }, "outputs": [], @@ -436,7 +404,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Ff_jQHLhGqJU" }, "source": [ @@ -447,13 +414,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - }, - "colab_type": "code", - "id": "b__1hPHVFALO", - "outputId": "88d35286-184c-44e7-a16b-5559f22e2eb1" + "id": "b__1hPHVFALO" }, "outputs": [], "source": [ @@ -471,7 +432,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "UQRgJcYgapqE" }, "source": [ @@ -482,8 +442,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "sGdakRtjaokF" }, "outputs": [], @@ -538,7 +496,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "NPwcfddTa0oB" }, "source": [ @@ -549,8 +506,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "x1BEqVyra2jW" }, "outputs": [], @@ -618,8 +573,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "71Lkdx6GFb3A" }, "outputs": [], @@ -632,7 +585,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "v5uzLcu2bNX3" }, "source": [ @@ -643,13 +595,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 1000 - }, - "colab_type": "code", - "id": "PvfD2SknWrt6", - "outputId": "0a427bb7-8184-4076-97ca-f638116ca52b" + "id": "PvfD2SknWrt6" }, "outputs": [], "source": [ @@ -669,7 +615,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "nDyK-EGqbN5r" }, "source": [ @@ -680,13 +625,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 326 - }, - "colab_type": "code", - "id": "y98sfom7SuGy", - "outputId": "00d94338-e841-4bd6-f9e3-509ef1f1a08b" + "id": "y98sfom7SuGy" }, "outputs": [], "source": [ @@ -767,7 +706,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "iodjSItQds1t" }, "source": [ @@ -778,13 +716,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 102 - }, - "colab_type": "code", - "id": "K6aWFB5IWlH2", - "outputId": "2179c9a3-cb27-447a-ac94-0e5ab2920aff" + "id": "K6aWFB5IWlH2" }, "outputs": [], "source": [ @@ -801,7 +733,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "g6Av-oPWvRc4" }, "source": [ @@ -816,13 +747,10 @@ "colab": { "collapsed_sections": [], "name": "networks_seq2seq_nmt.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3", - "language": "python", "name": "python3" } }, diff --git a/docs/tutorials/optimizers_conditionalgradient.ipynb b/docs/tutorials/optimizers_conditionalgradient.ipynb index b825a0903e..bf964b640a 100644 --- a/docs/tutorials/optimizers_conditionalgradient.ipynb +++ b/docs/tutorials/optimizers_conditionalgradient.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "pGUYKbJNWNgj" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "1PzPJglSWgnW" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "b5P4BEg1XYd5" }, "source": [ @@ -63,7 +59,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Faj8luWnYNSG" }, "source": [ @@ -74,7 +69,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MrDjqjY6YRYM" }, "source": [ @@ -87,7 +81,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "dooBaYGLYYnn" }, "source": [ @@ -97,7 +90,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "2sCyoNXlgGbk" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -107,13 +102,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "colab_type": "code", - "id": "qYo0FkL4O7io", - "outputId": "7c40f5df-075a-4d9f-910e-7dff84c46b1f" + "id": "qYo0FkL4O7io" }, "outputs": [], "source": [ @@ -126,8 +115,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "kR0PnjrIirpJ" }, "outputs": [], @@ -140,7 +127,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "-x0WBp-IYz7x" }, "source": [ @@ -151,8 +137,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "4KzMDUT0i1QE" }, "outputs": [], @@ -167,7 +151,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "XGADNG3-Y7aa" }, "source": [ @@ -178,13 +161,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 52 - }, - "colab_type": "code", - "id": "d6a-kbM_i1b2", - "outputId": "248bd642-2d06-4da5-acbe-b1bf59d335b0" + "id": "d6a-kbM_i1b2" }, "outputs": [], "source": [ @@ -201,7 +178,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "sOlB-WqjZp1Y" }, "source": [ @@ -212,8 +188,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "8LCmRXUgZqyV" }, "outputs": [], @@ -236,8 +210,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "udSvzKm4Z5Zr" }, "outputs": [], @@ -251,7 +223,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "qfhE1DfwZC1i" }, "source": [ @@ -264,13 +235,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 382 - }, - "colab_type": "code", - "id": "6-AMaOYEi1kK", - "outputId": "28534147-5ccd-48cf-f8b5-a6afdcf7612a" + "id": "6-AMaOYEi1kK" }, "outputs": [], "source": [ @@ -293,7 +258,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "8OJp4So9bYYR" }, "source": [ @@ -304,8 +268,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "SuizUueqn449" }, "outputs": [], @@ -321,8 +283,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "V8QC3xCwbfNl" }, "outputs": [], @@ -337,13 +297,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 382 - }, - "colab_type": "code", - "id": "9BNi4yXGcDlg", - "outputId": "be21bdc7-c693-4663-fa5a-f036fc4b6140" + "id": "9BNi4yXGcDlg" }, "outputs": [], "source": [ @@ -365,7 +319,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "1Myw0FVcd_Z9" }, "source": [ @@ -375,7 +328,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "0tJYQBRt-ZUl" }, "source": [ @@ -386,13 +338,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 296 - }, - "colab_type": "code", - "id": "Ewf17MW1cJVI", - "outputId": "481d5d00-5642-458d-aa86-225d7faf07ff" + "id": "Ewf17MW1cJVI" }, "outputs": [], "source": [ @@ -412,7 +358,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "JGtutiXuoZyx" }, "source": [ @@ -423,13 +368,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 296 - }, - "colab_type": "code", - "id": "s-SNIr10o2va", - "outputId": "1226fcbd-b71a-442b-811c-7155edbe623d" + "id": "s-SNIr10o2va" }, "outputs": [], "source": [ @@ -448,8 +387,6 @@ "colab": { "collapsed_sections": [], "name": "optimizers_conditionalgradient.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { diff --git a/docs/tutorials/optimizers_lazyadam.ipynb b/docs/tutorials/optimizers_lazyadam.ipynb index 1d688abb5e..90d0de2370 100644 --- a/docs/tutorials/optimizers_lazyadam.ipynb +++ b/docs/tutorials/optimizers_lazyadam.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Tce3stUlHN0L" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "tuOe1ymfHZPu" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MfBg1C5NB3X0" }, "source": [ @@ -62,7 +58,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "xHxb-dlhMIzW" }, "source": [ @@ -74,7 +69,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "bQwBbFVAyHJ_" }, "source": [ @@ -95,7 +89,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MUXex9ctTuDB" }, "source": [ @@ -105,7 +98,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "cHAOyeOVx-k3" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -115,8 +110,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "42ztALK4ZdyZ" }, "outputs": [], @@ -129,8 +122,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "ys65MwOLKnXq" }, "outputs": [], @@ -143,7 +134,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "KR01t9v_fxbT" }, "source": [ @@ -154,8 +144,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "djpoAvfWNyL5" }, "outputs": [], @@ -170,7 +158,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "0_D7CZqkv_Hj" }, "source": [ @@ -181,8 +168,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "U0bS3SyowBoB" }, "outputs": [], @@ -200,7 +185,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "HYE-BxhOzFQp" }, "source": [ @@ -213,8 +197,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "NxfYhtiSzHf-" }, "outputs": [], @@ -237,8 +219,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "1Y--0tK69SXf" }, "outputs": [], @@ -255,8 +235,6 @@ "colab": { "collapsed_sections": [], "name": "optimizers_lazyadam.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { diff --git a/docs/tutorials/time_stopping.ipynb b/docs/tutorials/time_stopping.ipynb index dae8d40bd4..bbcc83d673 100644 --- a/docs/tutorials/time_stopping.ipynb +++ b/docs/tutorials/time_stopping.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "mz0tl581YjZ0" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "hi0OrWAIYjZ4" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "gyGdPCvQYjaI" }, "source": [ @@ -47,7 +43,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "Z5csJXPVYjaM" }, "source": [ @@ -70,7 +65,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "BJhody3KYjaP" }, "source": [ @@ -81,7 +75,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "SaZsCaGbYjaU" }, "source": [ @@ -91,7 +84,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "VgJGPL3ts_1i" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -101,8 +96,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "fm_dHPvEYjar" }, "outputs": [], @@ -117,7 +110,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "vg0y1DrQYja4" }, "source": [ @@ -128,13 +120,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 52 - }, - "colab_type": "code", - "id": "HydkzZTuYja8", - "outputId": "bacf85d9-1b6a-42a3-98ff-e3926f54a3f2" + "id": "HydkzZTuYja8" }, "outputs": [], "source": [ @@ -147,7 +133,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "uX02I1kxYjbL" }, "source": [ @@ -158,8 +143,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "Tlk0MyEfYjbN" }, "outputs": [], @@ -179,7 +162,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "b5Xcyt0qYjbX" }, "source": [ @@ -190,14 +172,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 139 - }, - "colab_type": "code", - "id": "W82_IZ6iYjbZ", - "outputId": "3518f04b-38d1-4974-cbf9-42ff8a9b581e", - "scrolled": true + "id": "W82_IZ6iYjbZ" }, "outputs": [], "source": [ @@ -218,8 +193,6 @@ "metadata": { "colab": { "name": "time_stopping.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { diff --git a/docs/tutorials/tqdm_progress_bar.ipynb b/docs/tutorials/tqdm_progress_bar.ipynb index 9debbf6510..7a3927d7ed 100644 --- a/docs/tutorials/tqdm_progress_bar.ipynb +++ b/docs/tutorials/tqdm_progress_bar.ipynb @@ -3,7 +3,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "MyujzrAv2Vpk" }, "source": [ @@ -15,8 +14,6 @@ "execution_count": null, "metadata": { "cellView": "form", - "colab": {}, - "colab_type": "code", "id": "rTUqXTqa2Vpm" }, "outputs": [], @@ -37,7 +34,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "rNnfCHh82Vpq" }, "source": [ @@ -47,7 +43,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "4qrDJoTw2Vps" }, "source": [ @@ -70,7 +65,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "bVS_PkvX2Vpt" }, "source": [ @@ -81,7 +75,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "sRldODz32Vpu" }, "source": [ @@ -91,7 +84,9 @@ { "cell_type": "code", "execution_count": null, - "metadata": {}, + "metadata": { + "id": "H0yZwcvcR4Gc" + }, "outputs": [], "source": [ "!pip install -U tensorflow-addons" @@ -101,8 +96,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "etYr-Suo4KYj" }, "outputs": [], @@ -121,8 +114,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "SfXA0mI13pSE" }, "outputs": [], @@ -144,7 +135,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "2RGuwIwe2Vp7" }, "source": [ @@ -155,8 +145,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "qKfrsOSP2Vp8" }, "outputs": [], @@ -170,7 +158,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "ORtL0s4X2VqB" }, "source": [ @@ -181,8 +168,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "z8uAGGV32VqC" }, "outputs": [], @@ -202,7 +187,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "YWOnH1ga2VqF" }, "source": [ @@ -213,10 +197,7 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", - "id": "Vl_oj_OW2VqG", - "scrolled": true + "id": "Vl_oj_OW2VqG" }, "outputs": [], "source": [ @@ -237,7 +218,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "uFvBfwJN2VqK" }, "source": [ @@ -249,8 +229,6 @@ "cell_type": "code", "execution_count": null, "metadata": { - "colab": {}, - "colab_type": "code", "id": "Np3dD8bhe10E" }, "outputs": [], @@ -262,7 +240,6 @@ { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "36WRBMo7e10I" }, "source": [ @@ -274,8 +251,6 @@ "metadata": { "colab": { "name": "tqdm_progress_bar.ipynb", - "private_outputs": true, - "provenance": [], "toc_visible": true }, "kernelspec": { From fc50c3403c893c8913883f05db09d05e568193d6 Mon Sep 17 00:00:00 2001 From: Tzu-Wei Sung Date: Wed, 9 Sep 2020 19:25:32 -0700 Subject: [PATCH 046/259] Support fill_mode for transform (#2153) * Support fill_mode for transform --- .../image/tests/transform_ops_test.py | 56 +++++++++++++++++++ tensorflow_addons/image/transform_ops.py | 23 ++++++++ tensorflow_addons/image/translate_ops.py | 29 +++++++--- 3 files changed, 99 insertions(+), 9 deletions(-) diff --git a/tensorflow_addons/image/tests/transform_ops_test.py b/tensorflow_addons/image/tests/transform_ops_test.py index 094024bb2f..e208aa314b 100644 --- a/tensorflow_addons/image/tests/transform_ops_test.py +++ b/tensorflow_addons/image/tests/transform_ops_test.py @@ -68,6 +68,62 @@ def test_extreme_projective_transform(dtype): ) +@pytest.mark.with_device(["cpu", "gpu"]) +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +@pytest.mark.parametrize("dtype", _DTYPES) +def test_transform_constant_fill_mode(dtype): + image = tf.constant( + [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]], dtype=dtype + ) + expected = np.asarray( + [[0, 0, 1, 2], [0, 4, 5, 6], [0, 8, 9, 10], [0, 12, 13, 14]], + dtype=dtype.as_numpy_dtype, + ) + # Translate right by 1 (the transformation matrix is always inverted, + # hence the -1). + translation = tf.constant([1, 0, -1, 0, 1, 0, 0, 0], dtype=tf.float32) + image_transformed = transform_ops.transform( + image, translation, fill_mode="constant" + ) + np.testing.assert_equal(image_transformed.numpy(), expected) + + +@pytest.mark.with_device(["cpu", "gpu"]) +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +@pytest.mark.parametrize("dtype", _DTYPES) +def test_transform_reflect_fill_mode(dtype): + image = tf.constant( + [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]], dtype=dtype + ) + expected = np.asarray( + [[0, 0, 1, 2], [4, 4, 5, 6], [8, 8, 9, 10], [12, 12, 13, 14]], + dtype=dtype.as_numpy_dtype, + ) + # Translate right by 1 (the transformation matrix is always inverted, + # hence the -1). + translation = tf.constant([1, 0, -1, 0, 1, 0, 0, 0], dtype=tf.float32) + image_transformed = transform_ops.transform(image, translation, fill_mode="reflect") + np.testing.assert_equal(image_transformed.numpy(), expected) + + +@pytest.mark.with_device(["cpu", "gpu"]) +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +@pytest.mark.parametrize("dtype", _DTYPES) +def test_transform_wrap_fill_mode(dtype): + image = tf.constant( + [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]], dtype=dtype + ) + expected = np.asarray( + [[3, 0, 1, 2], [7, 4, 5, 6], [11, 8, 9, 10], [15, 12, 13, 14]], + dtype=dtype.as_numpy_dtype, + ) + # Translate right by 1 (the transformation matrix is always inverted, + # hence the -1). + translation = tf.constant([1, 0, -1, 0, 1, 0, 0, 0], dtype=tf.float32) + image_transformed = transform_ops.transform(image, translation, fill_mode="wrap") + np.testing.assert_equal(image_transformed.numpy(), expected) + + @pytest.mark.usefixtures("maybe_run_functions_eagerly") def test_transform_static_output_shape(): image = tf.constant([[1.0, 2.0], [3.0, 4.0]]) diff --git a/tensorflow_addons/image/transform_ops.py b/tensorflow_addons/image/transform_ops.py index c9bbf4804d..d789ec7fbb 100644 --- a/tensorflow_addons/image/transform_ops.py +++ b/tensorflow_addons/image/transform_ops.py @@ -36,6 +36,7 @@ def transform( images: TensorLike, transforms: TensorLike, interpolation: str = "NEAREST", + fill_mode: str = "CONSTANT", output_shape: Optional[list] = None, name: Optional[str] = None, ) -> tf.Tensor: @@ -55,6 +56,15 @@ def transform( gradients are not backpropagated into transformation parameters. interpolation: Interpolation mode. Supported values: "NEAREST", "BILINEAR". + fill_mode: Points outside the boundaries of the input are filled according + to the given mode (one of `{'constant', 'reflect', 'wrap'}`). + - *reflect*: `(d c b a | a b c d | d c b a)` + The input is extended by reflecting about the edge of the last pixel. + - *constant*: `(k k k k | a b c d | k k k k)` + The input is extended by filling all values beyond the edge with the + same constant value k = 0. + - *wrap*: `(a b c d | a b c d | a b c d)` + The input is extended by wrapping around to the opposite edge. output_shape: Output dimesion after the transform, [height, width]. If None, output is the same size as input image. @@ -105,11 +115,13 @@ def transform( % len(transforms.get_shape()) ) + # TODO(WindQAQ): Support "nearest" `fill_mode` and `fill_value` in TF2.4. output = tf.raw_ops.ImageProjectiveTransformV2( images=images, transforms=transforms, output_shape=output_shape, interpolation=interpolation.upper(), + fill_mode=fill_mode.upper(), ) return img_utils.from_4D_image(output, original_ndims) @@ -268,6 +280,7 @@ def rotate( images: TensorLike, angles: TensorLike, interpolation: str = "NEAREST", + fill_mode: str = "CONSTANT", name: Optional[str] = None, ) -> tf.Tensor: """Rotate image(s) counterclockwise by the passed angle(s) in radians. @@ -282,6 +295,15 @@ def rotate( batch. interpolation: Interpolation mode. Supported values: "NEAREST", "BILINEAR". + fill_mode: Points outside the boundaries of the input are filled according + to the given mode (one of `{'constant', 'reflect', 'wrap'}`). + - *reflect*: `(d c b a | a b c d | d c b a)` + The input is extended by reflecting about the edge of the last pixel. + - *constant*: `(k k k k | a b c d | k k k k)` + The input is extended by filling all values beyond the edge with the + same constant value k = 0. + - *wrap*: `(a b c d | a b c d | a b c d)` + The input is extended by wrapping around to the opposite edge. name: The name of the op. Returns: @@ -304,6 +326,7 @@ def rotate( images, angles_to_projective_transforms(angles, image_height, image_width), interpolation=interpolation, + fill_mode=fill_mode, ) return img_utils.from_4D_image(output, original_ndims) diff --git a/tensorflow_addons/image/translate_ops.py b/tensorflow_addons/image/translate_ops.py index e51eafefbc..e0f4516bc0 100644 --- a/tensorflow_addons/image/translate_ops.py +++ b/tensorflow_addons/image/translate_ops.py @@ -75,26 +75,36 @@ def translate( images: TensorLike, translations: TensorLike, interpolation: str = "NEAREST", + fill_mode: str = "CONSTANT", name: Optional[str] = None, ) -> tf.Tensor: """Translate image(s) by the passed vectors(s). Args: images: A tensor of shape - `(num_images, num_rows, num_columns, num_channels)` (NHWC), - `(num_rows, num_columns, num_channels)` (HWC), or - `(num_rows, num_columns)` (HW). The rank must be statically known (the - shape is not `TensorShape(None)`). + `(num_images, num_rows, num_columns, num_channels)` (NHWC), + `(num_rows, num_columns, num_channels)` (HWC), or + `(num_rows, num_columns)` (HW). The rank must be statically known (the + shape is not `TensorShape(None)`). translations: A vector representing `[dx, dy]` or (if `images` has rank 4) - a matrix of length num_images, with a `[dx, dy]` vector for each image - in the batch. + a matrix of length num_images, with a `[dx, dy]` vector for each image + in the batch. interpolation: Interpolation mode. Supported values: "NEAREST", - "BILINEAR". + "BILINEAR". + fill_mode: Points outside the boundaries of the input are filled according + to the given mode (one of `{'constant', 'reflect', 'wrap'}`). + - *reflect*: `(d c b a | a b c d | d c b a)` + The input is extended by reflecting about the edge of the last pixel. + - *constant*: `(k k k k | a b c d | k k k k)` + The input is extended by filling all values beyond the edge with the + same constant value k = 0. + - *wrap*: `(a b c d | a b c d | a b c d)` + The input is extended by wrapping around to the opposite edge. name: The name of the op. Returns: Image(s) with the same type and shape as `images`, translated by the - given vector(s). Empty space due to the translation will be filled with - zeros. + given vector(s). Empty space due to the translation will be filled with + zeros. Raises: TypeError: If `images` is an invalid type. """ @@ -103,6 +113,7 @@ def translate( images, translations_to_projective_transforms(translations), interpolation=interpolation, + fill_mode=fill_mode, ) From 3d37870fc59b6b32f991eb2ef1b802374dc59ab4 Mon Sep 17 00:00:00 2001 From: Tzu-Wei Sung Date: Wed, 9 Sep 2020 19:29:54 -0700 Subject: [PATCH 047/259] Speedup gaussian kernel generation (#2149) * Speedup gaussian kernel generation * Use newaxis instead of reshape --- tensorflow_addons/image/filters.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tensorflow_addons/image/filters.py b/tensorflow_addons/image/filters.py index 6e3da3e1ba..92046f50ec 100644 --- a/tensorflow_addons/image/filters.py +++ b/tensorflow_addons/image/filters.py @@ -211,8 +211,7 @@ def _get_gaussian_kernel(sigma, filter_shape): sigma = tf.convert_to_tensor(sigma) x = tf.range(-filter_shape // 2 + 1, filter_shape // 2 + 1) x = tf.cast(x ** 2, sigma.dtype) - x = tf.exp(-x / (2.0 * (sigma ** 2))) - x = x / tf.math.reduce_sum(x) + x = tf.nn.softmax(-x / (2.0 * (sigma ** 2))) return x @@ -291,18 +290,16 @@ def gaussian_filter2d( sigma = tf.cast(sigma, image.dtype) gaussian_kernel_x = _get_gaussian_kernel(sigma[1], filter_shape[1]) - gaussian_kernel_x = tf.reshape(gaussian_kernel_x, [1, filter_shape[1]]) + gaussian_kernel_x = gaussian_kernel_x[tf.newaxis, :] gaussian_kernel_y = _get_gaussian_kernel(sigma[0], filter_shape[0]) - gaussian_kernel_y = tf.reshape(gaussian_kernel_y, [filter_shape[0], 1]) + gaussian_kernel_y = gaussian_kernel_y[:, tf.newaxis] gaussian_kernel_2d = _get_gaussian_kernel_2d( gaussian_kernel_y, gaussian_kernel_x ) - gaussian_kernel_2d = tf.repeat(gaussian_kernel_2d, channels) - gaussian_kernel_2d = tf.reshape( - gaussian_kernel_2d, [filter_shape[0], filter_shape[1], channels, 1] - ) + gaussian_kernel_2d = gaussian_kernel_2d[:, :, tf.newaxis, tf.newaxis] + gaussian_kernel_2d = tf.tile(gaussian_kernel_2d, [1, 1, channels, 1]) image = _pad(image, filter_shape, mode=padding, constant_values=constant_values) From 41133a320f803155784157f089f85cdb04427e26 Mon Sep 17 00:00:00 2001 From: Anurag Arnab Date: Fri, 11 Sep 2020 00:01:01 +0200 Subject: [PATCH 048/259] Correct a typo in average_optimizers_callback.ipynb (#2159) --- docs/tutorials/average_optimizers_callback.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/tutorials/average_optimizers_callback.ipynb b/docs/tutorials/average_optimizers_callback.ipynb index 1e9bae3ea4..77e6fee31d 100644 --- a/docs/tutorials/average_optimizers_callback.ipynb +++ b/docs/tutorials/average_optimizers_callback.ipynb @@ -82,7 +82,7 @@ "\n", "## Model Average Checkpoint \n", "\n", - "> `callbacks.ModelCheckpoint` doesn't give you the option to save moving average weights in the middle of traning, which is why Model Average Optimizers required a custom callback. Using the ```update_weights``` parameter, ```ModelAverageCheckpoint``` allows you to:\n", + "> `callbacks.ModelCheckpoint` doesn't give you the option to save moving average weights in the middle of training, which is why Model Average Optimizers required a custom callback. Using the ```update_weights``` parameter, ```ModelAverageCheckpoint``` allows you to:\n", "1. Assign the moving average weights to the model, and save them.\n", "2. Keep the old non-averaged weights, but the saved model uses the average weights." ] From 5bdaf40fd6c1c20a15692af7caab1a40eef92498 Mon Sep 17 00:00:00 2001 From: Kaixi Hou Date: Thu, 10 Sep 2020 17:21:48 -0700 Subject: [PATCH 049/259] Avoid unnecessary reshapes for instance norm (#2158) * Avoid unnecessary reshapes for instance norm * flake8 format changes * Fixed failed tests * flake8 format update --- tensorflow_addons/layers/normalizations.py | 34 ++++++++++++++----- .../layers/tests/normalizations_test.py | 19 ++++++++--- 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/layers/normalizations.py b/tensorflow_addons/layers/normalizations.py index 8fe2910058..8a1d6c9b3b 100644 --- a/tensorflow_addons/layers/normalizations.py +++ b/tensorflow_addons/layers/normalizations.py @@ -125,7 +125,11 @@ def call(self, inputs): normalized_inputs = self._apply_normalization(reshaped_inputs, input_shape) - outputs = tf.reshape(normalized_inputs, tensor_input_shape) + is_instance_norm = (input_shape[self.axis] // self.groups) == 1 + if not is_instance_norm: + outputs = tf.reshape(normalized_inputs, tensor_input_shape) + else: + outputs = normalized_inputs return outputs @@ -156,17 +160,25 @@ def compute_output_shape(self, input_shape): def _reshape_into_groups(self, inputs, input_shape, tensor_input_shape): group_shape = [tensor_input_shape[i] for i in range(len(input_shape))] - group_shape[self.axis] = input_shape[self.axis] // self.groups - group_shape.insert(self.axis, self.groups) - group_shape = tf.stack(group_shape) - reshaped_inputs = tf.reshape(inputs, group_shape) - return reshaped_inputs, group_shape + is_instance_norm = (input_shape[self.axis] // self.groups) == 1 + if not is_instance_norm: + group_shape[self.axis] = input_shape[self.axis] // self.groups + group_shape.insert(self.axis, self.groups) + group_shape = tf.stack(group_shape) + reshaped_inputs = tf.reshape(inputs, group_shape) + return reshaped_inputs, group_shape + else: + return inputs, group_shape def _apply_normalization(self, reshaped_inputs, input_shape): group_shape = tf.keras.backend.int_shape(reshaped_inputs) group_reduction_axes = list(range(1, len(group_shape))) - axis = -2 if self.axis == -1 else self.axis - 1 + is_instance_norm = (input_shape[self.axis] // self.groups) == 1 + if not is_instance_norm: + axis = -2 if self.axis == -1 else self.axis - 1 + else: + axis = -1 if self.axis == -1 else self.axis - 1 group_reduction_axes.pop(axis) mean, variance = tf.nn.moments( @@ -274,8 +286,12 @@ def _add_beta_weight(self, input_shape): def _create_broadcast_shape(self, input_shape): broadcast_shape = [1] * len(input_shape) - broadcast_shape[self.axis] = input_shape[self.axis] // self.groups - broadcast_shape.insert(self.axis, self.groups) + is_instance_norm = (input_shape[self.axis] // self.groups) == 1 + if not is_instance_norm: + broadcast_shape[self.axis] = input_shape[self.axis] // self.groups + broadcast_shape.insert(self.axis, self.groups) + else: + broadcast_shape[self.axis] = self.groups return broadcast_shape diff --git a/tensorflow_addons/layers/tests/normalizations_test.py b/tensorflow_addons/layers/tests/normalizations_test.py index 617dc27ab6..a15817eca8 100644 --- a/tensorflow_addons/layers/tests/normalizations_test.py +++ b/tensorflow_addons/layers/tests/normalizations_test.py @@ -80,7 +80,7 @@ def run_reshape_test(axis, group, input_shape, expected_shape): run_reshape_test(1, 2, input_shape, expected_shape) input_shape = (10, 10, 10) - expected_shape = [10, 10, 1, 10] + expected_shape = [10, 10, 10] run_reshape_test(1, -1, input_shape, expected_shape) input_shape = (10, 10, 10) @@ -122,17 +122,26 @@ def _test_specific_layer(inputs, axis, groups, center, scale): outputs = model.predict(inputs, steps=1) assert not np.isnan(outputs).any() + is_instance_norm = False # Create shapes if groups == -1: groups = input_shape[axis] + if (input_shape[axis] // groups) == 1: + is_instance_norm = True np_inputs = inputs reshaped_dims = list(np_inputs.shape) - reshaped_dims[axis] = reshaped_dims[axis] // groups - reshaped_dims.insert(axis, groups) - reshaped_inputs = np.reshape(np_inputs, tuple(reshaped_dims)) + if not is_instance_norm: + reshaped_dims[axis] = reshaped_dims[axis] // groups + reshaped_dims.insert(axis, groups) + reshaped_inputs = np.reshape(np_inputs, tuple(reshaped_dims)) + else: + reshaped_inputs = np_inputs group_reduction_axes = list(range(1, len(reshaped_dims))) - axis = -2 if axis == -1 else axis - 1 + if not is_instance_norm: + axis = -2 if axis == -1 else axis - 1 + else: + axis = -1 if axis == -1 else axis - 1 group_reduction_axes.pop(axis) # Calculate mean and variance From 7f6bcf7db6532e5bdd27b88c304e5727f767d02b Mon Sep 17 00:00:00 2001 From: Abhishek Niranjan Date: Fri, 11 Sep 2020 13:10:49 +0530 Subject: [PATCH 050/259] beam search decoding procedure added to seq2seq_nmt tutorial (#2140) * beam search decoding procedure added * removed step() function calls while inference --- docs/tutorials/networks_seq2seq_nmt.ipynb | 1448 ++++++++++++++------- 1 file changed, 960 insertions(+), 488 deletions(-) diff --git a/docs/tutorials/networks_seq2seq_nmt.ipynb b/docs/tutorials/networks_seq2seq_nmt.ipynb index ac40502943..35e38f1909 100644 --- a/docs/tutorials/networks_seq2seq_nmt.ipynb +++ b/docs/tutorials/networks_seq2seq_nmt.ipynb @@ -1,24 +1,39 @@ { + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "accelerator": "GPU", + "colab": { + "name": "8_TF-addons_BasicDecoder&BeamDecoder_Usage.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, "cells": [ { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "f9ySOjrcc0Yp" + "id": "5aElYAKlV2Mi", + "colab_type": "code", + "colab": {} }, - "outputs": [], "source": [ "##### Copyright 2020 The TensorFlow Authors." - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "cellView": "form", - "id": "bl9GdT7h0Hxk" + "id": "wmYJlt6LWVOU", + "colab_type": "code", + "colab": {} }, - "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", @@ -31,12 +46,15 @@ "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", "metadata": { - "id": "WhwgQAn50EZp" + "id": "L-8q8rRRWcp6", + "colab_type": "text" }, "source": [ "# TensorFlow Addons Networks : Sequence-to-Sequence NMT with Attention Mechanism\n", @@ -60,7 +78,8 @@ { "cell_type": "markdown", "metadata": { - "id": "ip0n8178Fuwm" + "id": "9n0dcDw1Wszw", + "colab_type": "text" }, "source": [ "## Overview\n", @@ -71,7 +90,7 @@ "* **Data cleaning**\n", "* **Data preparation**\n", "* **Neural Translation Model with Attention**\n", - "* **Final Translation**\n", + "* **Final Translation with ```tf.addons.seq2seq.BasicDecoder``` and ```tf.addons.seq2seq.BeamSearchDecoder```** \n", "\n", "The basic idea behind such a model though, is only the encoder-decoder architecture. These networks are usually used for a variety of tasks like text-summerization, Machine translation, Image Captioning, etc. This tutorial provideas a hands-on understanding of the concept, explaining the technical jargons wherever necessary. We focus on the task of Neural Machine Translation (NMT) which was the very first testbed for seq2seq models.\n" ] @@ -79,681 +98,1134 @@ { "cell_type": "markdown", "metadata": { - "id": "YNiadLKNLleD" + "id": "MpySVYWJhxaV", + "colab_type": "text" }, "source": [ "## Setup" ] }, + { + "cell_type": "code", + "metadata": { + "id": "_kxfdP4hJUPB", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 170 + }, + "outputId": "6b64ae6f-4515-462f-ec04-812233a105d3" + }, + "source": [ + "!pip install tensorflow-addons==0.11.2" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Collecting tensorflow-addons==0.11.2\n", + "\u001b[?25l Downloading https://files.pythonhosted.org/packages/b3/f8/d6fca180c123f2851035c4493690662ebdad0849a9059d56035434bff5c9/tensorflow_addons-0.11.2-cp36-cp36m-manylinux2010_x86_64.whl (1.1MB)\n", + "\u001b[K |████████████████████████████████| 1.1MB 4.4MB/s \n", + "\u001b[?25hRequirement already satisfied: typeguard>=2.7 in /usr/local/lib/python3.6/dist-packages (from tensorflow-addons==0.11.2) (2.7.1)\n", + "Installing collected packages: tensorflow-addons\n", + " Found existing installation: tensorflow-addons 0.11.0\n", + " Uninstalling tensorflow-addons-0.11.0:\n", + " Successfully uninstalled tensorflow-addons-0.11.0\n", + "Successfully installed tensorflow-addons-0.11.2\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "tnxXKDjq3jEL", + "colab": {} + }, + "source": [ + "import tensorflow as tf\n", + "import tensorflow_addons as tfa\n", + "\n", + "import matplotlib.pyplot as plt\n", + "import matplotlib.ticker as ticker\n", + "from sklearn.model_selection import train_test_split\n", + "\n", + "import unicodedata\n", + "import re\n", + "import numpy as np\n", + "import os\n", + "import io\n", + "import time\n", + "\n" + ], + "execution_count": null, + "outputs": [] + }, { "cell_type": "markdown", "metadata": { - "id": "82GcQTsGf414" + "id": "Ii_vg-XNXTil", + "colab_type": "text" }, "source": [ - "## Additional Resources:\n", + "## Data Cleaning and Data Preparation \n", + "\n", + "We'll use a language dataset provided by http://www.manythings.org/anki/. This dataset contains language translation pairs in the format:\n", "\n", - "These are a lst of resurces you must install in order to allow you to run this notebook:\n", + "---\n", + " May I borrow this book? ¿Puedo tomar prestado este libro?\n", + "---\n", "\n", "\n", - "1. [German-English Dataset](http://www.manythings.org/anki/deu-eng.zip)\n", + "There are a variety of languages available, but we'll use the English-Spanish dataset. After downloading the dataset, here are the steps we'll take to prepare the data:\n", "\n", "\n", - "The dataset should be downloaded, in order to compile this notebook, the embeddings can be used, as they are pretrained. Though, we carry out our own training here.\n" + "1. Add a start and end token to each sentence.\n", + "2. Clean the sentences by removing special characters.\n", + "3. Create a Vocabulary with word index (mapping from word → id) and reverse word index (mapping from id → word).\n", + "5. Pad each sentence to a maximum length. (Why? we need to fix the maximum length for the inputs to recurrent encoders)" ] }, { "cell_type": "code", + "metadata": { + "id": "PvRnGWnvXm6l", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def download_nmt():\n", + " path_to_zip = tf.keras.utils.get_file(\n", + " 'spa-eng.zip', origin='http://storage.googleapis.com/download.tensorflow.org/data/spa-eng.zip',\n", + " extract=True)\n", + "\n", + " path_to_file = os.path.dirname(path_to_zip)+\"/spa-eng/spa.txt\"\n", + " return path_to_file\n" + ], "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", "metadata": { - "id": "vKBON2miJ_g1" + "id": "NFKB2c_tX4wU", + "colab_type": "text" }, - "outputs": [], "source": [ - "!pip install -U tensorflow-addons\n", - "!pip install nltk sklearn" + "### Define a NMTDataset class with necessary functions to follow Step 1 to Step 4. \n", + "The ```call()``` will return:\n", + "1. ```train_dataset``` and ```val_dataset``` : ```tf.data.Dataset``` objects\n", + "2. ```inp_lang_tokenizer``` and ```targ_lang_tokenizer``` : ```tf.keras.preprocessing.text.Tokenizer``` objects " ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "5OIlpST_6ga-" + "id": "JMAHz7kJXc5N", + "colab_type": "code", + "colab": {} }, - "outputs": [], "source": [ - "#download data\n", - "print(\"Downloading Dataset:\")\n", - "!wget --quiet http://www.manythings.org/anki/deu-eng.zip\n", - "!unzip -o deu-eng.zip" - ] + "class NMTDataset:\n", + " def __init__(self, problem_type='en-spa'):\n", + " self.problem_type = 'en-spa'\n", + " self.inp_lang_tokenizer = None\n", + " self.targ_lang_tokenizer = None\n", + " \n", + "\n", + " def unicode_to_ascii(self, s):\n", + " return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')\n", + "\n", + " ## Step 1 and Step 2 \n", + " def preprocess_sentence(self, w):\n", + " w = self.unicode_to_ascii(w.lower().strip())\n", + "\n", + " # creating a space between a word and the punctuation following it\n", + " # eg: \"he is a boy.\" => \"he is a boy .\"\n", + " # Reference:- https://stackoverflow.com/questions/3645931/python-padding-punctuation-with-white-spaces-keeping-punctuation\n", + " w = re.sub(r\"([?.!,¿])\", r\" \\1 \", w)\n", + " w = re.sub(r'[\" \"]+', \" \", w)\n", + "\n", + " # replacing everything with space except (a-z, A-Z, \".\", \"?\", \"!\", \",\")\n", + " w = re.sub(r\"[^a-zA-Z?.!,¿]+\", \" \", w)\n", + "\n", + " w = w.strip()\n", + "\n", + " # adding a start and an end token to the sentence\n", + " # so that the model know when to start and stop predicting.\n", + " w = ' ' + w + ' '\n", + " return w\n", + " \n", + " def create_dataset(self, path, num_examples):\n", + " # path : path to spa-eng.txt file\n", + " # num_examples : Limit the total number of training example for faster training (set num_examples = len(lines) to use full data)\n", + " lines = io.open(path, encoding='UTF-8').read().strip().split('\\n')\n", + " word_pairs = [[self.preprocess_sentence(w) for w in l.split('\\t')] for l in lines[:num_examples]]\n", + "\n", + " return zip(*word_pairs)\n", + "\n", + " # Step 3 and Step 4\n", + " def tokenize(self, lang):\n", + " # lang = list of sentences in a language\n", + " \n", + " # print(len(lang), \"example sentence: {}\".format(lang[0]))\n", + " lang_tokenizer = tf.keras.preprocessing.text.Tokenizer(filters='', oov_token='')\n", + " lang_tokenizer.fit_on_texts(lang)\n", + "\n", + " ## tf.keras.preprocessing.text.Tokenizer.texts_to_sequences converts string (w1, w2, w3, ......, wn) \n", + " ## to a list of correspoding integer ids of words (id_w1, id_w2, id_w3, ...., id_wn)\n", + " tensor = lang_tokenizer.texts_to_sequences(lang) \n", + "\n", + " ## tf.keras.preprocessing.sequence.pad_sequences takes argument a list of integer id sequences \n", + " ## and pads the sequences to match the longest sequences in the given input\n", + " tensor = tf.keras.preprocessing.sequence.pad_sequences(tensor, padding='post')\n", + "\n", + " return tensor, lang_tokenizer\n", + "\n", + " def load_dataset(self, path, num_examples=None):\n", + " # creating cleaned input, output pairs\n", + " targ_lang, inp_lang = self.create_dataset(path, num_examples)\n", + "\n", + " input_tensor, inp_lang_tokenizer = self.tokenize(inp_lang)\n", + " target_tensor, targ_lang_tokenizer = self.tokenize(targ_lang)\n", + "\n", + " return input_tensor, target_tensor, inp_lang_tokenizer, targ_lang_tokenizer\n", + "\n", + " def call(self, num_examples, BUFFER_SIZE, BATCH_SIZE):\n", + " file_path = download_nmt()\n", + " input_tensor, target_tensor, self.inp_lang_tokenizer, self.targ_lang_tokenizer = self.load_dataset(file_path, num_examples)\n", + " \n", + " input_tensor_train, input_tensor_val, target_tensor_train, target_tensor_val = train_test_split(input_tensor, target_tensor, test_size=0.2)\n", + "\n", + " train_dataset = tf.data.Dataset.from_tensor_slices((input_tensor_train, target_tensor_train))\n", + " train_dataset = train_dataset.shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)\n", + "\n", + " val_dataset = tf.data.Dataset.from_tensor_slices((input_tensor_val, target_tensor_val))\n", + " val_dataset = val_dataset.batch(BATCH_SIZE, drop_remainder=True)\n", + "\n", + " return train_dataset, val_dataset, self.inp_lang_tokenizer, self.targ_lang_tokenizer" + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", + "metadata": { + "id": "EIW4NVBmJ25k", + "colab_type": "code", + "colab": {} + }, + "source": [ + "\n", + "BUFFER_SIZE = 32000\n", + "BATCH_SIZE = 64\n", + "# Let's limit the #training examples for faster training\n", + "num_examples = 30000\n", + "\n", + "dataset_creator = NMTDataset('en-spa')\n", + "train_dataset, val_dataset, inp_lang, targ_lang = dataset_creator.call(num_examples, BUFFER_SIZE, BATCH_SIZE)" + ], "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", "metadata": { - "id": "co6-YpBwL-4d" + "id": "w2lCTy4vKOkB", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "1781be10-0554-46ba-84a2-04b71a25217d" }, - "outputs": [], "source": [ - "import csv\n", - "import string\n", - "import re\n", - "from typing import List, Tuple\n", - "from pickle import dump\n", - "from unicodedata import normalize\n", - "import numpy as np\n", - "import itertools\n", - "from pickle import load\n", - "from tensorflow.keras.utils import to_categorical\n", - "from tensorflow.keras.utils import plot_model\n", - "from tensorflow.keras.models import Sequential\n", - "from tensorflow.keras.layers import LSTM\n", - "from tensorflow.keras.layers import Dense\n", - "from tensorflow.keras.layers import Embedding\n", - "from pickle import load\n", - "import random\n", - "import tensorflow as tf\n", - "from tensorflow.keras.models import load_model\n", - "from nltk.translate.bleu_score import corpus_bleu\n", - "from sklearn.model_selection import train_test_split\n", - "import tensorflow_addons as tfa" + "example_input_batch, example_target_batch = next(iter(train_dataset))\n", + "example_input_batch.shape, example_target_batch.shape" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(TensorShape([64, 16]), TensorShape([64, 11]))" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 7 + } ] }, { "cell_type": "markdown", "metadata": { - "id": "q7gjUT_9XSoj" + "colab_type": "text", + "id": "rgCLkfv5uO3d" }, "source": [ - "## Data Cleaning\n", - "\n", - "Our data set is a German-English translation dataset. It contains 152,820 pairs of English to German phases, one pair per line with a tab separating the language. These dataset though organized needs cleaning before we can work on it. This will enable us to remove unnecessary bumps that may come in during the training. We also added start-of-sentence `` and end-of-sentence `` so that the model knows when to start and stop predicting." + "### Some important parameters" ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "6ZIu-TNqKFsd" - }, - "outputs": [], - "source": [ - "# Start of sentence\n", - "SOS = \"\"\n", - "# End of sentence\n", - "EOS = \"\"\n", - "# Relevant punctuation\n", - "PUNCTUATION = set(\"?,!.\")\n", - "\n", - "\n", - "def load_dataset(filename: str) -> str:\n", - " \"\"\"\n", - " load dataset into memory\n", - " \"\"\"\n", - " with open(filename, mode=\"rt\", encoding=\"utf-8\") as fp:\n", - " return fp.read()\n", - "\n", - "\n", - "def to_pairs(dataset: str, limit: int = None, shuffle=False) -> List[Tuple[str, str]]:\n", - " \"\"\"\n", - " Split dataset into pairs of sentences, discards dataset line info.\n", - "\n", - " e.g.\n", - " input -> 'Go.\\tGeh.\\tCC-BY 2.0 (France) Attribution: tatoeba.org\n", - " #2877272 (CM) & #8597805 (Roujin)'\n", - " output -> [('Go.', 'Geh.')]\n", - "\n", - " :param dataset: dataset containing examples of translations between\n", - " two languages\n", - " the examples are delimited by `\\n` and the contents of the lines are\n", - " delimited by `\\t`\n", - " :param limit: number that limit dataset size (optional)\n", - " :param shuffle: default is True\n", - " :return: list of pairs\n", - " \"\"\"\n", - " assert isinstance(limit, (int, type(None))), TypeError(\n", - " \"the limit value must be an integer\"\n", - " )\n", - " lines = dataset.strip().split(\"\\n\")\n", - " # Radom dataset\n", - " if shuffle is True:\n", - " random.shuffle(lines)\n", - " number_examples = limit or len(lines) # if None get all\n", - " pairs = []\n", - " for line in lines[: abs(number_examples)]:\n", - " # take only source and target\n", - " src, trg, _ = line.split(\"\\t\")\n", - " pairs.append((src, trg))\n", - "\n", - " # dataset size check\n", - " assert len(pairs) == number_examples\n", - " return pairs\n", - "\n", - "\n", - "def separe_punctuation(token: str) -> str:\n", - " \"\"\"\n", - " Separe punctuation if exists\n", - " \"\"\"\n", - "\n", - " if not set(token).intersection(PUNCTUATION):\n", - " return token\n", - " for p in PUNCTUATION:\n", - " token = f\" {p} \".join(token.split(p))\n", - " return \" \".join(token.split())\n", - "\n", - "\n", - "def preprocess(sentence: str, add_start_end: bool=True) -> str:\n", - " \"\"\"\n", - " - convert lowercase\n", - " - remove numbers\n", - " - remove special characters\n", - " - separe punctuation\n", - " - add start-of-sentence and end-of-sentence \n", - "\n", - " :param add_start_end: add SOS (start-of-sentence) and EOS (end-of-sentence)\n", - " \"\"\"\n", - " re_print = re.compile(f\"[^{re.escape(string.printable)}]\")\n", - " # convert lowercase and normalizing unicode characters\n", - " sentence = (\n", - " normalize(\"NFD\", sentence.lower()).encode(\"ascii\", \"ignore\").decode(\"UTF-8\")\n", - " )\n", - " cleaned_tokens = []\n", - " # tokenize sentence on white space\n", - " for token in sentence.split():\n", - " # removing non-printable chars form each token\n", - " token = re_print.sub(\"\", token).strip()\n", - " # ignore tokens with numbers\n", - " if re.findall(\"[0-9]\", token):\n", - " continue\n", - " # add space between words and punctuation eg: \"ok?go!\" => \"ok ? go !\"\n", - " token = separe_punctuation(token)\n", - " cleaned_tokens.append(token)\n", - "\n", - " # rebuild sentence with space between tokens\n", - " sentence = \" \".join(cleaned_tokens)\n", - "\n", - " # adding a start and an end token to the sentence\n", - " if add_start_end is True:\n", - " sentence = f\"{SOS} {sentence} {EOS}\"\n", - " return sentence\n", - "\n", - "\n", - "def dataset_preprocess(dataset: List[Tuple[str, str]]) -> Tuple[List[str], List[str]]:\n", - " \"\"\"\n", - " Returns processed database\n", - "\n", - " :param dataset: list of sentence pairs\n", - " :return: list of paralel data e.g. \n", - " (['first source sentence', 'second', ...], ['first target sentence', 'second', ...])\n", - " \"\"\"\n", - " source_cleaned = []\n", - " target_cleaned = []\n", - " for source, target in dataset:\n", - " source_cleaned.append(preprocess(source))\n", - " target_cleaned.append(preprocess(target))\n", - " return source_cleaned, target_cleaned\n" - ] + "colab_type": "code", + "id": "TqHsArVZ3jFS", + "colab": {} + }, + "source": [ + "vocab_inp_size = len(inp_lang.word_index)+1\n", + "vocab_tar_size = len(targ_lang.word_index)+1\n", + "max_length_input = example_input_batch.shape[1]\n", + "max_length_output = example_target_batch.shape[1]\n", + "\n", + "embedding_dim = 256\n", + "units = 1024\n", + "steps_per_epoch = num_examples//BATCH_SIZE\n" + ], + "execution_count": null, + "outputs": [] }, { - "cell_type": "markdown", + "cell_type": "code", "metadata": { - "id": "5nDIELt9RH-w" + "id": "g-yY9c6aIu1h", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "2302f86e-df0c-4fba-b1b7-0338a88f4a14" }, "source": [ - "## Create Dataset\n", - "\n", - "- limit number of examples\n", - "- load dataset into pairs `[('Be nice.', 'Seien Sie nett!'), ('Beat it.', 'Geh weg!'), ...]`\n", - "- preprocessing dataset" + "print(\"max_length_spanish, max_length_english, vocab_size_spanish, vocab_size_english\")\n", + "max_length_input, max_length_output, vocab_inp_size, vocab_tar_size" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "max_length_spanish, max_length_english, vocab_size_spanish, vocab_size_english\n" + ], + "name": "stdout" + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(16, 11, 9415, 4936)" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 9 + } ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "GMxdlVU1X8yI" + "colab_type": "code", + "id": "nZ2rI24i3jFg", + "colab": {} }, - "outputs": [], "source": [ - "NUM_EXAMPLES = 10000 # Limit dataset size\n", + "##### \n", "\n", - "# load from .txt\n", - "filename = 'deu.txt' #change filename if necessary\n", - "dataset = load_dataset(filename)\n", - "# get pairs limited into 1000\n", - "pairs = to_pairs(dataset, limit=NUM_EXAMPLES)\n", - "print(f\"Dataset size: {len(pairs)}\")\n", - "raw_data_en, raw_data_ge = dataset_preprocess(pairs)\n", + "class Encoder(tf.keras.Model):\n", + " def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz):\n", + " super(Encoder, self).__init__()\n", + " self.batch_sz = batch_sz\n", + " self.enc_units = enc_units\n", + " self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)\n", "\n", - "# show last 5 pairs\n", - "for pair in zip(raw_data_en[-5:],raw_data_ge[-5:]):\n", - " print(pair)" - ] + " ##-------- LSTM layer in Encoder ------- ##\n", + " self.lstm_layer = tf.keras.layers.LSTM(self.enc_units,\n", + " return_sequences=True,\n", + " return_state=True,\n", + " recurrent_initializer='glorot_uniform')\n", + " \n", + "\n", + "\n", + " def call(self, x, hidden):\n", + " x = self.embedding(x)\n", + " output, h, c = self.lstm_layer(x, initial_state = hidden)\n", + " return output, h, c\n", + "\n", + " def initialize_hidden_state(self):\n", + " return [tf.zeros((self.batch_sz, self.enc_units)), tf.zeros((self.batch_sz, self.enc_units))] " + ], + "execution_count": null, + "outputs": [] }, { - "cell_type": "markdown", + "cell_type": "code", "metadata": { - "id": "Cfb66QxWYr6A" + "colab_type": "code", + "id": "60gSVh05Jl6l", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "dd7acce3-8dbd-4eb8-d839-645b0c063d8d" }, "source": [ - "## Tokenization" + "## Test Encoder Stack\n", + "\n", + "encoder = Encoder(vocab_inp_size, embedding_dim, units, BATCH_SIZE)\n", + "\n", + "\n", + "# sample input\n", + "sample_hidden = encoder.initialize_hidden_state()\n", + "sample_output, sample_h, sample_c = encoder(example_input_batch, sample_hidden)\n", + "print ('Encoder output shape: (batch size, sequence length, units) {}'.format(sample_output.shape))\n", + "print ('Encoder h vecotr shape: (batch size, units) {}'.format(sample_h.shape))\n", + "print ('Encoder c vector shape: (batch size, units) {}'.format(sample_c.shape))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Encoder output shape: (batch size, sequence length, units) (64, 16, 1024)\n", + "Encoder h vecotr shape: (batch size, units) (64, 1024)\n", + "Encoder c vector shape: (batch size, units) (64, 1024)\n" + ], + "name": "stdout" + } ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "3oq60MBPSanQ" + "colab_type": "code", + "id": "yJ_B3mhW3jFk", + "colab": {} }, - "outputs": [], "source": [ - "en_tokenizer = tf.keras.preprocessing.text.Tokenizer(filters='')\n", - "en_tokenizer.fit_on_texts(raw_data_en)\n", + "class Decoder(tf.keras.Model):\n", + " def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz, attention_type='luong'):\n", + " super(Decoder, self).__init__()\n", + " self.batch_sz = batch_sz\n", + " self.dec_units = dec_units\n", + " self.attention_type = attention_type\n", + " \n", + " # Embedding Layer\n", + " self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)\n", + " \n", + " #Final Dense layer on which softmax will be applied\n", + " self.fc = tf.keras.layers.Dense(vocab_size)\n", "\n", - "data_en = en_tokenizer.texts_to_sequences(raw_data_en)\n", - "data_en = tf.keras.preprocessing.sequence.pad_sequences(data_en,padding='post')\n", + " # Define the fundamental cell for decoder recurrent structure\n", + " self.decoder_rnn_cell = tf.keras.layers.LSTMCell(self.dec_units)\n", + " \n", "\n", - "ge_tokenizer = tf.keras.preprocessing.text.Tokenizer(filters='')\n", - "ge_tokenizer.fit_on_texts(raw_data_ge)\n", "\n", - "data_ge = ge_tokenizer.texts_to_sequences(raw_data_ge)\n", - "data_ge = tf.keras.preprocessing.sequence.pad_sequences(data_ge,padding='post')" - ] + " # Sampler\n", + " self.sampler = tfa.seq2seq.sampler.TrainingSampler()\n", + "\n", + " # Create attention mechanism with memory = None\n", + " self.attention_mechanism = self.build_attention_mechanism(self.dec_units, \n", + " None, self.batch_sz*[max_length_input], self.attention_type)\n", + "\n", + " # Wrap attention mechanism with the fundamental rnn cell of decoder\n", + " self.rnn_cell = self.build_rnn_cell(batch_sz)\n", + "\n", + " # Define the decoder with respect to fundamental rnn cell\n", + " self.decoder = tfa.seq2seq.BasicDecoder(self.rnn_cell, sampler=self.sampler, output_layer=self.fc)\n", + "\n", + " \n", + " def build_rnn_cell(self, batch_sz):\n", + " rnn_cell = tfa.seq2seq.AttentionWrapper(self.decoder_rnn_cell, \n", + " self.attention_mechanism, attention_layer_size=self.dec_units)\n", + " return rnn_cell\n", + "\n", + " def build_attention_mechanism(self, dec_units, memory, memory_sequence_length, attention_type='luong'):\n", + " # ------------- #\n", + " # typ: Which sort of attention (Bahdanau, Luong)\n", + " # dec_units: final dimension of attention outputs \n", + " # memory: encoder hidden states of shape (batch_size, max_length_input, enc_units)\n", + " # memory_sequence_length: 1d array of shape (batch_size) with every element set to max_length_input (for masking purpose)\n", + "\n", + " if(attention_type=='bahdanau'):\n", + " return tfa.seq2seq.BahdanauAttention(units=dec_units, memory=memory, memory_sequence_length=memory_sequence_length)\n", + " else:\n", + " return tfa.seq2seq.LuongAttention(units=dec_units, memory=memory, memory_sequence_length=memory_sequence_length)\n", + "\n", + " def build_initial_state(self, batch_sz, encoder_state, Dtype):\n", + " decoder_initial_state = self.rnn_cell.get_initial_state(batch_size=batch_sz, dtype=Dtype)\n", + " decoder_initial_state = decoder_initial_state.clone(cell_state=encoder_state)\n", + " return decoder_initial_state\n", + "\n", + "\n", + " def call(self, inputs, initial_state):\n", + " x = self.embedding(inputs)\n", + " outputs, _, _ = self.decoder(x, initial_state=initial_state, sequence_length=self.batch_sz*[max_length_output-1])\n", + " return outputs\n", + "\n" + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "XH5oSRNeSc1s" + "id": "DaiO0Z6_Ml1c", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "069bf53d-5121-4f82-9fd9-76c62d3223ec" }, - "outputs": [], "source": [ - "def max_len(tensor):\n", - " #print( np.argmax([len(t) for t in tensor]))\n", - " return max( len(t) for t in tensor)" + "# Test decoder stack\n", + "\n", + "decoder = Decoder(vocab_tar_size, embedding_dim, units, BATCH_SIZE, 'luong')\n", + "sample_x = tf.random.uniform((BATCH_SIZE, max_length_output))\n", + "decoder.attention_mechanism.setup_memory(sample_output)\n", + "initial_state = decoder.build_initial_state(BATCH_SIZE, [sample_h, sample_c], tf.float32)\n", + "\n", + "\n", + "sample_decoder_outputs = decoder(sample_x, initial_state)\n", + "\n", + "print(\"Decoder Outputs Shape: \", sample_decoder_outputs.rnn_output.shape)\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Decoder Outputs Shape: (64, 10, 4936)\n" + ], + "name": "stdout" + } ] }, { "cell_type": "markdown", "metadata": { - "id": "KdM37lNBGXAj" + "colab_type": "text", + "id": "_ch_71VbIRfK" }, "source": [ - "## Model Parameters" + "## Define the optimizer and the loss function" ] }, { "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "WmTHr5iV3jFr", + "colab": {} + }, + "source": [ + "optimizer = tf.keras.optimizers.Adam()\n", + "\n", + "\n", + "def loss_function(real, pred):\n", + " # real shape = (BATCH_SIZE, max_length_output)\n", + " # pred shape = (BATCH_SIZE, max_length_output, tar_vocab_size )\n", + " cross_entropy = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True, reduction='none')\n", + " loss = cross_entropy(y_true=real, y_pred=pred)\n", + " mask = tf.logical_not(tf.math.equal(real,0)) #output 0 for y=0 else output 1\n", + " mask = tf.cast(mask, dtype=loss.dtype) \n", + " loss = mask* loss\n", + " loss = tf.reduce_mean(loss)\n", + " return loss " + ], "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", "metadata": { - "id": "EfiBUJM2Et6C" + "colab_type": "text", + "id": "DMVWzzsfNl4e" }, - "outputs": [], "source": [ - "X_train, X_test, Y_train, Y_test = train_test_split(data_en,data_ge,test_size=0.2)\n", - "BATCH_SIZE = 64\n", - "BUFFER_SIZE = len(X_train)\n", - "steps_per_epoch = BUFFER_SIZE//BATCH_SIZE\n", - "embedding_dims = 256\n", - "rnn_units = 1024\n", - "dense_units = 1024\n", - "Dtype = tf.float32 #used to initialize DecoderCell Zero state" + "## Checkpoints (Object-based saving)" ] }, + { + "cell_type": "code", + "metadata": { + "colab_type": "code", + "id": "Zj8bXQTgNwrF", + "colab": {} + }, + "source": [ + "checkpoint_dir = './training_checkpoints'\n", + "checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt\")\n", + "checkpoint = tf.train.Checkpoint(optimizer=optimizer,\n", + " encoder=encoder,\n", + " decoder=decoder)" + ], + "execution_count": null, + "outputs": [] + }, { "cell_type": "markdown", "metadata": { - "id": "Ff_jQHLhGqJU" + "id": "8Bw95utNiFHa", + "colab_type": "text" }, "source": [ - "## Dataset Prepration" + "## One train_step operations" ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "b__1hPHVFALO" + "colab_type": "code", + "id": "sC9ArXSsVfqn", + "colab": {} }, - "outputs": [], "source": [ - "Tx = max_len(data_en)\n", - "Ty = max_len(data_ge) \n", + "@tf.function\n", + "def train_step(inp, targ, enc_hidden):\n", + " loss = 0\n", "\n", - "input_vocab_size = len(en_tokenizer.word_index)+1 \n", - "output_vocab_size = len(ge_tokenizer.word_index)+ 1\n", - "dataset = tf.data.Dataset.from_tensor_slices((X_train, Y_train)).shuffle(BUFFER_SIZE).batch(BATCH_SIZE, drop_remainder=True)\n", - "example_X, example_Y = next(iter(dataset))\n", - "print(example_X.shape) \n", - "print(example_Y.shape) " - ] + " with tf.GradientTape() as tape:\n", + " enc_output, enc_h, enc_c = encoder(inp, enc_hidden)\n", + "\n", + "\n", + " dec_input = targ[ : , :-1 ] # Ignore token\n", + " real = targ[ : , 1: ] # ignore token\n", + "\n", + " # Set the AttentionMechanism object with encoder_outputs\n", + " decoder.attention_mechanism.setup_memory(enc_output)\n", + "\n", + " # Create AttentionWrapperState as initial_state for decoder\n", + " decoder_initial_state = decoder.build_initial_state(BATCH_SIZE, [enc_h, enc_c], tf.float32)\n", + " pred = decoder(dec_input, decoder_initial_state)\n", + " logits = pred.rnn_output\n", + " loss = loss_function(real, logits)\n", + "\n", + " variables = encoder.trainable_variables + decoder.trainable_variables\n", + " gradients = tape.gradient(loss, variables)\n", + " optimizer.apply_gradients(zip(gradients, variables))\n", + "\n", + " return loss" + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", "metadata": { - "id": "UQRgJcYgapqE" + "id": "pey8eb9piMMg", + "colab_type": "text" }, "source": [ - "## Defining NMT Model" + "## Train the model" ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "sGdakRtjaokF" + "colab_type": "code", + "id": "ddefjBMa3jF0", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + }, + "outputId": "2b43d0b9-6e1f-4592-92ce-41d86bdd5514" }, - "outputs": [], "source": [ - "#ENCODER\n", - "class EncoderNetwork(tf.keras.Model):\n", - " def __init__(self,input_vocab_size,embedding_dims, rnn_units ):\n", - " super().__init__()\n", - " self.encoder_embedding = tf.keras.layers.Embedding(input_dim=input_vocab_size,\n", - " output_dim=embedding_dims)\n", - " self.encoder_rnnlayer = tf.keras.layers.LSTM(rnn_units,return_sequences=True, \n", - " return_state=True )\n", - " \n", - "#DECODER\n", - "class DecoderNetwork(tf.keras.Model):\n", - " def __init__(self,output_vocab_size, embedding_dims, rnn_units):\n", - " super().__init__()\n", - " self.decoder_embedding = tf.keras.layers.Embedding(input_dim=output_vocab_size,\n", - " output_dim=embedding_dims) \n", - " self.dense_layer = tf.keras.layers.Dense(output_vocab_size)\n", - " self.decoder_rnncell = tf.keras.layers.LSTMCell(rnn_units)\n", - " # Sampler\n", - " self.sampler = tfa.seq2seq.sampler.TrainingSampler()\n", - " # Create attention mechanism with memory = None\n", - " self.attention_mechanism = self.build_attention_mechanism(dense_units,None,BATCH_SIZE*[Tx])\n", - " self.rnn_cell = self.build_rnn_cell(BATCH_SIZE)\n", - " self.decoder = tfa.seq2seq.BasicDecoder(self.rnn_cell, sampler= self.sampler,\n", - " output_layer=self.dense_layer)\n", - "\n", - " def build_attention_mechanism(self, units,memory, memory_sequence_length):\n", - " return tfa.seq2seq.LuongAttention(units, memory = memory, \n", - " memory_sequence_length=memory_sequence_length)\n", - " #return tfa.seq2seq.BahdanauAttention(units, memory = memory, memory_sequence_length=memory_sequence_length)\n", - "\n", - " # wrap decodernn cell \n", - " def build_rnn_cell(self, batch_size ):\n", - " rnn_cell = tfa.seq2seq.AttentionWrapper(self.decoder_rnncell, self.attention_mechanism,\n", - " attention_layer_size=dense_units)\n", - " return rnn_cell\n", - " \n", - " def build_decoder_initial_state(self, batch_size, encoder_state,Dtype):\n", - " decoder_initial_state = self.rnn_cell.get_initial_state(batch_size = batch_size, \n", - " dtype = Dtype)\n", - " decoder_initial_state = decoder_initial_state.clone(cell_state=encoder_state) \n", - " return decoder_initial_state\n", - "\n", - "encoderNetwork = EncoderNetwork(input_vocab_size,embedding_dims, rnn_units)\n", - "decoderNetwork = DecoderNetwork(output_vocab_size,embedding_dims, rnn_units)\n", - "optimizer = tf.keras.optimizers.Adam()\n" + "EPOCHS = 10\n", + "\n", + "for epoch in range(EPOCHS):\n", + " start = time.time()\n", + "\n", + " enc_hidden = encoder.initialize_hidden_state()\n", + " total_loss = 0\n", + " # print(enc_hidden[0].shape, enc_hidden[1].shape)\n", + "\n", + " for (batch, (inp, targ)) in enumerate(train_dataset.take(steps_per_epoch)):\n", + " batch_loss = train_step(inp, targ, enc_hidden)\n", + " total_loss += batch_loss\n", + "\n", + " if batch % 100 == 0:\n", + " print('Epoch {} Batch {} Loss {:.4f}'.format(epoch + 1,\n", + " batch,\n", + " batch_loss.numpy()))\n", + " # saving (checkpoint) the model every 2 epochs\n", + " if (epoch + 1) % 2 == 0:\n", + " checkpoint.save(file_prefix = checkpoint_prefix)\n", + "\n", + " print('Epoch {} Loss {:.4f}'.format(epoch + 1,\n", + " total_loss / steps_per_epoch))\n", + " print('Time taken for 1 epoch {} sec\\n'.format(time.time() - start))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Epoch 1 Batch 0 Loss 5.1692\n", + "Epoch 1 Batch 100 Loss 2.2288\n", + "Epoch 1 Batch 200 Loss 1.9930\n", + "Epoch 1 Batch 300 Loss 1.7783\n", + "Epoch 1 Loss 1.6975\n", + "Time taken for 1 epoch 37.26002788543701 sec\n", + "\n", + "Epoch 2 Batch 0 Loss 1.6408\n", + "Epoch 2 Batch 100 Loss 1.5767\n", + "Epoch 2 Batch 200 Loss 1.4054\n", + "Epoch 2 Batch 300 Loss 1.3755\n", + "Epoch 2 Loss 1.1412\n", + "Time taken for 1 epoch 30.0094051361084 sec\n", + "\n", + "Epoch 3 Batch 0 Loss 1.0296\n", + "Epoch 3 Batch 100 Loss 1.0306\n", + "Epoch 3 Batch 200 Loss 1.0675\n", + "Epoch 3 Batch 300 Loss 0.9574\n", + "Epoch 3 Loss 0.8037\n", + "Time taken for 1 epoch 28.983767986297607 sec\n", + "\n", + "Epoch 4 Batch 0 Loss 0.5923\n", + "Epoch 4 Batch 100 Loss 0.7533\n", + "Epoch 4 Batch 200 Loss 0.7397\n", + "Epoch 4 Batch 300 Loss 0.6779\n", + "Epoch 4 Loss 0.5419\n", + "Time taken for 1 epoch 29.649972200393677 sec\n", + "\n", + "Epoch 5 Batch 0 Loss 0.4320\n", + "Epoch 5 Batch 100 Loss 0.4349\n", + "Epoch 5 Batch 200 Loss 0.4686\n", + "Epoch 5 Batch 300 Loss 0.4748\n", + "Epoch 5 Loss 0.3827\n", + "Time taken for 1 epoch 29.06334638595581 sec\n", + "\n", + "Epoch 6 Batch 0 Loss 0.3422\n", + "Epoch 6 Batch 100 Loss 0.3052\n", + "Epoch 6 Batch 200 Loss 0.3288\n", + "Epoch 6 Batch 300 Loss 0.3216\n", + "Epoch 6 Loss 0.2814\n", + "Time taken for 1 epoch 29.57170796394348 sec\n", + "\n", + "Epoch 7 Batch 0 Loss 0.2129\n", + "Epoch 7 Batch 100 Loss 0.2382\n", + "Epoch 7 Batch 200 Loss 0.2406\n", + "Epoch 7 Batch 300 Loss 0.2792\n", + "Epoch 7 Loss 0.2162\n", + "Time taken for 1 epoch 28.95500087738037 sec\n", + "\n", + "Epoch 8 Batch 0 Loss 0.2073\n", + "Epoch 8 Batch 100 Loss 0.2095\n", + "Epoch 8 Batch 200 Loss 0.1962\n", + "Epoch 8 Batch 300 Loss 0.1879\n", + "Epoch 8 Loss 0.1794\n", + "Time taken for 1 epoch 29.70877432823181 sec\n", + "\n", + "Epoch 9 Batch 0 Loss 0.1517\n", + "Epoch 9 Batch 100 Loss 0.2231\n", + "Epoch 9 Batch 200 Loss 0.2203\n", + "Epoch 9 Batch 300 Loss 0.2282\n", + "Epoch 9 Loss 0.1496\n", + "Time taken for 1 epoch 29.20821261405945 sec\n", + "\n", + "Epoch 10 Batch 0 Loss 0.1204\n", + "Epoch 10 Batch 100 Loss 0.1370\n", + "Epoch 10 Batch 200 Loss 0.1778\n", + "Epoch 10 Batch 300 Loss 0.2069\n", + "Epoch 10 Loss 0.1316\n", + "Time taken for 1 epoch 29.576894283294678 sec\n", + "\n" + ], + "name": "stdout" + } ] }, { "cell_type": "markdown", "metadata": { - "id": "NPwcfddTa0oB" + "colab_type": "text", + "id": "mU3Ce8M6I3rz" }, "source": [ - "## Initializing Training functions" + "## Use tf-addons BasicDecoder for decoding\n" ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "x1BEqVyra2jW" + "colab_type": "code", + "id": "EbQpyYs13jF_", + "colab": {} }, - "outputs": [], "source": [ - "def loss_function(y_pred, y):\n", - " \n", - " #shape of y [batch_size, ty]\n", - " #shape of y_pred [batch_size, Ty, output_vocab_size] \n", - " sparsecategoricalcrossentropy = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True,\n", - " reduction='none')\n", - " loss = sparsecategoricalcrossentropy(y_true=y, y_pred=y_pred)\n", - " mask = tf.logical_not(tf.math.equal(y,0)) #output 0 for y=0 else output 1\n", - " mask = tf.cast(mask, dtype=loss.dtype)\n", - " loss = mask* loss\n", - " loss = tf.reduce_mean(loss)\n", - " return loss\n", - "\n", - "def train_step(input_batch, output_batch,encoder_initial_cell_state):\n", - " #initialize loss = 0\n", - " loss = 0\n", - " with tf.GradientTape() as tape:\n", - " encoder_emb_inp = encoderNetwork.encoder_embedding(input_batch)\n", - " a, a_tx, c_tx = encoderNetwork.encoder_rnnlayer(encoder_emb_inp, \n", - " initial_state =encoder_initial_cell_state)\n", - "\n", - " #[last step activations,last memory_state] of encoder passed as input to decoder Network\n", - " \n", - " \n", - " # Prepare correct Decoder input & output sequence data\n", - " decoder_input = output_batch[:,:-1] # ignore \n", - " #compare logits with timestepped +1 version of decoder_input\n", - " decoder_output = output_batch[:,1:] #ignore \n", + "def evaluate_sentence(sentence):\n", + " sentence = dataset_creator.preprocess_sentence(sentence)\n", "\n", + " inputs = [inp_lang.word_index[i] for i in sentence.split(' ')]\n", + " inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs],\n", + " maxlen=max_length_input,\n", + " padding='post')\n", + " inputs = tf.convert_to_tensor(inputs)\n", + " inference_batch_size = inputs.shape[0]\n", + " result = ''\n", "\n", - " # Decoder Embeddings\n", - " decoder_emb_inp = decoderNetwork.decoder_embedding(decoder_input)\n", + " enc_start_state = [tf.zeros((inference_batch_size, units)), tf.zeros((inference_batch_size,units))]\n", + " enc_out, enc_h, enc_c = encoder(inputs, enc_start_state)\n", "\n", - " #Setting up decoder memory from encoder output and Zero State for AttentionWrapperState\n", - " decoderNetwork.attention_mechanism.setup_memory(a)\n", - " decoder_initial_state = decoderNetwork.build_decoder_initial_state(BATCH_SIZE,\n", - " encoder_state=[a_tx, c_tx],\n", - " Dtype=tf.float32)\n", - " \n", - " #BasicDecoderOutput \n", - " outputs, _, _ = decoderNetwork.decoder(decoder_emb_inp,initial_state=decoder_initial_state,\n", - " sequence_length=BATCH_SIZE*[Ty-1])\n", + " dec_h = enc_h\n", + " dec_c = enc_c\n", + "\n", + " start_tokens = tf.fill([inference_batch_size], targ_lang.word_index[''])\n", + " end_token = targ_lang.word_index['']\n", + "\n", + " greedy_sampler = tfa.seq2seq.GreedyEmbeddingSampler()\n", + "\n", + " # Instantiate BasicDecoder object\n", + " decoder_instance = tfa.seq2seq.BasicDecoder(cell=decoder.rnn_cell, sampler=greedy_sampler, output_layer=decoder.fc)\n", + " # Setup Memory in decoder stack\n", + " decoder.attention_mechanism.setup_memory(enc_out)\n", "\n", - " logits = outputs.rnn_output\n", - " #Calculate loss\n", + " # set decoder_initial_state\n", + " decoder_initial_state = decoder.build_initial_state(inference_batch_size, [enc_h, enc_c], tf.float32)\n", "\n", - " loss = loss_function(logits, decoder_output)\n", "\n", - " #Returns the list of all layer variables / weights.\n", - " variables = encoderNetwork.trainable_variables + decoderNetwork.trainable_variables \n", - " # differentiate loss wrt variables\n", - " gradients = tape.gradient(loss, variables)\n", + " ### Since the BasicDecoder wraps around Decoder's rnn cell only, we have to ensure that the inputs to BasicDecoder \n", + " ### decoding step is output of embedding layer. tfa.seq2seq.GreedyEmbeddingSampler() takes care of this. \n", + " ### We only need to get the weights of embedding layer, which can be done by decoder.embedding.variables[0] and pass this callabble to BasicDecoder's call() function\n", "\n", - " #grads_and_vars – List of(gradient, variable) pairs.\n", - " grads_and_vars = zip(gradients,variables)\n", - " optimizer.apply_gradients(grads_and_vars)\n", - " return loss" + " decoder_embedding_matrix = decoder.embedding.variables[0]\n", + " \n", + " outputs, _, _ = decoder_instance(decoder_embedding_matrix, start_tokens = start_tokens, end_token= end_token, initial_state=decoder_initial_state)\n", + " return outputs.sample_id.numpy()\n", + "\n", + "def translate(sentence):\n", + " result = evaluate_sentence(sentence)\n", + " print(result)\n", + " result = targ_lang.sequences_to_texts(result)\n", + " print('Input: %s' % (sentence))\n", + " print('Predicted translation: {}'.format(result))" + ], + "execution_count": 98, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "colab_type": "text", + "id": "n250XbnjOaqP" + }, + "source": [ + "## Restore the latest checkpoint and test" ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "71Lkdx6GFb3A" + "colab_type": "code", + "id": "UJpT9D5_OgP6", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "26ffd441-4e4f-41c6-afdc-0be8ffd07d20" }, - "outputs": [], "source": [ - "#RNN LSTM hidden and memory state initializer\n", - "def initialize_initial_state():\n", - " return [tf.zeros((BATCH_SIZE, rnn_units)), tf.zeros((BATCH_SIZE, rnn_units))]" + "# restoring the latest checkpoint in checkpoint_dir\n", + "checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 20 + } ] }, { - "cell_type": "markdown", + "cell_type": "code", "metadata": { - "id": "v5uzLcu2bNX3" + "id": "WYmYhNN_faR5", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "c3487c60-dff9-49d3-ae9d-5fa6d9f97899" }, "source": [ - "## Training" + "translate(u'hace mucho frio aqui.')" + ], + "execution_count": 99, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[ 11 12 49 224 40 4 3]]\n", + "Input: hace mucho frio aqui.\n", + "Predicted translation: ['it s very pretty here . ']\n" + ], + "name": "stdout" + } ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "PvfD2SknWrt6" + "colab_type": "code", + "id": "zSx2iM36EZQZ", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "bdca33b5-a395-4335-9dd5-cf0ce5065571" }, - "outputs": [], "source": [ - "epochs = 15\n", - "for i in range(1, epochs+1):\n", - "\n", - " encoder_initial_cell_state = initialize_initial_state()\n", - " total_loss = 0.0\n", - "\n", - " for ( batch , (input_batch, output_batch)) in enumerate(dataset.take(steps_per_epoch)):\n", - " batch_loss = train_step(input_batch, output_batch, encoder_initial_cell_state)\n", - " total_loss += batch_loss\n", - " if (batch+1)%5 == 0:\n", - " print(\"total loss: {} epoch {} batch {} \".format(batch_loss.numpy(), i, batch+1))" + "translate(u'esta es mi vida.')" + ], + "execution_count": 100, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[ 20 9 22 190 4 3]]\n", + "Input: esta es mi vida.\n", + "Predicted translation: ['this is my life . ']\n" + ], + "name": "stdout" + } ] }, { - "cell_type": "markdown", + "cell_type": "code", "metadata": { - "id": "nDyK-EGqbN5r" + "colab_type": "code", + "id": "A3LLCx3ZE0Ls", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "70c42c2e-dfb1-4040-bad7-3a35ae673880" }, "source": [ - "## Evaluation" + "translate(u'¿todavia estan en casa?')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[25 7 90 8 3]]\n", + "Input: ¿todavia estan en casa?\n", + "Predicted translation: ['are you home ? ']\n" + ], + "name": "stdout" + } ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "y98sfom7SuGy" - }, - "outputs": [], - "source": [ - "#In this section we evaluate our model on a raw_input converted to german, for this the entire sentence has to be passed\n", - "#through the length of the model, for this we use greedsampler to run through the decoder\n", - "#and the final embedding matrix trained on the data is used to generate embeddings\n", - "input_raw='how are you'\n", - "\n", - "# We have a transcript file containing English-German pairs\n", - "# Preprocess X\n", - "input_raw = preprocess(input_raw, add_start_end=False)\n", - "input_lines = [f'{SOS} {input_raw}']\n", - "input_sequences = [[en_tokenizer.word_index[w] for w in line.split()] for line in input_lines]\n", - "input_sequences = tf.keras.preprocessing.sequence.pad_sequences(input_sequences,\n", - " maxlen=Tx, padding='post')\n", - "inp = tf.convert_to_tensor(input_sequences)\n", - "#print(inp.shape)\n", - "inference_batch_size = input_sequences.shape[0]\n", - "encoder_initial_cell_state = [tf.zeros((inference_batch_size, rnn_units)),\n", - " tf.zeros((inference_batch_size, rnn_units))]\n", - "encoder_emb_inp = encoderNetwork.encoder_embedding(inp)\n", - "a, a_tx, c_tx = encoderNetwork.encoder_rnnlayer(encoder_emb_inp,\n", - " initial_state =encoder_initial_cell_state)\n", - "print('a_tx :', a_tx.shape)\n", - "print('c_tx :', c_tx.shape)\n", - "\n", - "start_tokens = tf.fill([inference_batch_size],ge_tokenizer.word_index[SOS])\n", - "\n", - "end_token = ge_tokenizer.word_index[EOS]\n", - "\n", - "greedy_sampler = tfa.seq2seq.GreedyEmbeddingSampler()\n", - "\n", - "decoder_input = tf.expand_dims([ge_tokenizer.word_index[SOS]]* inference_batch_size,1)\n", - "decoder_emb_inp = decoderNetwork.decoder_embedding(decoder_input)\n", - "\n", - "decoder_instance = tfa.seq2seq.BasicDecoder(cell = decoderNetwork.rnn_cell, sampler = greedy_sampler,\n", - " output_layer=decoderNetwork.dense_layer)\n", - "decoderNetwork.attention_mechanism.setup_memory(a)\n", - "#pass [ last step activations , encoder memory_state ] as input to decoder for LSTM\n", - "print(f\"decoder_initial_state = [a_tx, c_tx] : {np.array([a_tx, c_tx]).shape}\")\n", - "decoder_initial_state = decoderNetwork.build_decoder_initial_state(inference_batch_size,\n", - " encoder_state=[a_tx, c_tx],\n", - " Dtype=tf.float32)\n", - "print(f\"\"\"\n", - "Compared to simple encoder-decoder without attention, the decoder_initial_state\n", - "is an AttentionWrapperState object containing s_prev tensors and context and alignment vector\n", - "\n", - "decoder initial state shape: {np.array(decoder_initial_state).shape}\n", - "decoder_initial_state tensor\n", - "{decoder_initial_state}\n", - "\"\"\")\n", - "\n", - "# Since we do not know the target sequence lengths in advance, we use maximum_iterations to limit the translation lengths.\n", - "# One heuristic is to decode up to two times the source sentence lengths.\n", - "maximum_iterations = tf.round(tf.reduce_max(Tx) * 2)\n", - "\n", - "#initialize inference decoder\n", - "decoder_embedding_matrix = decoderNetwork.decoder_embedding.variables[0] \n", - "(first_finished, first_inputs,first_state) = decoder_instance.initialize(decoder_embedding_matrix,\n", - " start_tokens = start_tokens,\n", - " end_token=end_token,\n", - " initial_state = decoder_initial_state)\n", - "#print( first_finished.shape)\n", - "print(f\"first_inputs returns the same decoder_input i.e. embedding of {SOS} : {first_inputs.shape}\")\n", - "print(f\"start_index_emb_avg {tf.reduce_sum(tf.reduce_mean(first_inputs, axis=0))}\") # mean along the batch\n", - "\n", - "inputs = first_inputs\n", - "state = first_state \n", - "predictions = np.empty((inference_batch_size,0), dtype = np.int32) \n", - "for j in range(maximum_iterations):\n", - " outputs, next_state, next_inputs, finished = decoder_instance.step(j,inputs,state)\n", - " inputs = next_inputs\n", - " state = next_state\n", - " outputs = np.expand_dims(outputs.sample_id,axis = -1)\n", - " predictions = np.append(predictions, outputs, axis = -1)" + "colab_type": "code", + "id": "DUQVLVqUE1YW", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "2f19091c-d198-4ad1-a485-859e76a3f74f" + }, + "source": [ + "# wrong translation\n", + "translate(u'trata de averiguarlo.')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[126 16 892 11 75 4 3]]\n", + "Input: trata de averiguarlo.\n", + "Predicted translation: ['try to figure it out . ']\n" + ], + "name": "stdout" + } ] }, { "cell_type": "markdown", "metadata": { - "id": "iodjSItQds1t" + "id": "IRUuNDeY0HiC", + "colab_type": "text" }, "source": [ - "## Final Translation" + "## Use tf-addons BeamSearchDecoder \n", + "\n" ] }, { "cell_type": "code", - "execution_count": null, "metadata": { - "id": "K6aWFB5IWlH2" + "id": "AJ-RTQ0hsJNL", + "colab_type": "code", + "colab": {} }, - "outputs": [], "source": [ - "#prediction based on our sentence earlier\n", - "print(\"English Sentence:\")\n", - "print(input_raw)\n", - "print(\"\\nGerman Translation:\")\n", - "for i in range(len(predictions)):\n", - " line = predictions[i,:]\n", - " seq = list(itertools.takewhile( lambda index: index !=2, line))\n", - " print(\" \".join( [ge_tokenizer.index_word[w] for w in seq]))" - ] + "def beam_evaluate_sentence(sentence, beam_width=3):\n", + " sentence = dataset_creator.preprocess_sentence(sentence)\n", + "\n", + " inputs = [inp_lang.word_index[i] for i in sentence.split(' ')]\n", + " inputs = tf.keras.preprocessing.sequence.pad_sequences([inputs],\n", + " maxlen=max_length_input,\n", + " padding='post')\n", + " inputs = tf.convert_to_tensor(inputs)\n", + " inference_batch_size = inputs.shape[0]\n", + " result = ''\n", + "\n", + " enc_start_state = [tf.zeros((inference_batch_size, units)), tf.zeros((inference_batch_size,units))]\n", + " enc_out, enc_h, enc_c = encoder(inputs, enc_start_state)\n", + "\n", + " dec_h = enc_h\n", + " dec_c = enc_c\n", + "\n", + " start_tokens = tf.fill([inference_batch_size], targ_lang.word_index[''])\n", + " end_token = targ_lang.word_index['']\n", + "\n", + " # From official documentation\n", + " # NOTE If you are using the BeamSearchDecoder with a cell wrapped in AttentionWrapper, then you must ensure that:\n", + " # The encoder output has been tiled to beam_width via tfa.seq2seq.tile_batch (NOT tf.tile).\n", + " # The batch_size argument passed to the get_initial_state method of this wrapper is equal to true_batch_size * beam_width.\n", + " # The initial state created with get_initial_state above contains a cell_state value containing properly tiled final state from the encoder.\n", + "\n", + " enc_out = tfa.seq2seq.tile_batch(enc_out, multiplier=beam_width)\n", + " decoder.attention_mechanism.setup_memory(enc_out)\n", + " print(\"beam_with * [batch_size, max_length_input, rnn_units] : 3 * [1, 16, 1024]] :\", enc_out.shape)\n", + "\n", + " # set decoder_inital_state which is an AttentionWrapperState considering beam_width\n", + " hidden_state = tfa.seq2seq.tile_batch([enc_h, enc_c], multiplier=beam_width)\n", + " decoder_initial_state = decoder.rnn_cell.get_initial_state(batch_size=beam_width*inference_batch_size, dtype=tf.float32)\n", + " decoder_initial_state = decoder_initial_state.clone(cell_state=hidden_state)\n", + "\n", + " # Instantiate BeamSearchDecoder\n", + " decoder_instance = tfa.seq2seq.BeamSearchDecoder(decoder.rnn_cell,beam_width=beam_width, output_layer=decoder.fc)\n", + " decoder_embedding_matrix = decoder.embedding.variables[0]\n", + "\n", + " # The BeamSearchDecoder object's call() function takes care of everything.\n", + " outputs, final_state, sequence_lengths = decoder_instance(decoder_embedding_matrix, start_tokens=start_tokens, end_token=end_token, initial_state=decoder_initial_state)\n", + " # outputs is tfa.seq2seq.FinalBeamSearchDecoderOutput object. \n", + " # The final beam predictions are stored in outputs.predicted_id\n", + " # outputs.beam_search_decoder_output is a tfa.seq2seq.BeamSearchDecoderOutput object which keep tracks of beam_scores and parent_ids while performing a beam decoding step\n", + " # final_state = tfa.seq2seq.BeamSearchDecoderState object.\n", + " # Sequence Length = [inference_batch_size, beam_width] details the maximum length of the beams that are generated\n", + "\n", + " \n", + " # outputs.predicted_id.shape = (inference_batch_size, time_step_outputs, beam_width)\n", + " # outputs.beam_search_decoder_output.scores.shape = (inference_batch_size, time_step_outputs, beam_width)\n", + " # Convert the shape of outputs and beam_scores to (inference_batch_size, beam_width, time_step_outputs)\n", + " final_outputs = tf.transpose(outputs.predicted_ids, perm=(0,2,1))\n", + " beam_scores = tf.transpose(outputs.beam_search_decoder_output.scores, perm=(0,2,1))\n", + " \n", + " return final_outputs.numpy(), beam_scores.numpy()" + ], + "execution_count": 89, + "outputs": [] }, { - "cell_type": "markdown", + "cell_type": "code", + "metadata": { + "id": "g_LvXGvX8X-O", + "colab_type": "code", + "colab": {} + }, + "source": [ + "def beam_translate(sentence):\n", + " result, beam_scores = beam_evaluate_sentence(sentence)\n", + " print(result.shape, beam_scores.shape)\n", + " for beam, score in zip(result, beam_scores):\n", + " print(beam.shape, score.shape)\n", + " output = targ_lang.sequences_to_texts(beam)\n", + " output = [a[:a.index('')] for a in output]\n", + " beam_score = [a.sum() for a in score]\n", + " print('Input: %s' % (sentence))\n", + " for i in range(len(output)):\n", + " print('{} Predicted translation: {} {}'.format(i+1, output[i], beam_score[i]))\n" + ], + "execution_count": 90, + "outputs": [] + }, + { + "cell_type": "code", "metadata": { - "id": "g6Av-oPWvRc4" + "id": "TODnXBleDzzO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 136 + }, + "outputId": "f048caa5-f7f6-4037-a997-56b5e8922cc8" }, "source": [ - "### The accuracy can be improved by implementing:\n", - "* Beam Search or Lexicon Search\n", - "* Bi-directional encoder-decoder model " + "beam_translate(u'hace mucho frio aqui.')" + ], + "execution_count": 91, + "outputs": [ + { + "output_type": "stream", + "text": [ + "beam_with * [batch_size, max_length_input, rnn_units] : 3 * [1, 16, 1024]] : (3, 16, 1024)\n", + "(1, 3, 7) (1, 3, 7)\n", + "(3, 7) (3, 7)\n", + "Input: hace mucho frio aqui.\n", + "1 Predicted translation: it s very pretty here . -4.117094039916992\n", + "2 Predicted translation: it s very cold here . -14.85302734375\n", + "3 Predicted translation: it s very pretty news . -25.59416389465332\n" + ], + "name": "stdout" + } ] - } - ], - "metadata": { - "accelerator": "GPU", - "colab": { - "collapsed_sections": [], - "name": "networks_seq2seq_nmt.ipynb", - "toc_visible": true }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" + { + "cell_type": "code", + "metadata": { + "id": "_BezQwENFY3L", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 136 + }, + "outputId": "e84a9058-7c9c-4c99-9924-a435edd7e9b4" + }, + "source": [ + "beam_translate(u'¿todavia estan en casa?')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "beam_with * [batch_size, max_length_input, rnn_units] : 3 * [1, 16, 1024]] : (3, 16, 1024)\n", + "(1, 3, 7) (1, 3, 7)\n", + "(3, 7) (3, 7)\n", + "Input: ¿todavia estan en casa?\n", + "1 Predicted translation: are you still home ? -4.036754131317139\n", + "2 Predicted translation: are you still at home ? -15.306867599487305\n", + "3 Predicted translation: are you still go home ? -20.533388137817383\n" + ], + "name": "stdout" + } + ] } - }, - "nbformat": 4, - "nbformat_minor": 0 + ] } From e3e0853c56d05f2e657354a14b0e3210042faf6f Mon Sep 17 00:00:00 2001 From: Tzu-Wei Sung Date: Fri, 11 Sep 2020 12:56:51 -0700 Subject: [PATCH 051/259] Setup notebook testing (#2160) * Setup notebook testing * Trivial change * Run on all notebooks * Format networks_seq2seq_nmt.ipynb * Lint all notebooks * Change repo name * Expose template to be formatted --- .github/workflows/ci_test.yml | 29 + docs/tutorials/_template.ipynb | 2 +- docs/tutorials/image_ops.ipynb | 2 +- .../layers_weightnormalization.ipynb | 2 +- docs/tutorials/losses_triplet.ipynb | 4 +- docs/tutorials/networks_seq2seq_nmt.ipynb | 572 +++++++----------- .../optimizers_conditionalgradient.ipynb | 4 +- 7 files changed, 259 insertions(+), 356 deletions(-) diff --git a/.github/workflows/ci_test.yml b/.github/workflows/ci_test.yml index ac43fdd220..eccafdcf8a 100644 --- a/.github/workflows/ci_test.yml +++ b/.github/workflows/ci_test.yml @@ -95,3 +95,32 @@ jobs: - run: pip install pygithub click - name: Check that the CODEOWNERS is valid run: python .github/workflows/notify_codeowners.py .github/CODEOWNERS + nbfmt: + name: Notebook format + runs-on: ubuntu-latest + steps: + - uses: actions/setup-python@v1 + - uses: actions/checkout@v2 + - name: Install tensorflow-docs + run: python3 -m pip install -U git+https://github.com/tensorflow/docs + - name: Check notebook formatting + run: | + # Run on all notebooks to prevent upstream change. + echo "Check formatting with nbfmt:" + python3 -m tensorflow_docs.tools.nbfmt --test \ + $(find docs/tutorials/ -type f -name *.ipynb) + nblint: + name: Notebook lint + runs-on: ubuntu-latest + steps: + - uses: actions/setup-python@v1 + - uses: actions/checkout@v2 + - name: Install tensorflow-docs + run: python3 -m pip install -U git+https://github.com/tensorflow/docs + - name: Lint notebooks + run: | + # Run on all notebooks to prevent upstream change. + echo "Lint check with nblint:" + python3 -m tensorflow_docs.tools.nblint \ + --arg=repo:tensorflow/addons \ + $(find docs/tutorials/ -type f -name *.ipynb ! -path "docs/tutorials/_template.ipynb") diff --git a/docs/tutorials/_template.ipynb b/docs/tutorials/_template.ipynb index feec9c8738..07994386fd 100644 --- a/docs/tutorials/_template.ipynb +++ b/docs/tutorials/_template.ipynb @@ -57,7 +57,7 @@ " View source on GitHub\n", " \n", " \n", - " Download notebook\n", + " Download notebook\n", " \n", "" ] diff --git a/docs/tutorials/image_ops.ipynb b/docs/tutorials/image_ops.ipynb index 58d9655c77..d43a5bccf9 100644 --- a/docs/tutorials/image_ops.ipynb +++ b/docs/tutorials/image_ops.ipynb @@ -64,7 +64,7 @@ "## Overview\n", "This notebook will demonstrate how to use the some image operations in TensorFlow Addons.\n", "\n", - "Here is the list of image operations we'll be covering in this example:\n", + "Here is the list of image operations you'll be covering in this example:\n", "\n", "- `tfa.image.mean_filter2d`\n", "\n", diff --git a/docs/tutorials/layers_weightnormalization.ipynb b/docs/tutorials/layers_weightnormalization.ipynb index 9a7dc9cbdf..f1884cf8e7 100644 --- a/docs/tutorials/layers_weightnormalization.ipynb +++ b/docs/tutorials/layers_weightnormalization.ipynb @@ -78,7 +78,7 @@ "\n", "Tim Salimans, Diederik P. Kingma (2016)\n", "\n", - "> By reparameterizing the weights in this way we improve the conditioning of the optimization problem and we speed up convergence of stochastic gradient descent. Our reparameterization is inspired by batch normalization but does not introduce any dependencies between the examples in a minibatch. This means that our method can also be applied successfully to recurrent models such as LSTMs and to noise-sensitive applications such as deep reinforcement learning or generative models, for which batch normalization is less well suited. Although our method is much simpler, it still provides much of the speed-up of full batch normalization. In addition, the computational overhead of our method is lower, permitting more optimization steps to be taken in the same amount of time.\n", + "> By reparameterizing the weights in this way you improve the conditioning of the optimization problem and speed up convergence of stochastic gradient descent. Our reparameterization is inspired by batch normalization but does not introduce any dependencies between the examples in a minibatch. This means that our method can also be applied successfully to recurrent models such as LSTMs and to noise-sensitive applications such as deep reinforcement learning or generative models, for which batch normalization is less well suited. Although our method is much simpler, it still provides much of the speed-up of full batch normalization. In addition, the computational overhead of our method is lower, permitting more optimization steps to be taken in the same amount of time.\n", "\n", "> https://arxiv.org/abs/1602.07868 \n", "\n", diff --git a/docs/tutorials/losses_triplet.ipynb b/docs/tutorials/losses_triplet.ipynb index 0130191bd8..fd29dfc898 100644 --- a/docs/tutorials/losses_triplet.ipynb +++ b/docs/tutorials/losses_triplet.ipynb @@ -84,7 +84,7 @@ "\n", "![function](https://user-images.githubusercontent.com/18154355/61484709-7589b800-a96d-11e9-9c3c-e880514af4b7.png)\n", "\n", - "Where A is our anchor input, P is the positive sample input, N is the negative sample input, and alpha is some margin we use to specify when a triplet has become too \"easy\" and we no longer want to adjust the weights from it." + "Where A is our anchor input, P is the positive sample input, N is the negative sample input, and alpha is some margin you use to specify when a triplet has become too \"easy\" and you no longer want to adjust the weights from it." ] }, { @@ -94,7 +94,7 @@ }, "source": [ "## SemiHard Online Learning\n", - "As shown in the paper, the best results are from triplets known as \"Semi-Hard\". These are defined as triplets where the negative is farther from the anchor than the positive, but still produces a positive loss. To efficiently find these triplets we utilize online learning and only train from the Semi-Hard examples in each batch. \n" + "As shown in the paper, the best results are from triplets known as \"Semi-Hard\". These are defined as triplets where the negative is farther from the anchor than the positive, but still produces a positive loss. To efficiently find these triplets you utilize online learning and only train from the Semi-Hard examples in each batch. \n" ] }, { diff --git a/docs/tutorials/networks_seq2seq_nmt.ipynb b/docs/tutorials/networks_seq2seq_nmt.ipynb index 35e38f1909..8fe62ab8d0 100644 --- a/docs/tutorials/networks_seq2seq_nmt.ipynb +++ b/docs/tutorials/networks_seq2seq_nmt.ipynb @@ -1,39 +1,22 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "accelerator": "GPU", - "colab": { - "name": "8_TF-addons_BasicDecoder&BeamDecoder_Usage.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "display_name": "Python 3", - "name": "python3" - } - }, "cells": [ { - "cell_type": "code", + "cell_type": "markdown", "metadata": { - "id": "5aElYAKlV2Mi", - "colab_type": "code", - "colab": {} + "id": "5aElYAKlV2Mi" }, "source": [ "##### Copyright 2020 The TensorFlow Authors." - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "wmYJlt6LWVOU", - "colab_type": "code", - "colab": {} + "cellView": "form", + "id": "wmYJlt6LWVOU" }, + "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", @@ -46,15 +29,12 @@ "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "L-8q8rRRWcp6", - "colab_type": "text" + "id": "L-8q8rRRWcp6" }, "source": [ "# TensorFlow Addons Networks : Sequence-to-Sequence NMT with Attention Mechanism\n", @@ -78,13 +58,12 @@ { "cell_type": "markdown", "metadata": { - "id": "9n0dcDw1Wszw", - "colab_type": "text" + "id": "9n0dcDw1Wszw" }, "source": [ "## Overview\n", "This notebook gives a brief introduction into the ***Sequence to Sequence Model Architecture***\n", - "In this noteboook we broadly cover four essential topics necessary for Neural Machine Translation:\n", + "In this noteboook you broadly cover four essential topics necessary for Neural Machine Translation:\n", "\n", "\n", "* **Data cleaning**\n", @@ -92,14 +71,13 @@ "* **Neural Translation Model with Attention**\n", "* **Final Translation with ```tf.addons.seq2seq.BasicDecoder``` and ```tf.addons.seq2seq.BeamSearchDecoder```** \n", "\n", - "The basic idea behind such a model though, is only the encoder-decoder architecture. These networks are usually used for a variety of tasks like text-summerization, Machine translation, Image Captioning, etc. This tutorial provideas a hands-on understanding of the concept, explaining the technical jargons wherever necessary. We focus on the task of Neural Machine Translation (NMT) which was the very first testbed for seq2seq models.\n" + "The basic idea behind such a model though, is only the encoder-decoder architecture. These networks are usually used for a variety of tasks like text-summerization, Machine translation, Image Captioning, etc. This tutorial provideas a hands-on understanding of the concept, explaining the technical jargons wherever necessary. You focus on the task of Neural Machine Translation (NMT) which was the very first testbed for seq2seq models.\n" ] }, { "cell_type": "markdown", "metadata": { - "id": "MpySVYWJhxaV", - "colab_type": "text" + "id": "MpySVYWJhxaV" }, "source": [ "## Setup" @@ -107,21 +85,13 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "_kxfdP4hJUPB", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 170 - }, - "outputId": "6b64ae6f-4515-462f-ec04-812233a105d3" + "id": "_kxfdP4hJUPB" }, - "source": [ - "!pip install tensorflow-addons==0.11.2" - ], - "execution_count": null, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "Collecting tensorflow-addons==0.11.2\n", @@ -133,18 +103,20 @@ " Uninstalling tensorflow-addons-0.11.0:\n", " Successfully uninstalled tensorflow-addons-0.11.0\n", "Successfully installed tensorflow-addons-0.11.2\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "!pip install tensorflow-addons==0.11.2" ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "tnxXKDjq3jEL", - "colab": {} + "id": "tnxXKDjq3jEL" }, + "outputs": [], "source": [ "import tensorflow as tf\n", "import tensorflow_addons as tfa\n", @@ -158,44 +130,40 @@ "import numpy as np\n", "import os\n", "import io\n", - "import time\n", - "\n" - ], - "execution_count": null, - "outputs": [] + "import time\n" + ] }, { "cell_type": "markdown", "metadata": { - "id": "Ii_vg-XNXTil", - "colab_type": "text" + "id": "Ii_vg-XNXTil" }, "source": [ "## Data Cleaning and Data Preparation \n", "\n", - "We'll use a language dataset provided by http://www.manythings.org/anki/. This dataset contains language translation pairs in the format:\n", + "You'll use a language dataset provided by http://www.manythings.org/anki/. This dataset contains language translation pairs in the format:\n", "\n", "---\n", " May I borrow this book? ¿Puedo tomar prestado este libro?\n", "---\n", "\n", "\n", - "There are a variety of languages available, but we'll use the English-Spanish dataset. After downloading the dataset, here are the steps we'll take to prepare the data:\n", + "There are a variety of languages available, but you'll use the English-Spanish dataset. After downloading the dataset, here are the steps you'll take to prepare the data:\n", "\n", "\n", "1. Add a start and end token to each sentence.\n", "2. Clean the sentences by removing special characters.\n", "3. Create a Vocabulary with word index (mapping from word → id) and reverse word index (mapping from id → word).\n", - "5. Pad each sentence to a maximum length. (Why? we need to fix the maximum length for the inputs to recurrent encoders)" + "5. Pad each sentence to a maximum length. (Why? you need to fix the maximum length for the inputs to recurrent encoders)" ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "PvRnGWnvXm6l", - "colab_type": "code", - "colab": {} + "id": "PvRnGWnvXm6l" }, + "outputs": [], "source": [ "def download_nmt():\n", " path_to_zip = tf.keras.utils.get_file(\n", @@ -204,15 +172,12 @@ "\n", " path_to_file = os.path.dirname(path_to_zip)+\"/spa-eng/spa.txt\"\n", " return path_to_file\n" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "NFKB2c_tX4wU", - "colab_type": "text" + "id": "NFKB2c_tX4wU" }, "source": [ "### Define a NMTDataset class with necessary functions to follow Step 1 to Step 4. \n", @@ -223,11 +188,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "JMAHz7kJXc5N", - "colab_type": "code", - "colab": {} + "id": "JMAHz7kJXc5N" }, + "outputs": [], "source": [ "class NMTDataset:\n", " def __init__(self, problem_type='en-spa'):\n", @@ -307,19 +272,16 @@ " val_dataset = val_dataset.batch(BATCH_SIZE, drop_remainder=True)\n", "\n", " return train_dataset, val_dataset, self.inp_lang_tokenizer, self.targ_lang_tokenizer" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "EIW4NVBmJ25k", - "colab_type": "code", - "colab": {} + "id": "EIW4NVBmJ25k" }, + "outputs": [], "source": [ - "\n", "BUFFER_SIZE = 32000\n", "BATCH_SIZE = 64\n", "# Let's limit the #training examples for faster training\n", @@ -327,45 +289,36 @@ "\n", "dataset_creator = NMTDataset('en-spa')\n", "train_dataset, val_dataset, inp_lang, targ_lang = dataset_creator.call(num_examples, BUFFER_SIZE, BATCH_SIZE)" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "w2lCTy4vKOkB", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "outputId": "1781be10-0554-46ba-84a2-04b71a25217d" + "id": "w2lCTy4vKOkB" }, - "source": [ - "example_input_batch, example_target_batch = next(iter(train_dataset))\n", - "example_input_batch.shape, example_target_batch.shape" - ], - "execution_count": null, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "(TensorShape([64, 16]), TensorShape([64, 11]))" ] }, + "execution_count": 7, "metadata": { "tags": [] }, - "execution_count": 7 + "output_type": "execute_result" } + ], + "source": [ + "example_input_batch, example_target_batch = next(iter(train_dataset))\n", + "example_input_batch.shape, example_target_batch.shape" ] }, { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "rgCLkfv5uO3d" }, "source": [ @@ -374,11 +327,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "TqHsArVZ3jFS", - "colab": {} + "id": "TqHsArVZ3jFS" }, + "outputs": [], "source": [ "vocab_inp_size = len(inp_lang.word_index)+1\n", "vocab_tar_size = len(targ_lang.word_index)+1\n", @@ -388,55 +341,47 @@ "embedding_dim = 256\n", "units = 1024\n", "steps_per_epoch = num_examples//BATCH_SIZE\n" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "g-yY9c6aIu1h", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 51 - }, - "outputId": "2302f86e-df0c-4fba-b1b7-0338a88f4a14" + "id": "g-yY9c6aIu1h" }, - "source": [ - "print(\"max_length_spanish, max_length_english, vocab_size_spanish, vocab_size_english\")\n", - "max_length_input, max_length_output, vocab_inp_size, vocab_tar_size" - ], - "execution_count": null, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "max_length_spanish, max_length_english, vocab_size_spanish, vocab_size_english\n" - ], - "name": "stdout" + ] }, { - "output_type": "execute_result", "data": { "text/plain": [ "(16, 11, 9415, 4936)" ] }, + "execution_count": 9, "metadata": { "tags": [] }, - "execution_count": 9 + "output_type": "execute_result" } + ], + "source": [ + "print(\"max_length_spanish, max_length_english, vocab_size_spanish, vocab_size_english\")\n", + "max_length_input, max_length_output, vocab_inp_size, vocab_tar_size" ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "nZ2rI24i3jFg", - "colab": {} + "id": "nZ2rI24i3jFg" }, + "outputs": [], "source": [ "##### \n", "\n", @@ -462,21 +407,25 @@ "\n", " def initialize_hidden_state(self):\n", " return [tf.zeros((self.batch_sz, self.enc_units)), tf.zeros((self.batch_sz, self.enc_units))] " - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "60gSVh05Jl6l", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - }, - "outputId": "dd7acce3-8dbd-4eb8-d839-645b0c063d8d" + "id": "60gSVh05Jl6l" }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Encoder output shape: (batch size, sequence length, units) (64, 16, 1024)\n", + "Encoder h vecotr shape: (batch size, units) (64, 1024)\n", + "Encoder c vector shape: (batch size, units) (64, 1024)\n" + ] + } + ], "source": [ "## Test Encoder Stack\n", "\n", @@ -489,27 +438,15 @@ "print ('Encoder output shape: (batch size, sequence length, units) {}'.format(sample_output.shape))\n", "print ('Encoder h vecotr shape: (batch size, units) {}'.format(sample_h.shape))\n", "print ('Encoder c vector shape: (batch size, units) {}'.format(sample_c.shape))" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Encoder output shape: (batch size, sequence length, units) (64, 16, 1024)\n", - "Encoder h vecotr shape: (batch size, units) (64, 1024)\n", - "Encoder c vector shape: (batch size, units) (64, 1024)\n" - ], - "name": "stdout" - } ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "yJ_B3mhW3jFk", - "colab": {} + "id": "yJ_B3mhW3jFk" }, + "outputs": [], "source": [ "class Decoder(tf.keras.Model):\n", " def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz, attention_type='luong'):\n", @@ -569,23 +506,24 @@ " def call(self, inputs, initial_state):\n", " x = self.embedding(inputs)\n", " outputs, _, _ = self.decoder(x, initial_state=initial_state, sequence_length=self.batch_sz*[max_length_output-1])\n", - " return outputs\n", - "\n" - ], - "execution_count": null, - "outputs": [] + " return outputs\n" + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "DaiO0Z6_Ml1c", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "outputId": "069bf53d-5121-4f82-9fd9-76c62d3223ec" + "id": "DaiO0Z6_Ml1c" }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Decoder Outputs Shape: (64, 10, 4936)\n" + ] + } + ], "source": [ "# Test decoder stack\n", "\n", @@ -598,22 +536,11 @@ "sample_decoder_outputs = decoder(sample_x, initial_state)\n", "\n", "print(\"Decoder Outputs Shape: \", sample_decoder_outputs.rnn_output.shape)\n" - ], - "execution_count": null, - "outputs": [ - { - "output_type": "stream", - "text": [ - "Decoder Outputs Shape: (64, 10, 4936)\n" - ], - "name": "stdout" - } ] }, { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "_ch_71VbIRfK" }, "source": [ @@ -622,11 +549,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "WmTHr5iV3jFr", - "colab": {} + "id": "WmTHr5iV3jFr" }, + "outputs": [], "source": [ "optimizer = tf.keras.optimizers.Adam()\n", "\n", @@ -641,14 +568,11 @@ " loss = mask* loss\n", " loss = tf.reduce_mean(loss)\n", " return loss " - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "DMVWzzsfNl4e" }, "source": [ @@ -657,26 +581,23 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "Zj8bXQTgNwrF", - "colab": {} + "id": "Zj8bXQTgNwrF" }, + "outputs": [], "source": [ "checkpoint_dir = './training_checkpoints'\n", "checkpoint_prefix = os.path.join(checkpoint_dir, \"ckpt\")\n", "checkpoint = tf.train.Checkpoint(optimizer=optimizer,\n", " encoder=encoder,\n", " decoder=decoder)" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "8Bw95utNiFHa", - "colab_type": "text" + "id": "8Bw95utNiFHa" }, "source": [ "## One train_step operations" @@ -684,11 +605,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "sC9ArXSsVfqn", - "colab": {} + "id": "sC9ArXSsVfqn" }, + "outputs": [], "source": [ "@tf.function\n", "def train_step(inp, targ, enc_hidden):\n", @@ -715,15 +636,12 @@ " optimizer.apply_gradients(zip(gradients, variables))\n", "\n", " return loss" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "pey8eb9piMMg", - "colab_type": "text" + "id": "pey8eb9piMMg" }, "source": [ "## Train the model" @@ -731,44 +649,13 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "ddefjBMa3jF0", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 1000 - }, - "outputId": "2b43d0b9-6e1f-4592-92ce-41d86bdd5514" + "id": "ddefjBMa3jF0" }, - "source": [ - "EPOCHS = 10\n", - "\n", - "for epoch in range(EPOCHS):\n", - " start = time.time()\n", - "\n", - " enc_hidden = encoder.initialize_hidden_state()\n", - " total_loss = 0\n", - " # print(enc_hidden[0].shape, enc_hidden[1].shape)\n", - "\n", - " for (batch, (inp, targ)) in enumerate(train_dataset.take(steps_per_epoch)):\n", - " batch_loss = train_step(inp, targ, enc_hidden)\n", - " total_loss += batch_loss\n", - "\n", - " if batch % 100 == 0:\n", - " print('Epoch {} Batch {} Loss {:.4f}'.format(epoch + 1,\n", - " batch,\n", - " batch_loss.numpy()))\n", - " # saving (checkpoint) the model every 2 epochs\n", - " if (epoch + 1) % 2 == 0:\n", - " checkpoint.save(file_prefix = checkpoint_prefix)\n", - "\n", - " print('Epoch {} Loss {:.4f}'.format(epoch + 1,\n", - " total_loss / steps_per_epoch))\n", - " print('Time taken for 1 epoch {} sec\\n'.format(time.time() - start))" - ], - "execution_count": null, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "Epoch 1 Batch 0 Loss 5.1692\n", @@ -841,15 +728,39 @@ "Epoch 10 Loss 0.1316\n", "Time taken for 1 epoch 29.576894283294678 sec\n", "\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "EPOCHS = 10\n", + "\n", + "for epoch in range(EPOCHS):\n", + " start = time.time()\n", + "\n", + " enc_hidden = encoder.initialize_hidden_state()\n", + " total_loss = 0\n", + " # print(enc_hidden[0].shape, enc_hidden[1].shape)\n", + "\n", + " for (batch, (inp, targ)) in enumerate(train_dataset.take(steps_per_epoch)):\n", + " batch_loss = train_step(inp, targ, enc_hidden)\n", + " total_loss += batch_loss\n", + "\n", + " if batch % 100 == 0:\n", + " print('Epoch {} Batch {} Loss {:.4f}'.format(epoch + 1,\n", + " batch,\n", + " batch_loss.numpy()))\n", + " # saving (checkpoint) the model every 2 epochs\n", + " if (epoch + 1) % 2 == 0:\n", + " checkpoint.save(file_prefix = checkpoint_prefix)\n", + "\n", + " print('Epoch {} Loss {:.4f}'.format(epoch + 1,\n", + " total_loss / steps_per_epoch))\n", + " print('Time taken for 1 epoch {} sec\\n'.format(time.time() - start))" ] }, { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "mU3Ce8M6I3rz" }, "source": [ @@ -858,11 +769,11 @@ }, { "cell_type": "code", + "execution_count": 98, "metadata": { - "colab_type": "code", - "id": "EbQpyYs13jF_", - "colab": {} + "id": "EbQpyYs13jF_" }, + "outputs": [], "source": [ "def evaluate_sentence(sentence):\n", " sentence = dataset_creator.preprocess_sentence(sentence)\n", @@ -895,9 +806,9 @@ " decoder_initial_state = decoder.build_initial_state(inference_batch_size, [enc_h, enc_c], tf.float32)\n", "\n", "\n", - " ### Since the BasicDecoder wraps around Decoder's rnn cell only, we have to ensure that the inputs to BasicDecoder \n", + " ### Since the BasicDecoder wraps around Decoder's rnn cell only, you have to ensure that the inputs to BasicDecoder \n", " ### decoding step is output of embedding layer. tfa.seq2seq.GreedyEmbeddingSampler() takes care of this. \n", - " ### We only need to get the weights of embedding layer, which can be done by decoder.embedding.variables[0] and pass this callabble to BasicDecoder's call() function\n", + " ### You only need to get the weights of embedding layer, which can be done by decoder.embedding.variables[0] and pass this callabble to BasicDecoder's call() function\n", "\n", " decoder_embedding_matrix = decoder.embedding.variables[0]\n", " \n", @@ -910,14 +821,11 @@ " result = targ_lang.sequences_to_texts(result)\n", " print('Input: %s' % (sentence))\n", " print('Predicted translation: {}'.format(result))" - ], - "execution_count": 98, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "colab_type": "text", "id": "n250XbnjOaqP" }, "source": [ @@ -926,162 +834,130 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "UJpT9D5_OgP6", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 34 - }, - "outputId": "26ffd441-4e4f-41c6-afdc-0be8ffd07d20" + "id": "UJpT9D5_OgP6" }, - "source": [ - "# restoring the latest checkpoint in checkpoint_dir\n", - "checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))" - ], - "execution_count": null, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "" ] }, + "execution_count": 20, "metadata": { "tags": [] }, - "execution_count": 20 + "output_type": "execute_result" } + ], + "source": [ + "# restoring the latest checkpoint in checkpoint_dir\n", + "checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))" ] }, { "cell_type": "code", + "execution_count": 99, "metadata": { - "id": "WYmYhNN_faR5", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - }, - "outputId": "c3487c60-dff9-49d3-ae9d-5fa6d9f97899" + "id": "WYmYhNN_faR5" }, - "source": [ - "translate(u'hace mucho frio aqui.')" - ], - "execution_count": 99, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "[[ 11 12 49 224 40 4 3]]\n", "Input: hace mucho frio aqui.\n", "Predicted translation: ['it s very pretty here . ']\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "translate(u'hace mucho frio aqui.')" ] }, { "cell_type": "code", + "execution_count": 100, "metadata": { - "colab_type": "code", - "id": "zSx2iM36EZQZ", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - }, - "outputId": "bdca33b5-a395-4335-9dd5-cf0ce5065571" + "id": "zSx2iM36EZQZ" }, - "source": [ - "translate(u'esta es mi vida.')" - ], - "execution_count": 100, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "[[ 20 9 22 190 4 3]]\n", "Input: esta es mi vida.\n", "Predicted translation: ['this is my life . ']\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "translate(u'esta es mi vida.')" ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "A3LLCx3ZE0Ls", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - }, - "outputId": "70c42c2e-dfb1-4040-bad7-3a35ae673880" + "id": "A3LLCx3ZE0Ls" }, - "source": [ - "translate(u'¿todavia estan en casa?')" - ], - "execution_count": null, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "[[25 7 90 8 3]]\n", "Input: ¿todavia estan en casa?\n", "Predicted translation: ['are you home ? ']\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "translate(u'¿todavia estan en casa?')" ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "colab_type": "code", - "id": "DUQVLVqUE1YW", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 68 - }, - "outputId": "2f19091c-d198-4ad1-a485-859e76a3f74f" + "id": "DUQVLVqUE1YW" }, - "source": [ - "# wrong translation\n", - "translate(u'trata de averiguarlo.')" - ], - "execution_count": null, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "[[126 16 892 11 75 4 3]]\n", "Input: trata de averiguarlo.\n", "Predicted translation: ['try to figure it out . ']\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# wrong translation\n", + "translate(u'trata de averiguarlo.')" ] }, { "cell_type": "markdown", "metadata": { - "id": "IRUuNDeY0HiC", - "colab_type": "text" + "id": "IRUuNDeY0HiC" }, "source": [ - "## Use tf-addons BeamSearchDecoder \n", - "\n" + "## Use tf-addons BeamSearchDecoder \n" ] }, { "cell_type": "code", + "execution_count": 89, "metadata": { - "id": "AJ-RTQ0hsJNL", - "colab_type": "code", - "colab": {} + "id": "AJ-RTQ0hsJNL" }, + "outputs": [], "source": [ "def beam_evaluate_sentence(sentence, beam_width=3):\n", " sentence = dataset_creator.preprocess_sentence(sentence)\n", @@ -1138,17 +1014,15 @@ " beam_scores = tf.transpose(outputs.beam_search_decoder_output.scores, perm=(0,2,1))\n", " \n", " return final_outputs.numpy(), beam_scores.numpy()" - ], - "execution_count": 89, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 90, "metadata": { - "id": "g_LvXGvX8X-O", - "colab_type": "code", - "colab": {} + "id": "g_LvXGvX8X-O" }, + "outputs": [], "source": [ "def beam_translate(sentence):\n", " result, beam_scores = beam_evaluate_sentence(sentence)\n", @@ -1161,27 +1035,17 @@ " print('Input: %s' % (sentence))\n", " for i in range(len(output)):\n", " print('{} Predicted translation: {} {}'.format(i+1, output[i], beam_score[i]))\n" - ], - "execution_count": 90, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 91, "metadata": { - "id": "TODnXBleDzzO", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 136 - }, - "outputId": "f048caa5-f7f6-4037-a997-56b5e8922cc8" + "id": "TODnXBleDzzO" }, - "source": [ - "beam_translate(u'hace mucho frio aqui.')" - ], - "execution_count": 91, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "beam_with * [batch_size, max_length_input, rnn_units] : 3 * [1, 16, 1024]] : (3, 16, 1024)\n", @@ -1191,28 +1055,22 @@ "1 Predicted translation: it s very pretty here . -4.117094039916992\n", "2 Predicted translation: it s very cold here . -14.85302734375\n", "3 Predicted translation: it s very pretty news . -25.59416389465332\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "beam_translate(u'hace mucho frio aqui.')" ] }, { "cell_type": "code", + "execution_count": null, "metadata": { - "id": "_BezQwENFY3L", - "colab_type": "code", - "colab": { - "base_uri": "https://localhost:8080/", - "height": 136 - }, - "outputId": "e84a9058-7c9c-4c99-9924-a435edd7e9b4" + "id": "_BezQwENFY3L" }, - "source": [ - "beam_translate(u'¿todavia estan en casa?')" - ], - "execution_count": null, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "beam_with * [batch_size, max_length_input, rnn_units] : 3 * [1, 16, 1024]] : (3, 16, 1024)\n", @@ -1222,10 +1080,26 @@ "1 Predicted translation: are you still home ? -4.036754131317139\n", "2 Predicted translation: are you still at home ? -15.306867599487305\n", "3 Predicted translation: are you still go home ? -20.533388137817383\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "beam_translate(u'¿todavia estan en casa?')" ] } - ] + ], + "metadata": { + "accelerator": "GPU", + "colab": { + "collapsed_sections": [], + "name": "networks_seq2seq_nmt.ipynb", + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + } + }, + "nbformat": 4, + "nbformat_minor": 0 } diff --git a/docs/tutorials/optimizers_conditionalgradient.ipynb b/docs/tutorials/optimizers_conditionalgradient.ipynb index bf964b640a..8ac4c1f6d8 100644 --- a/docs/tutorials/optimizers_conditionalgradient.ipynb +++ b/docs/tutorials/optimizers_conditionalgradient.ipynb @@ -75,7 +75,7 @@ "# ConditionalGradient\n", "\n", "\n", - "> Constraining the parameters of a neural network has been shown to be beneficial in training because of the underlying regularization effects. Often, parameters are constrained via a soft penalty (which never guarantees the constraint satisfaction) or via a projection operation (which is computationally expensive). Conditional gradient (CG) optimizer, on the other hand, enforces the constraints strictly without the need for an expensive projection step. It works by minimizing a linear approximation of the objective within the constraint set. In this notebook, we demonstrate the appliction of Frobenius norm constraint via the CG optimizer on the MNIST dataset. CG is now available as a tensorflow API. More details of the optimizer are available at https://arxiv.org/pdf/1803.06453.pdf\n" + "> Constraining the parameters of a neural network has been shown to be beneficial in training because of the underlying regularization effects. Often, parameters are constrained via a soft penalty (which never guarantees the constraint satisfaction) or via a projection operation (which is computationally expensive). Conditional gradient (CG) optimizer, on the other hand, enforces the constraints strictly without the need for an expensive projection step. It works by minimizing a linear approximation of the objective within the constraint set. In this notebook, you demonstrate the appliction of Frobenius norm constraint via the CG optimizer on the MNIST dataset. CG is now available as a tensorflow API. More details of the optimizer are available at https://arxiv.org/pdf/1803.06453.pdf\n" ] }, { @@ -331,7 +331,7 @@ "id": "0tJYQBRt-ZUl" }, "source": [ - "The current implementation of CG optimizer is based on Frobenius Norm, with considering Frobenius Norm as regularizer in the target function. Therefore, we compare CG’s regularized effect with SGD optimizer, which has not imposed Frobenius Norm regularizer." + "The current implementation of CG optimizer is based on Frobenius Norm, with considering Frobenius Norm as regularizer in the target function. Therefore, you compare CG’s regularized effect with SGD optimizer, which has not imposed Frobenius Norm regularizer." ] }, { From 4eeb2b2d20b2f04f0467a83e986c1367144caaaa Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 09:58:24 +0500 Subject: [PATCH 052/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index b7ccaa85d6..6776077c2f 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(actuals, predictions) + >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From 62fb9d5441a808be4a0f7f0206bd58116192cc6f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:13:50 +0500 Subject: [PATCH 053/259] y_true, y_pred are not accepted in hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 6776077c2f..b7ccaa85d6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> metric = hamming_distance(actuals, predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From 62e1eb8a055a61b8a799540bb61ff27c11e10eab Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:24:37 +0500 Subject: [PATCH 054/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index b7ccaa85d6..9f2a78e7b0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,9 +90,10 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - >>> # hl.update_state(actuals, predictions) + + >>> hl.update_state(actuals, predictions) >>> # uncomment the line below to see the result - >>> # print('Hamming loss: ', hl.result().numpy()) # 0.25 + >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -100,7 +101,8 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - >>> # hl.update_state(actuals, predictions) + + >>> hl.update_state(actuals, predictions) >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ From eb2ffb84b6cb9f535c583a4e1708428361a02712 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:28:11 +0500 Subject: [PATCH 055/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 9f2a78e7b0..c4a15911a0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,7 +90,7 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - + >>> hl.update_state(actuals, predictions) >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 @@ -101,7 +101,7 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - + >>> hl.update_state(actuals, predictions) >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 From 58eae9f77672122d889b1713a0287d73c8721939 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:04:52 +0500 Subject: [PATCH 056/259] assigned metric.update_Status to result --- tensorflow_addons/metrics/cohens_kappa.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cce36ef738..356f958b51 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # metric.update_state(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', metric.result().numpy()) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # metric.update_state(actuals, preds, sample_weight=weights) + >>> result = metric.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 From a57a7f3a94fda004f8f1054ffa8a8d945f349b8e Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:12:16 +0500 Subject: [PATCH 057/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 9227be1080..fa0570bcd9 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -49,9 +49,9 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ... dtype=tf.float32) >>> # Matthews correlation coefficient >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', metric.result().numpy()) + >>> # print('Matthews correlation coefficient is:', result.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 388e9e1f091856dd03183d91b718f599fd15537e Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:14:40 +0500 Subject: [PATCH 058/259] assigned m.update_status (yactual,ypred) to result --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 7cdd9deb26..de34960af6 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> # metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', metric.result().numpy()) + >>> result = metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', result.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -63,8 +63,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> # metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', metric.result().numpy()) + >>> result = metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', result.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From 8dbeaaaa07028cbe79c26af8a4297b782792c9f8 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:15:53 +0500 Subject: [PATCH 059/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 98d9b735bd..02dc45749b 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -65,7 +65,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> metric = tfa.metrics.r_square.RSquare() - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From e818d2ee73ce2f2da7a058331c2a850db1305d42 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:27:27 +0500 Subject: [PATCH 060/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 02dc45749b..1b8ad4ee33 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -65,9 +65,10 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> metric = tfa.metrics.r_square.RSquare() - >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result - >>> # print('R^2 score is: ', result(actuals, preds).numpy()) + >>> # print('R^2 score is: ', result.numpy()) >>> # R^2 score is: 0.57142866 """ From 07365ac4a49b2f4b398f9772e6b555e780543332 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:31:38 +0500 Subject: [PATCH 061/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index de34960af6..c4ec7d5c90 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,9 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> result = metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', result.result().numpy()) + >>> metric.update_state(y_true, y_pred) + >>> result = metric(y_true, y_pred) + >>> # print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -63,8 +64,9 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> result = metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', result.result().numpy()) + >>> metric.update_state(y_true, y_pred) + >>> result = metric(y_true, y_pred) + >>> # print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From 557cc3b87368fcb5018921154f1e8c6599a9527e Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:32:39 +0500 Subject: [PATCH 062/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index fa0570bcd9..cc5c9f4c15 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -49,9 +49,10 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ... dtype=tf.float32) >>> # Matthews correlation coefficient >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', result.result().numpy()) + >>> # print('Matthews correlation coefficient is:', result.numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 13466d7c3454aa006fa207e165245c1e857c301b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:34:04 +0500 Subject: [PATCH 063/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index a123a9f224..9e6f6ea648 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -38,7 +38,7 @@ class GeometricMean(Metric): >>> metric = tfa.metrics.GeometricMean() >>> metric.update_state([1, 3, 5, 7, 9]) >>> metric.result().numpy() - 3.9362833 + >>> # 3.9362833 """ From 1118061e32dfc2118a2521866434757942674170 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:37:56 +0500 Subject: [PATCH 064/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 356f958b51..612aec63e3 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,14 +44,16 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> result = metric.update_state(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.result().numpy()) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> result = metric.update_state(actuals, preds, sample_weight=weights) - >>> # print('Final result: ', metric.result().numpy()) + >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) + >>> result = metric(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.numpy()) >>> # Final result: 0.37209308 Usage with tf.keras API: From 3897e2f492551ac29cebd4b83561e1465fdc263d Mon Sep 17 00:00:00 2001 From: hongy <34040987+hyang0129@users.noreply.github.com> Date: Mon, 14 Sep 2020 13:39:36 -0400 Subject: [PATCH 065/259] Discriminative Layer Training (#969) * initial setup. need to build tests * build some tests. need to test them * fixed typo * created first test * created first test * accidentally messed up another file * accidentally messed up another file * accidentally messed up another file * added run all distributed * fixed formatting * trying to fix tests not running on github CI. * realized that I should probably add the new optimizer files to the build and init * added typeguard and docstring * removed run_all_distributed * graph and eager testing for SGD * reformatted * added distributed tests * removed distributed tests * reverted discriminative layer grad adjust back to apply gradients * added distributed tests with one time virtual device init * increased tolerance for distributed added comments explaining tests * changed how distributed is recognized for increasing tolerance * Redesigned Logic into Optimizer Wrapper (#1) * redesigned methodology to use multiple optimizers (one per unique LR) and pass grads to these multiple optimizers. Should allow for complex optimizers to behave properly * adjusted behavior of resource apply to only return the op if the lr_mult matches the lr_mult of the optimizer should only return 1 op for each var. * updated init file changed training config * removed variable position and added some more comments * removed grouped variables as unnecessary * reformatted * updated documentation explicitly defined serialization as not supported * added typecheck for name * added typecheck for name * fixed blank line at end of init file * realized no new line meant to add new line guessing that build file needs to be in alpha order? * ran buildifier * fixed accidentally affecting moving average * changed print to logging.info * changed print to logging.info * Revert "changed print to logging.info" This reverts commit 3fa5e190 * added tutorial. tutorial doesn't import from tfa. May need to remove from PR. Please let me know * refactored to use static method refactored to use getattr updated warning on not using lr_mult expanded on some docstrings * updated the usage of lr_mult in variables * renamed discriminative wrapper to disclayeropt * added note to disuade directly calling apply_gradients * updated toy_cnn to use tempdir and no longer call context.eager implemented toy_rnn function with same flow as toycnn * added toy_rnn and sgd to the test permutations * refactored permutes and train results into private fns * reformatted files and fixed flake 8 issues fixed bad references when lr_mult was changed * added missing functions in prep for tests * updated assign lr mult and explained further why refactored get lowest layers to assign sublayers explained recursively assign sublayers better * forgot to run black so ran it to reformat * specified inputshape for rnn * increased size of test temporarily removed SGD opt. Double opts doubles the number of tests to run so just need to see how long this one takes. * remove toy rnn for now * changed back to medium. maybe large was not actually increasing runtime * fixed input layer * fixed input layer being in wrong place * virtual device modification issue * fixed incorrect usage of lr_mult * added comments for tests explaining them better added toy rnn for testing * added new test fix toy rnn initialization * fixed typo * added inputshape so that pretrained rnn generates weights * changed test to allow head to learn. it should move the loss better * reformatted * fixed test for variable assignment added get config and from config * reformatted * fixed layer references from 1 to 0 because input layer isn't counted as an actual layer in the layer list * reformatted * increased lr and epochs because learning was happning, but assertless tolerance too low * attempting to use run distributed from test utils * removed tutorial * switched to alternative distributed training method * trying to use run distributed without graph and eager * trying to use run_distributed * seems that doing any tensorstuff before tf.test.main creates the issue. changed models to auto check if weights exist and create or load * forgot to return a model on first run of model fn * create model weights on init * changed how args are passed for testcase * changed how args are passed for testcase * try fix init * trying to init weights on model properly * trying to init weights on model properly * just trying all the possibilities * trying to fix weights setup * expanded some comments for some tests * fixed some docstrings and expanded on some comments * reformatted files expanded on many comments and added full stops fixed get/from_config based on optimzierv2 added model checkpoint test * capitalized comments properly. * removed sgd, reduced size of training inputs. * simplified checkpoint name * reformatted * remove run tests in notebook * updated README.md fixed indent for __init__ added test for from config and to config * fixed formatting * removed distributed tests and added a warning if optimizer is initialized within a strategy scope * renamed test_wrap to wrap_test bc pytest thought it was a test. * converting tests into the pytest framework * converted tests and parameterized * cleaned up code * added additional checks and doc string for changes in lr multiplier during training. * changed comment * Simplified discriminative layer training by using a multi optimizer wrapper class. Removed old tests and added new tests conforming to pytest standard. * Refactored code using black and flake8 * updated init file * fixed typeguard error and usage of private/experimental api. * restructured wrapper serialization and removed unnecessary components. * expanded on docstr and added repr * cleaned up docstrings, added assertion tests, and added explicit test for only the serialization * ran black and flake8 * fixed doc string Co-authored-by: gabrieldemarmiesse --- tensorflow_addons/optimizers/__init__.py | 3 + .../discriminative_layer_training.py | 166 ++++++++++++++++++ .../discriminative_layer_training_test.py | 113 ++++++++++++ 3 files changed, 282 insertions(+) create mode 100644 tensorflow_addons/optimizers/discriminative_layer_training.py create mode 100644 tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py diff --git a/tensorflow_addons/optimizers/__init__.py b/tensorflow_addons/optimizers/__init__.py index e3a6b996dc..5e1d90f2e4 100644 --- a/tensorflow_addons/optimizers/__init__.py +++ b/tensorflow_addons/optimizers/__init__.py @@ -26,6 +26,9 @@ from tensorflow_addons.optimizers.cyclical_learning_rate import ( ExponentialCyclicalLearningRate, ) +from tensorflow_addons.optimizers.discriminative_layer_training import ( + MultiOptimzer, +) from tensorflow_addons.optimizers.lamb import LAMB from tensorflow_addons.optimizers.lazy_adam import LazyAdam from tensorflow_addons.optimizers.lookahead import Lookahead diff --git a/tensorflow_addons/optimizers/discriminative_layer_training.py b/tensorflow_addons/optimizers/discriminative_layer_training.py new file mode 100644 index 0000000000..e56387d682 --- /dev/null +++ b/tensorflow_addons/optimizers/discriminative_layer_training.py @@ -0,0 +1,166 @@ +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Discriminative Layer Training Optimizer for TensorFlow.""" + +from typing import Union + +import tensorflow as tf +from typeguard import typechecked + + +@tf.keras.utils.register_keras_serializable(package="Addons") +class MultiOptimzer(tf.keras.optimizers.Optimizer): + """Multi Optimizer Wrapper for Discriminative Layer Training. + + Creates a wrapper around a set of instantiated optimizer layer pairs. Generally useful for transfer learning + of deep networks. + + Each optimizer will optimize only the weights associated with its paired layer. This can be used + to implement discriminative layer training by assigning different learning rates to each optimizer + layer pair. (Optimizer, list(Layers)) pairs are also supported. Please note that the layers must be + instantiated before instantiating the optimizer. + + Args: + optimizers_and_layers: a list of tuples of an optimizer and a layer or model. Each tuple should contain + exactly 1 instantiated optimizer and 1 object that subclasses tf.keras.Model or tf.keras.Layer. Nested + layers and models will be automatically discovered. Alternatively, in place of a single layer, you can pass + a list of layers. + optimizer_specs: specialized list for serialization. Should be left as None for almost all cases. If you are + loading a serialized version of this optimizer, please use tf.keras.models.load_model after saving a + model compiled with this optimizer. + + Usage: + + ```python + model = get_model() + + opt1 = tf.keras.optimizers.Adam(learning_rate=1e-4) + opt2 = tf.keras.optimizers.Adam(learning_rate=1e-2) + + opt_layer_pairs = [(opt1, model.layers[0]), (opt2, model.layers[1:])] + + loss = tf.keras.losses.MSE + optimizer = tfa.optimizers.MultiOpt(opt_layer_pairs) + + model.compile(optimizer=optimizer, loss = loss) + + model.fit(x,y) + ''' + + Reference: + + [Universal Language Model Fine-tuning for Text Classification](https://arxiv.org/abs/1801.06146) + [Collaborative Layer-wise Discriminative Learning in Deep Neural Networks](https://arxiv.org/abs/1607.05440) + + Notes: + + Currently, MultiOpt does not support callbacks that modify optimizers. However, you can instantiate + optimizer layer pairs with tf.keras.optimizers.schedules.LearningRateSchedule instead of a static learning + rate. + + This code should function on CPU, GPU, and TPU. Apply the with strategy.scope() context as you + would with any other optimizer. + + """ + + @typechecked + def __init__( + self, + optimizers_and_layers: Union[list, None] = None, + optimizer_specs: Union[list, None] = None, + name: str = "MultiOptimzer", + **kwargs + ): + + super(MultiOptimzer, self).__init__(name, **kwargs) + + if optimizer_specs is None and optimizers_and_layers is not None: + self.optimizer_specs = [ + self.create_optimizer_spec(opt, layer) + for opt, layer in optimizers_and_layers + ] + + elif optimizer_specs is not None and optimizers_and_layers is None: + self.optimizer_specs = [ + self.maybe_initialize_optimizer_spec(spec) for spec in optimizer_specs + ] + + else: + raise RuntimeError( + "You must specify either an list of optimizers and layers or a list of optimizer_specs" + ) + + def apply_gradients(self, grads_and_vars, name=None, **kwargs): + """Wrapped apply_gradient method. + + Returns a list of tf ops to be executed. + Name of variable is used rather than var.ref() to enable serialization and deserialization. + """ + + for spec in self.optimizer_specs: + spec["gv"] = [] + + for grad, var in tuple(grads_and_vars): + for spec in self.optimizer_specs: + for name in spec["weights"]: + if var.name == name: + spec["gv"].append((grad, var)) + + return tf.group( + [ + spec["optimizer"].apply_gradients(spec["gv"], **kwargs) + for spec in self.optimizer_specs + ] + ) + + def get_config(self): + config = super(MultiOptimzer, self).get_config() + config.update({"optimizer_specs": self.optimizer_specs}) + return config + + @classmethod + def create_optimizer_spec(cls, optimizer_instance, layer): + + assert isinstance( + optimizer_instance, tf.keras.optimizers.Optimizer + ), "Object passed is not an instance of tf.keras.optimizers.Optimizer" + + assert isinstance(layer, tf.keras.layers.Layer) or isinstance( + layer, tf.keras.Model + ), "Object passed is not an instance of tf.keras.layers.Layer nor tf.keras.Model" + + if type(layer) == list: + weights = [var.name for sublayer in layer for var in sublayer.weights] + else: + weights = [var.name for var in layer.weights] + + return { + "optimizer": optimizer_instance, + "weights": weights, + } + + @classmethod + def maybe_initialize_optimizer_spec(cls, optimizer_spec): + if type(optimizer_spec["optimizer"]) == dict: + optimizer_spec["optimizer"] = tf.keras.optimizers.deserialize( + optimizer_spec["optimizer"] + ) + + return optimizer_spec + + def __repr__(self): + return "Multi Optimizer with %i optimizer layer pairs" % len( + self.optimizer_specs + ) diff --git a/tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py b/tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py new file mode 100644 index 0000000000..8a93cfb903 --- /dev/null +++ b/tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py @@ -0,0 +1,113 @@ +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Tests for Discriminative Layer Training Optimizer for TensorFlow.""" + +import pytest +import numpy as np +import tensorflow as tf + +from tensorflow_addons.optimizers.discriminative_layer_training import MultiOptimzer +from tensorflow_addons.utils import test_utils + + +def _dtypes_to_test(use_gpu): + # Based on issue #347 in the following link, + # "https://github.com/tensorflow/addons/issues/347" + # tf.half is not registered for 'ResourceScatterUpdate' OpKernel + # for 'GPU' devices. + # So we have to remove tf.half when testing with gpu. + # The function "_DtypesToTest" is from + # "https://github.com/tensorflow/tensorflow/blob/5d4a6cee737a1dc6c20172a1dc1 + # 5df10def2df72/tensorflow/python/kernel_tests/conv_ops_3d_test.py#L53-L62" + # TODO(WindQAQ): Clean up this in TF2.4 + + if use_gpu: + return [tf.float32, tf.float64] + else: + return [tf.half, tf.float32, tf.float64] + + +@pytest.mark.with_device(["cpu", "gpu"]) +@pytest.mark.parametrize("dtype", [tf.float16, tf.float32, tf.float64]) +@pytest.mark.parametrize("serialize", [True, False]) +def test_fit_layer_optimizer(dtype, device, serialize): + # Test ensures that each optimizer is only optimizing its own layer with its learning rate + + if "gpu" in device and dtype == tf.float16: + pytest.xfail("See https://github.com/tensorflow/addons/issues/347") + + model = tf.keras.Sequential( + [tf.keras.Input(shape=[1]), tf.keras.layers.Dense(1), tf.keras.layers.Dense(1)] + ) + + x = np.array(np.ones([100])) + y = np.array(np.ones([100])) + + weights_before_train = ( + model.layers[0].weights[0].numpy(), + model.layers[1].weights[0].numpy(), + ) + + opt1 = tf.keras.optimizers.Adam(learning_rate=1e-3) + opt2 = tf.keras.optimizers.SGD(learning_rate=0) + + opt_layer_pairs = [(opt1, model.layers[0]), (opt2, model.layers[1])] + + loss = tf.keras.losses.MSE + optimizer = MultiOptimzer(opt_layer_pairs) + + model.compile(optimizer=optimizer, loss=loss) + + # serialize whole model including optimizer, clear the session, then reload the whole model. + if serialize: + model.save("test", save_format="tf") + tf.keras.backend.clear_session() + model = tf.keras.models.load_model("test") + + model.fit(x, y, batch_size=8, epochs=10) + + weights_after_train = ( + model.layers[0].weights[0].numpy(), + model.layers[1].weights[0].numpy(), + ) + + with np.testing.assert_raises(AssertionError): + # expect weights to be different for layer 1 + test_utils.assert_allclose_according_to_type( + weights_before_train[0], weights_after_train[0] + ) + + # expect weights to be same for layer 2 + test_utils.assert_allclose_according_to_type( + weights_before_train[1], weights_after_train[1] + ) + + +def test_serialization(): + + model = tf.keras.Sequential( + [tf.keras.Input(shape=[1]), tf.keras.layers.Dense(1), tf.keras.layers.Dense(1)] + ) + + opt1 = tf.keras.optimizers.Adam(learning_rate=1e-3) + opt2 = tf.keras.optimizers.SGD(learning_rate=0) + + opt_layer_pairs = [(opt1, model.layers[0]), (opt2, model.layers[1])] + + optimizer = MultiOptimzer(opt_layer_pairs) + config = tf.keras.optimizers.serialize(optimizer) + + new_optimizer = tf.keras.optimizers.deserialize(config) + assert new_optimizer.get_config() == optimizer.get_config() From 816aa2343a2602fe3bf91b078c9b34c347444253 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:43:52 +0500 Subject: [PATCH 066/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index 9e6f6ea648..a123a9f224 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -38,7 +38,7 @@ class GeometricMean(Metric): >>> metric = tfa.metrics.GeometricMean() >>> metric.update_state([1, 3, 5, 7, 9]) >>> metric.result().numpy() - >>> # 3.9362833 + 3.9362833 """ From ba10725fdd2f41b2175bce737a8104c328e37616 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:49:59 +0500 Subject: [PATCH 067/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index c4a15911a0..e6733781bd 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> metric = hamming_distance(actuals, predictions) - >>> #print('Hamming distance: ', metric.numpy()) + >>> print('Hamming distance: ', metric.numpy()) """ result = tf.not_equal(actuals, predictions) @@ -92,6 +92,7 @@ def hamming_loss_fn( ... dtype=tf.float32) >>> hl.update_state(actuals, predictions) + tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 @@ -103,7 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + >>> print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From ea667601495923c6b980452ab4a2ec8b13bdfa44 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:53:39 +0500 Subject: [PATCH 068/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 612aec63e3..86518c41a1 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,16 +45,17 @@ class CohenKappa(Metric): >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds) + tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.numpy()) - >>> # Final result: 0.61904764 + >>>print('Final result: ', result.numpy()) + Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.numpy()) - >>> # Final result: 0.37209308 + >>> print('Final result: ', result.numpy()) + Final result: 0.37209308 Usage with tf.keras API: From d99502f703afd59d2eb4def93569e53488465b07 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:58:19 +0500 Subject: [PATCH 069/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index cc5c9f4c15..c1ba9e2f96 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,9 +51,8 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> metric.update_state(y_true = actuals, y_pred = preds) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', result.numpy()) - >>> # Matthews correlation coefficient is : -0.33333334 + >>> print('Matthews correlation coefficient is:', result.numpy()) + Matthews correlation coefficient is : -0.33333334 """ From 27a975ea58b93531c225b2b7603f3a47077bc231 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:08:10 +0500 Subject: [PATCH 070/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index c4ec7d5c90..8a067f7bb9 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -54,7 +54,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> # print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -66,7 +66,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> # print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From adc06a79b06f52ea9530a567506b649c2a896404 Mon Sep 17 00:00:00 2001 From: hongy <34040987+hyang0129@users.noreply.github.com> Date: Mon, 14 Sep 2020 15:11:29 -0400 Subject: [PATCH 071/259] fixed typo in multioptimizer class name and added code owners (#2164) --- .github/CODEOWNERS | 2 ++ tensorflow_addons/optimizers/__init__.py | 2 +- .../optimizers/discriminative_layer_training.py | 6 +++--- .../optimizers/tests/discriminative_layer_training_test.py | 6 +++--- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 879f140855..a016b7a3ec 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -146,6 +146,8 @@ /tensorflow_addons/optimizers/tests/conditional_gradient_test.py @pkan2 @lokhande-vishnu /tensorflow_addons/optimizers/cyclical_learning_rate.py @raphaelmeudec /tensorflow_addons/optimizers/tests/cyclical_learning_rate_test.py @raphaelmeudec +/tensorflow_addons/optimizers/discriminative_layer_training.py @hyang0129 +/tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py @hyang0129 /tensorflow_addons/optimizers/lamb.py @junjiek /tensorflow_addons/optimizers/tests/lamb_test.py @junjiek /tensorflow_addons/optimizers/lazy_adam.py @ssaishruthi diff --git a/tensorflow_addons/optimizers/__init__.py b/tensorflow_addons/optimizers/__init__.py index 5e1d90f2e4..a565533632 100644 --- a/tensorflow_addons/optimizers/__init__.py +++ b/tensorflow_addons/optimizers/__init__.py @@ -27,7 +27,7 @@ ExponentialCyclicalLearningRate, ) from tensorflow_addons.optimizers.discriminative_layer_training import ( - MultiOptimzer, + MultiOptimizer, ) from tensorflow_addons.optimizers.lamb import LAMB from tensorflow_addons.optimizers.lazy_adam import LazyAdam diff --git a/tensorflow_addons/optimizers/discriminative_layer_training.py b/tensorflow_addons/optimizers/discriminative_layer_training.py index e56387d682..494c29d365 100644 --- a/tensorflow_addons/optimizers/discriminative_layer_training.py +++ b/tensorflow_addons/optimizers/discriminative_layer_training.py @@ -21,7 +21,7 @@ @tf.keras.utils.register_keras_serializable(package="Addons") -class MultiOptimzer(tf.keras.optimizers.Optimizer): +class MultiOptimizer(tf.keras.optimizers.Optimizer): """Multi Optimizer Wrapper for Discriminative Layer Training. Creates a wrapper around a set of instantiated optimizer layer pairs. Generally useful for transfer learning @@ -84,7 +84,7 @@ def __init__( **kwargs ): - super(MultiOptimzer, self).__init__(name, **kwargs) + super(MultiOptimizer, self).__init__(name, **kwargs) if optimizer_specs is None and optimizers_and_layers is not None: self.optimizer_specs = [ @@ -126,7 +126,7 @@ def apply_gradients(self, grads_and_vars, name=None, **kwargs): ) def get_config(self): - config = super(MultiOptimzer, self).get_config() + config = super(MultiOptimizer, self).get_config() config.update({"optimizer_specs": self.optimizer_specs}) return config diff --git a/tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py b/tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py index 8a93cfb903..08a096b840 100644 --- a/tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py +++ b/tensorflow_addons/optimizers/tests/discriminative_layer_training_test.py @@ -18,7 +18,7 @@ import numpy as np import tensorflow as tf -from tensorflow_addons.optimizers.discriminative_layer_training import MultiOptimzer +from tensorflow_addons.optimizers.discriminative_layer_training import MultiOptimizer from tensorflow_addons.utils import test_utils @@ -66,7 +66,7 @@ def test_fit_layer_optimizer(dtype, device, serialize): opt_layer_pairs = [(opt1, model.layers[0]), (opt2, model.layers[1])] loss = tf.keras.losses.MSE - optimizer = MultiOptimzer(opt_layer_pairs) + optimizer = MultiOptimizer(opt_layer_pairs) model.compile(optimizer=optimizer, loss=loss) @@ -106,7 +106,7 @@ def test_serialization(): opt_layer_pairs = [(opt1, model.layers[0]), (opt2, model.layers[1])] - optimizer = MultiOptimzer(opt_layer_pairs) + optimizer = MultiOptimizer(opt_layer_pairs) config = tf.keras.optimizers.serialize(optimizer) new_optimizer = tf.keras.optimizers.deserialize(config) From 4fc2ced6ea3ab5f0895f21e2b50f7f1a2bbd21ba Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:44:49 +0500 Subject: [PATCH 072/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 8a067f7bb9..be3bf82a2e 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,8 +55,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] + Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -67,8 +66,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] """ From f6575a7f016d46aa414aba682a50bd1d0eb63625 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:45:25 +0500 Subject: [PATCH 073/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index c1ba9e2f96..68cc2ff9e9 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,8 +51,8 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> metric.update_state(y_true = actuals, y_pred = preds) >>> result = metric(y_true = actuals, y_pred = preds) - >>> print('Matthews correlation coefficient is:', result.numpy()) - Matthews correlation coefficient is : -0.33333334 + >>> print('Matthews correlation coefficient is :', result.numpy()) + Matthews correlation coefficient is : [-0.33333334] """ From 36cd439be20fad79e646e4f88c9ed24ef9613284 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:53:07 +0500 Subject: [PATCH 074/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index e6733781bd..4e8e914f44 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -44,7 +44,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> metric = hamming_distance(actuals, predictions) >>> print('Hamming distance: ', metric.numpy()) - + Hamming distance: 0.3 """ result = tf.not_equal(actuals, predictions) not_eq = tf.reduce_sum(tf.cast(result, tf.float32)) @@ -94,8 +94,8 @@ def hamming_loss_fn( >>> hl.update_state(actuals, predictions) tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result - >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 - + >>> print('Hamming loss: ', hl.result().numpy()) + Hamming loss: 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) >>> actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], @@ -104,8 +104,8 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> print('Hamming loss: ', hl.result().numpy()) # 0.16666667 - + >>> print('Hamming loss: ', hl.result().numpy()) + Hamming loss: 0.16666667 """ if mode not in ["multiclass", "multilabel"]: raise TypeError("mode must be either multiclass or multilabel]") From 51f2b5f48cc7d40b7d9c57cfd87e34a7620ee179 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:57:41 +0500 Subject: [PATCH 075/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 4e8e914f44..21bb0c3ccb 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -94,7 +94,7 @@ def hamming_loss_fn( >>> hl.update_state(actuals, predictions) tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result - >>> print('Hamming loss: ', hl.result().numpy()) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -104,7 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> print('Hamming loss: ', hl.result().numpy()) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From 37f40a727b38f896ca3aab10555ebf5807714ab0 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:05:16 +0500 Subject: [PATCH 076/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 86518c41a1..e42883a4c7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -47,7 +47,7 @@ class CohenKappa(Metric): >>> metric.update_state(y_true = actuals, y_pred = preds) tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) >>> result = metric(y_true = actuals, y_pred = preds) - >>>print('Final result: ', result.numpy()) + >>> print('Final result: ', result.numpy()) Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. From 84a3ce66053f498e46fcfec57eb2b62f1801dca9 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:08:40 +0500 Subject: [PATCH 077/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 21bb0c3ccb..cffde3014e 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -92,7 +92,7 @@ def hamming_loss_fn( ... dtype=tf.float32) >>> hl.update_state(actuals, predictions) - tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 + >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.25 From 84da148416f0618163435e141e02a648ac7bad09 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:27:24 +0500 Subject: [PATCH 078/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index e42883a4c7..00faf0dc59 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,7 +45,12 @@ class CohenKappa(Metric): >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds) - tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) + >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) Final result: 0.61904764 From 1b79cc4231c2d455aeae331d3118f404332f3ab3 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:29:14 +0500 Subject: [PATCH 079/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index cffde3014e..63c40f26e6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -104,6 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.16666667 """ From bcc75fa813c4646349f64fb4f533f9c5831a427e Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:35:27 +0500 Subject: [PATCH 080/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index be3bf82a2e..f26ff0f7a3 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,7 +55,14 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] + Confusion matrix: [[[2. 0.] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -66,7 +73,14 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + Confusion matrix: [[[2. 0.] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ From e6425838303cc23ee93e2a2693fbfe8e1aba650d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:59:54 +0500 Subject: [PATCH 081/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 00faf0dc59..5aaa800a89 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -58,6 +58,12 @@ class CohenKappa(Metric): >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) + >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) Final result: 0.37209308 From 7b8c9bc94c7de2718ed71f5aab098d2ccd965379 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:14:01 +0500 Subject: [PATCH 082/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index f26ff0f7a3..693ce6a857 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,14 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] - + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From c06c27bdcbabd42cdbd49b8a517d332ff32aded8 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:26:36 +0500 Subject: [PATCH 083/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 5aaa800a89..c3d459563c 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -66,7 +66,7 @@ class CohenKappa(Metric): [ 0., 0., 2., 0., 7.]], dtype=float32)> >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) - Final result: 0.37209308 + Final result: 0.42080373 Usage with tf.keras API: From dbd3261a966bc4941c38329b9af08eb29abdfd42 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:29:20 +0500 Subject: [PATCH 084/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 693ce6a857..357a4aab67 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,12 +56,12 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [0. 2.]] - - [[0. 2.] + [[2. 0.] + [0. 2.]] + + [[0. 2.] [2. 0.]]] >>> # if multiclass input is provided @@ -74,12 +74,12 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] - [[2. 0.] - [2. 0.]] - - [[2. 2.] + [[2. 2.] [0. 0.]]] """ From 2ddb74d755de2d59ab1cecef20db2971dabbb099 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 08:29:29 +0500 Subject: [PATCH 085/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 357a4aab67..9ca016fc9e 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From abe85612ed22b5961225db10edb59a6228856e56 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 08:32:01 +0500 Subject: [PATCH 086/259] unified diff --- .../metrics/multilabel_confusion_matrix.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 9ca016fc9e..aa7e131d1d 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From cab083736baa75a489f11719f0d26457b6756441 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 09:05:46 +0500 Subject: [PATCH 087/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index aa7e131d1d..fe25dfb4e7 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,7 +56,7 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] [[2. 0.] [0. 2.]] @@ -74,7 +74,7 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] [[2. 0.] [2. 0.]] From 8320f163b347853b746dcecc048b61ae1145eab6 Mon Sep 17 00:00:00 2001 From: Harshith MohanKumar <47203161+Harsh188@users.noreply.github.com> Date: Tue, 15 Sep 2020 22:42:22 +0530 Subject: [PATCH 088/259] Rnn testable doctests (#2147) * added doctests layer_norm_simple * added doctests peephole_lstm_cell.py * minor changes * including output for example * Updated examples to be descriptive and standardized --- tensorflow_addons/rnn/esn_cell.py | 12 ++++++ tensorflow_addons/rnn/layer_norm_lstm_cell.py | 13 ++++++ .../rnn/layer_norm_simple_rnn_cell.py | 41 +++++++++++-------- tensorflow_addons/rnn/nas_cell.py | 13 ++++++ tensorflow_addons/rnn/peephole_lstm_cell.py | 18 ++++---- 5 files changed, 71 insertions(+), 26 deletions(-) diff --git a/tensorflow_addons/rnn/esn_cell.py b/tensorflow_addons/rnn/esn_cell.py index d36be9f369..e85f638f43 100644 --- a/tensorflow_addons/rnn/esn_cell.py +++ b/tensorflow_addons/rnn/esn_cell.py @@ -32,6 +32,18 @@ class ESNCell(keras.layers.AbstractRNNCell): "The "echo state" approach to analysing and training recurrent neural networks". GMD Report148, German National Research Center for Information Technology, 2001. https://www.researchgate.net/publication/215385037 + + Example: + + >>> inputs = np.random.random([30,23,9]).astype(np.float32) + >>> ESNCell = tfa.rnn.ESNCell(4) + >>> rnn = tf.keras.layers.RNN(ESNCell, return_sequences=True, return_state=True) + >>> outputs, memory_state = rnn(inputs) + >>> outputs.shape + TensorShape([30, 23, 4]) + >>> memory_state.shape + TensorShape([30, 4]) + Arguments: units: Positive integer, dimensionality in the reservoir. connectivity: Float between 0 and 1. diff --git a/tensorflow_addons/rnn/layer_norm_lstm_cell.py b/tensorflow_addons/rnn/layer_norm_lstm_cell.py index 3a3a359663..adff626673 100644 --- a/tensorflow_addons/rnn/layer_norm_lstm_cell.py +++ b/tensorflow_addons/rnn/layer_norm_lstm_cell.py @@ -46,6 +46,19 @@ class LayerNormLSTMCell(keras.layers.LSTMCell): "Recurrent Dropout without Memory Loss" Stanislau Semeniuta, Aliaksei Severyn, Erhardt Barth. + + Example: + + >>> inputs = np.random.random([30,23,9]).astype(np.float32) + >>> lnLSTMCell = tfa.rnn.LayerNormLSTMCell(4) + >>> rnn = tf.keras.layers.RNN(lnLSTMCell, return_sequences=True, return_state=True) + >>> outputs, memory_state, carry_state = rnn(inputs) + >>> outputs.shape + TensorShape([30, 23, 4]) + >>> memory_state.shape + TensorShape([30, 4]) + >>> carry_state.shape + TensorShape([30, 4]) """ @typechecked diff --git a/tensorflow_addons/rnn/layer_norm_simple_rnn_cell.py b/tensorflow_addons/rnn/layer_norm_simple_rnn_cell.py index f9562c6329..cc5c2b6d70 100644 --- a/tensorflow_addons/rnn/layer_norm_simple_rnn_cell.py +++ b/tensorflow_addons/rnn/layer_norm_simple_rnn_cell.py @@ -37,6 +37,17 @@ class LayerNormSimpleRNNCell(keras.layers.SimpleRNNCell): "Layer Normalization." ArXiv:1607.06450 [Cs, Stat], July 21, 2016. http://arxiv.org/abs/1607.06450 + Example: + + >>> inputs = np.random.random([30,23,9]).astype(np.float32) + >>> lnsRNNCell = tfa.rnn.LayerNormSimpleRNNCell(4) + >>> rnn = tf.keras.layers.RNN(lnsRNNCell, return_sequences=True, return_state=True) + >>> outputs, memory_state = rnn(inputs) + >>> outputs.shape + TensorShape([30, 23, 4]) + >>> memory_state.shape + TensorShape([30, 4]) + Arguments: units: Positive integer, dimensionality of the output space. activation: Activation function to use. @@ -89,25 +100,19 @@ class LayerNormSimpleRNNCell(keras.layers.SimpleRNNCell): Examples: - ```python - import numpy as np - import tensorflow.keras as keras - import tensorflow_addons as tfa - - inputs = np.random.random([32, 10, 8]).astype(np.float32) - rnn = keras.layers.RNN(tfa.rnn.LayerNormSimpleRNNCell(4)) - - output = rnn(inputs) # The output has shape `[32, 4]`. - - rnn = keras.layers.RNN( - tfa.rnn.LayerNormSimpleRNNCell(4), - return_sequences=True, - return_state=True) + >>> inputs = np.random.random([32, 10, 8]).astype(np.float32) + >>> rnn = tf.keras.layers.RNN(tfa.rnn.LayerNormSimpleRNNCell(4)) + >>> output = rnn(inputs) # The output has shape `[32, 4]`. + >>> rnn = tf.keras.layers.RNN( + ... tfa.rnn.LayerNormSimpleRNNCell(4), + ... return_sequences=True, + ... return_state=True) + >>> whole_sequence_output, final_state = rnn(inputs) + >>> whole_sequence_output + + >>> final_state + - # whole_sequence_output has shape `[32, 10, 4]`. - # final_state has shape `[32, 4]`. - whole_sequence_output, final_state = rnn(inputs) - ``` """ @typechecked diff --git a/tensorflow_addons/rnn/nas_cell.py b/tensorflow_addons/rnn/nas_cell.py index 05054a0233..6b6686ba4b 100644 --- a/tensorflow_addons/rnn/nas_cell.py +++ b/tensorflow_addons/rnn/nas_cell.py @@ -38,6 +38,19 @@ class NASCell(keras.layers.AbstractRNNCell): "Neural Architecture Search with Reinforcement Learning" Proc. ICLR 2017. The class uses an optional projection layer. + + Example: + + >>> inputs = np.random.random([30,23,9]).astype(np.float32) + >>> NASCell = tfa.rnn.NASCell(4) + >>> rnn = tf.keras.layers.RNN(NASCell, return_sequences=True, return_state=True) + >>> outputs, memory_state, carry_state = rnn(inputs) + >>> outputs.shape + TensorShape([30, 23, 4]) + >>> memory_state.shape + TensorShape([30, 4]) + >>> carry_state.shape + TensorShape([30, 4]) """ # NAS cell's architecture base. diff --git a/tensorflow_addons/rnn/peephole_lstm_cell.py b/tensorflow_addons/rnn/peephole_lstm_cell.py index 1f791d53d8..658db084f4 100644 --- a/tensorflow_addons/rnn/peephole_lstm_cell.py +++ b/tensorflow_addons/rnn/peephole_lstm_cell.py @@ -39,14 +39,16 @@ class PeepholeLSTMCell(tf.keras.layers.LSTMCell): Example: - ```python - # Create 2 PeepholeLSTMCells - peephole_lstm_cells = [PeepholeLSTMCell(size) for size in [128, 256]] - # Create a layer composed sequentially of the peephole LSTM cells. - layer = RNN(peephole_lstm_cells) - input = keras.Input((timesteps, input_dim)) - output = layer(input) - ``` + >>> inputs = np.random.random([30,23,9]).astype(np.float32) + >>> LSTMCell = tfa.rnn.PeepholeLSTMCell(4) + >>> rnn = tf.keras.layers.RNN(LSTMCell, return_sequences=True, return_state=True) + >>> outputs, memory_state, carry_state = rnn(inputs) + >>> outputs.shape + TensorShape([30, 23, 4]) + >>> memory_state.shape + TensorShape([30, 4]) + >>> carry_state.shape + TensorShape([30, 4]) """ def build(self, input_shape): From 1c3c072a276613019662f3f6eba9dee9b4d5bd1f Mon Sep 17 00:00:00 2001 From: Leon Shams <52867365+LeonShams@users.noreply.github.com> Date: Tue, 15 Sep 2020 10:14:04 -0700 Subject: [PATCH 089/259] Added support for noisy dense layers. (#2099) * Create noisy_dense.py * Create noisy_dense_test.py * Update __init__.py * Fix minor typo * Update noisy_dense_test.py * Update comments * Update comments * Update noisy_dense.py * fix typo * Update noisy_dense.py * Update noisy_dense_test.py * Fix compliance issues * Fix compliance issues * Update comments * Fix typo * Update CODEOWNERS * Update CODEOWNERS * add use bias to config * Update noisy_dense.py * Update CODEOWNERS * Revert "Update CODEOWNERS" This reverts commit 82e979f37cef4580d93894ed3822f3611f20e5fd. * Update noisy_dense.py * Update noisy_dense.py * Update noisy_dense.py * Update noisy_dense.py * Revert "Update CODEOWNERS" This reverts commit 840ab1ca1a0267e88700db26b12e29d469fd6194. * Revert "Revert "Update CODEOWNERS"" This reverts commit 7852e62a871d8b670953c2d7f4d106d52fc1d594. * Update noisy_dense.py * Code reformatted with updated black * Update noisy_dense.py * Update noisy_dense.py * Update noisy_dense.py * Added support for manual noise reset * support for noise removal * tests for noise removal * use typecheck and remove unicode, * fix typo and code cleanup * control noise removal through call * Inherit from Dense instead of Layer * Added missing comment * Documentation and test improvement * fix typo * minor formatting changes * minor formatting fix Co-authored-by: schaall <52867365+schaall@users.noreply.github.com> --- .github/CODEOWNERS | 2 + tensorflow_addons/layers/__init__.py | 1 + tensorflow_addons/layers/noisy_dense.py | 264 ++++++++++++++++++ .../layers/tests/noisy_dense_test.py | 141 ++++++++++ 4 files changed, 408 insertions(+) create mode 100644 tensorflow_addons/layers/noisy_dense.py create mode 100644 tensorflow_addons/layers/tests/noisy_dense_test.py diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index a016b7a3ec..89b3c18f37 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -105,6 +105,8 @@ /tensorflow_addons/layers/tests/esn_test.py @pedrolarben /tensorflow_addons/layers/snake.py @failure-to-thrive /tensorflow_addons/layers/tests/snake_test.py @failure-to-thrive +/tensorflow_addons/layers/noisy_dense.py @leonshams +/tensorflow_addons/layers/tests/noisy_dense_test.py @leonshams /tensorflow_addons/losses/contrastive.py @windqaq /tensorflow_addons/losses/tests/contrastive_test.py @windqaq diff --git a/tensorflow_addons/layers/__init__.py b/tensorflow_addons/layers/__init__.py index 594d025a9f..f8a0d9a11a 100644 --- a/tensorflow_addons/layers/__init__.py +++ b/tensorflow_addons/layers/__init__.py @@ -38,3 +38,4 @@ from tensorflow_addons.layers.tlu import TLU from tensorflow_addons.layers.wrappers import WeightNormalization from tensorflow_addons.layers.esn import ESN +from tensorflow_addons.layers.noisy_dense import NoisyDense diff --git a/tensorflow_addons/layers/noisy_dense.py b/tensorflow_addons/layers/noisy_dense.py new file mode 100644 index 0000000000..647b28db7d --- /dev/null +++ b/tensorflow_addons/layers/noisy_dense.py @@ -0,0 +1,264 @@ +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== + +import tensorflow as tf +from tensorflow.keras import ( + activations, + initializers, + regularizers, + constraints, +) +from tensorflow.keras import backend as K +from tensorflow.keras.layers import InputSpec +from typeguard import typechecked + +from tensorflow_addons.utils import types + + +def _scale_noise(x): + return tf.sign(x) * tf.sqrt(tf.abs(x)) + + +@tf.keras.utils.register_keras_serializable(package="Addons") +class NoisyDense(tf.keras.layers.Dense): + r"""Noisy dense layer that injects random noise to the weights of dense layer. + + Noisy dense layers are fully connected layers whose weights and biases are + augmented by factorised Gaussian noise. The factorised Gaussian noise is + controlled through gradient descent by a second weights layer. + + A `NoisyDense` layer implements the operation: + $$ + \mathrm{NoisyDense}(x) = + \mathrm{activation}(\mathrm{dot}(x, \mu + (\sigma \cdot \epsilon)) + + \mathrm{bias}) + $$ + where $\mu$ is the standard weights layer, $\epsilon$ is the factorised + Gaussian noise, and $\sigma$ is a second weights layer which controls + $\epsilon$. + + Note: bias only added if `use_bias` is `True`. + + Example: + + >>> # Create a `Sequential` model and add a NoisyDense + >>> # layer as the first layer. + >>> model = tf.keras.models.Sequential() + >>> model.add(tf.keras.Input(shape=(16,))) + >>> model.add(NoisyDense(32, activation='relu')) + >>> # Now the model will take as input arrays of shape (None, 16) + >>> # and output arrays of shape (None, 32). + >>> # Note that after the first layer, you don't need to specify + >>> # the size of the input anymore: + >>> model.add(NoisyDense(32)) + >>> model.output_shape + (None, 32) + + Arguments: + units: Positive integer, dimensionality of the output space. + sigma: A float between 0-1 used as a standard deviation figure and is + applied to the gaussian noise layer (`sigma_kernel` and `sigma_bias`). + activation: Activation function to use. + If you don't specify anything, no activation is applied + (ie. "linear" activation: `a(x) = x`). + use_bias: Boolean, whether the layer uses a bias vector. + kernel_regularizer: Regularizer function applied to + the `kernel` weights matrix. + bias_regularizer: Regularizer function applied to the bias vector. + activity_regularizer: Regularizer function applied to + the output of the layer (its "activation"). + kernel_constraint: Constraint function applied to + the `kernel` weights matrix. + bias_constraint: Constraint function applied to the bias vector. + + Input shape: + N-D tensor with shape: `(batch_size, ..., input_dim)`. + The most common situation would be + a 2D input with shape `(batch_size, input_dim)`. + + Output shape: + N-D tensor with shape: `(batch_size, ..., units)`. + For instance, for a 2D input with shape `(batch_size, input_dim)`, + the output would have shape `(batch_size, units)`. + + References: + - [Noisy Networks for Explanation](https://arxiv.org/pdf/1706.10295.pdf) + """ + + @typechecked + def __init__( + self, + units: int, + sigma: float = 0.5, + activation: types.Activation = None, + use_bias: bool = True, + kernel_regularizer: types.Regularizer = None, + bias_regularizer: types.Regularizer = None, + activity_regularizer: types.Regularizer = None, + kernel_constraint: types.Constraint = None, + bias_constraint: types.Constraint = None, + **kwargs + ): + super().__init__( + units=units, + activation=activation, + use_bias=use_bias, + kernel_regularizer=kernel_regularizer, + bias_regularizer=bias_regularizer, + activity_regularizer=activity_regularizer, + kernel_constraint=kernel_constraint, + bias_constraint=bias_constraint, + **kwargs, + ) + delattr(self, "kernel_initializer") + delattr(self, "bias_initializer") + self.sigma = sigma + + def build(self, input_shape): + # Make sure dtype is correct + dtype = tf.dtypes.as_dtype(self.dtype or K.floatx()) + if not (dtype.is_floating or dtype.is_complex): + raise TypeError( + "Unable to build `Dense` layer with non-floating point " + "dtype %s" % (dtype,) + ) + + input_shape = tf.TensorShape(input_shape) + self.last_dim = tf.compat.dimension_value(input_shape[-1]) + sqrt_dim = self.last_dim ** (1 / 2) + if self.last_dim is None: + raise ValueError( + "The last dimension of the inputs to `Dense` " + "should be defined. Found `None`." + ) + self.input_spec = InputSpec(min_ndim=2, axes={-1: self.last_dim}) + + sigma_init = initializers.Constant(value=self.sigma / sqrt_dim) + mu_init = initializers.RandomUniform(minval=-1 / sqrt_dim, maxval=1 / sqrt_dim) + + # Learnable parameters + self.sigma_kernel = self.add_weight( + "sigma_kernel", + shape=[self.last_dim, self.units], + initializer=sigma_init, + regularizer=self.kernel_regularizer, + constraint=self.kernel_constraint, + dtype=self.dtype, + trainable=True, + ) + + self.mu_kernel = self.add_weight( + "mu_kernel", + shape=[self.last_dim, self.units], + initializer=mu_init, + regularizer=self.kernel_regularizer, + constraint=self.kernel_constraint, + dtype=self.dtype, + trainable=True, + ) + + if self.use_bias: + self.sigma_bias = self.add_weight( + "sigma_bias", + shape=[ + self.units, + ], + initializer=sigma_init, + regularizer=self.bias_regularizer, + constraint=self.bias_constraint, + dtype=self.dtype, + trainable=True, + ) + + self.mu_bias = self.add_weight( + "mu_bias", + shape=[ + self.units, + ], + initializer=mu_init, + regularizer=self.bias_regularizer, + constraint=self.bias_constraint, + dtype=self.dtype, + trainable=True, + ) + else: + self.sigma_bias = None + self.mu_bias = None + self._reset_noise() + self.built = True + + @property + def kernel(self): + return self.mu_kernel + (self.sigma_kernel * self.eps_kernel) + + @property + def bias(self): + if self.use_bias: + return self.mu_bias + (self.sigma_bias * self.eps_bias) + + def _reset_noise(self): + """Create the factorised Gaussian noise.""" + + dtype = self._compute_dtype_object + + # Generate random noise + eps_i = tf.random.normal([self.last_dim, self.units], dtype=dtype) + eps_j = tf.random.normal( + [ + self.units, + ], + dtype=dtype, + ) + + # Scale the random noise + self.eps_kernel = _scale_noise(eps_i) * _scale_noise(eps_j) + self.eps_bias = _scale_noise(eps_j) + + def _remove_noise(self): + """Remove the factorised Gaussian noise.""" + + dtype = self._compute_dtype_object + self.eps_kernel = tf.zeros([self.last_dim, self.units], dtype=dtype) + self.eps_bias = tf.zeros([self.units], dtype=dtype) + + def call(self, inputs, reset_noise=True, remove_noise=False): + # Generate fixed parameters added as the noise + if remove_noise: + self._remove_noise() + elif reset_noise: + self._reset_noise() + + # TODO(WindQAQ): Replace this with `dense()` once public. + return super().call(inputs) + + def get_config(self): + # TODO(WindQAQ): Get rid of this hacky way. + config = super(tf.keras.layers.Dense, self).get_config() + config.update( + { + "units": self.units, + "sigma": self.sigma, + "activation": activations.serialize(self.activation), + "use_bias": self.use_bias, + "kernel_regularizer": regularizers.serialize(self.kernel_regularizer), + "bias_regularizer": regularizers.serialize(self.bias_regularizer), + "activity_regularizer": regularizers.serialize( + self.activity_regularizer + ), + "kernel_constraint": constraints.serialize(self.kernel_constraint), + "bias_constraint": constraints.serialize(self.bias_constraint), + } + ) + return config diff --git a/tensorflow_addons/layers/tests/noisy_dense_test.py b/tensorflow_addons/layers/tests/noisy_dense_test.py new file mode 100644 index 0000000000..9f76307518 --- /dev/null +++ b/tensorflow_addons/layers/tests/noisy_dense_test.py @@ -0,0 +1,141 @@ +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Tests NoisyDense layer.""" + + +import pytest +import numpy as np + +import tensorflow as tf +from tensorflow import keras +from tensorflow.keras.mixed_precision.experimental import Policy + +from tensorflow_addons.utils import test_utils +from tensorflow_addons.layers.noisy_dense import NoisyDense + + +@pytest.mark.parametrize( + "input_shape", [(3, 2), (3, 4, 2), (None, None, 2), (3, 4, 5, 2)] +) +def test_noisy_dense(input_shape): + test_utils.layer_test(NoisyDense, kwargs={"units": 3}, input_shape=input_shape) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +@pytest.mark.parametrize("dtype", ["float16", "float32", "float64"]) +def test_noisy_dense_dtype(dtype): + inputs = tf.convert_to_tensor( + np.random.randint(low=0, high=7, size=(2, 2)), dtype=dtype + ) + layer = NoisyDense(5, dtype=dtype, name="noisy_dense_" + dtype) + outputs = layer(inputs) + np.testing.assert_array_equal(outputs.dtype, dtype) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_noisy_dense_with_policy(): + inputs = tf.convert_to_tensor(np.random.randint(low=0, high=7, size=(2, 2))) + layer = NoisyDense(5, dtype=Policy("mixed_float16"), name="noisy_dense_policy") + outputs = layer(inputs) + output_signature = layer.compute_output_signature( + tf.TensorSpec(dtype="float16", shape=(2, 2)) + ) + np.testing.assert_array_equal(output_signature.dtype, tf.dtypes.float16) + np.testing.assert_array_equal(output_signature.shape, (2, 5)) + np.testing.assert_array_equal(outputs.dtype, "float16") + np.testing.assert_array_equal(layer.mu_kernel.dtype, "float32") + np.testing.assert_array_equal(layer.sigma_kernel.dtype, "float32") + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_noisy_dense_regularization(): + layer = NoisyDense( + 3, + kernel_regularizer=keras.regularizers.l1(0.01), + bias_regularizer="l1", + activity_regularizer="l2", + name="noisy_dense_reg", + ) + layer(keras.backend.variable(np.ones((2, 4)))) + np.testing.assert_array_equal(5, len(layer.losses)) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_noisy_dense_constraints(): + k_constraint = keras.constraints.max_norm(0.01) + b_constraint = keras.constraints.max_norm(0.01) + layer = NoisyDense( + 3, + kernel_constraint=k_constraint, + bias_constraint=b_constraint, + name="noisy_dense_constriants", + ) + layer(keras.backend.variable(np.ones((2, 4)))) + np.testing.assert_array_equal(layer.mu_kernel.constraint, k_constraint) + np.testing.assert_array_equal(layer.sigma_kernel.constraint, k_constraint) + np.testing.assert_array_equal(layer.mu_bias.constraint, b_constraint) + np.testing.assert_array_equal(layer.sigma_bias.constraint, b_constraint) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_noisy_dense_automatic_reset_noise(): + inputs = tf.convert_to_tensor(np.random.randint(low=0, high=7, size=(2, 2))) + layer = NoisyDense(5, name="noise_dense_auto_reset_noise") + layer(inputs) + initial_eps_kernel = layer.eps_kernel + initial_eps_bias = layer.eps_bias + layer(inputs) + new_eps_kernel = layer.eps_kernel + new_eps_bias = layer.eps_bias + np.testing.assert_raises( + AssertionError, + np.testing.assert_array_equal, + initial_eps_kernel, + new_eps_kernel, + ) + np.testing.assert_raises( + AssertionError, + np.testing.assert_array_equal, + initial_eps_bias, + new_eps_bias, + ) + + +@pytest.mark.usefixtures("maybe_run_functions_eagerly") +def test_noisy_dense_remove_noise(): + inputs = tf.convert_to_tensor(np.random.randint(low=0, high=7, size=(2, 2))) + layer = NoisyDense(5, name="noise_dense_manual_reset_noise") + layer(inputs) + initial_eps_kernel = layer.eps_kernel + initial_eps_bias = layer.eps_bias + layer(inputs, reset_noise=False, remove_noise=True) + new_eps_kernel = layer.eps_kernel + new_eps_bias = layer.eps_bias + kernel_zeros = tf.zeros(initial_eps_kernel.shape, dtype=initial_eps_kernel.dtype) + bias_zeros = tf.zeros(initial_eps_bias.shape, dtype=initial_eps_kernel.dtype) + np.testing.assert_raises( + AssertionError, + np.testing.assert_array_equal, + initial_eps_kernel, + new_eps_kernel, + ) + np.testing.assert_raises( + AssertionError, + np.testing.assert_array_equal, + initial_eps_bias, + new_eps_bias, + ) + np.testing.assert_array_equal(kernel_zeros, new_eps_kernel) + np.testing.assert_array_equal(bias_zeros, new_eps_bias) From dbcd5aaef6da32bcd24040c55a49cef1538ef765 Mon Sep 17 00:00:00 2001 From: MHStadler Date: Thu, 17 Sep 2020 00:51:17 +0100 Subject: [PATCH 090/259] Added stochastic depth layer (#2154) * Added stochastic depth layer * Fixed code style and added missing __init__ entry * Fixed tests and style * Fixed code style * Updated CODEOWNERS * Added codeowners for tests * Changes after code review * Test and formatting fixes * Fixed doc string * Added mixed precision test * Further code review changes * Code review changes --- .github/CODEOWNERS | 2 + tensorflow_addons/layers/__init__.py | 1 + tensorflow_addons/layers/stochastic_depth.py | 88 +++++++++++++++++++ .../layers/tests/stochastic_depth_test.py | 58 ++++++++++++ 4 files changed, 149 insertions(+) create mode 100644 tensorflow_addons/layers/stochastic_depth.py create mode 100644 tensorflow_addons/layers/tests/stochastic_depth_test.py diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 89b3c18f37..bde99aec1e 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -105,6 +105,8 @@ /tensorflow_addons/layers/tests/esn_test.py @pedrolarben /tensorflow_addons/layers/snake.py @failure-to-thrive /tensorflow_addons/layers/tests/snake_test.py @failure-to-thrive +/tensorflow_addons/layers/stochastic_depth.py @mhstadler @windqaq +/tensorflow_addons/layers/tests/stochastic_depth_test.py @mhstadler @windqaq /tensorflow_addons/layers/noisy_dense.py @leonshams /tensorflow_addons/layers/tests/noisy_dense_test.py @leonshams diff --git a/tensorflow_addons/layers/__init__.py b/tensorflow_addons/layers/__init__.py index f8a0d9a11a..7c89f95a06 100644 --- a/tensorflow_addons/layers/__init__.py +++ b/tensorflow_addons/layers/__init__.py @@ -38,4 +38,5 @@ from tensorflow_addons.layers.tlu import TLU from tensorflow_addons.layers.wrappers import WeightNormalization from tensorflow_addons.layers.esn import ESN +from tensorflow_addons.layers.stochastic_depth import StochasticDepth from tensorflow_addons.layers.noisy_dense import NoisyDense diff --git a/tensorflow_addons/layers/stochastic_depth.py b/tensorflow_addons/layers/stochastic_depth.py new file mode 100644 index 0000000000..3cf0df4f8c --- /dev/null +++ b/tensorflow_addons/layers/stochastic_depth.py @@ -0,0 +1,88 @@ +import tensorflow as tf +from typeguard import typechecked + + +@tf.keras.utils.register_keras_serializable(package="Addons") +class StochasticDepth(tf.keras.layers.Layer): + """Stochastic Depth layer. + + Implements Stochastic Depth as described in + [Deep Networks with Stochastic Depth](https://arxiv.org/abs/1603.09382), to randomly drop residual branches + in residual architectures. + + Usage: + Residual architectures with fixed depth, use residual branches that are merged back into the main network + by adding the residual branch back to the input: + + >>> input = np.ones((1, 3, 3, 1), dtype = np.float32) + >>> residual = tf.keras.layers.Conv2D(1, 1)(input) + >>> output = tf.keras.layers.Add()([input, residual]) + >>> output.shape + TensorShape([1, 3, 3, 1]) + + StochasticDepth acts as a drop-in replacement for the addition: + + >>> input = np.ones((1, 3, 3, 1), dtype = np.float32) + >>> residual = tf.keras.layers.Conv2D(1, 1)(input) + >>> output = tfa.layers.StochasticDepth()([input, residual]) + >>> output.shape + TensorShape([1, 3, 3, 1]) + + At train time, StochasticDepth returns: + + $$ + x[0] + b_l * x[1], + $$ + + where $b_l$ is a random Bernoulli variable with probability $P(b_l = 1) = p_l$ + + At test time, StochasticDepth rescales the activations of the residual branch based on the survival probability ($p_l$): + + $$ + x[0] + p_l * x[1] + $$ + + Arguments: + survival_probability: float, the probability of the residual branch being kept. + + Call Arguments: + inputs: List of `[shortcut, residual]` where `shortcut`, and `residual` are tensors of equal shape. + + Output shape: + Equal to the shape of inputs `shortcut`, and `residual` + """ + + @typechecked + def __init__(self, survival_probability: float = 0.5, **kwargs): + super().__init__(**kwargs) + + self.survival_probability = survival_probability + + def call(self, x, training=None): + if not isinstance(x, list) or len(x) != 2: + raise ValueError("input must be a list of length 2.") + + shortcut, residual = x + + # Random bernoulli variable indicating whether the branch should be kept or not or not + b_l = tf.keras.backend.random_bernoulli([], p=self.survival_probability) + + def _call_train(): + return shortcut + b_l * residual + + def _call_test(): + return shortcut + self.survival_probability * residual + + return tf.keras.backend.in_train_phase( + _call_train, _call_test, training=training + ) + + def compute_output_shape(self, input_shape): + return input_shape[0] + + def get_config(self): + base_config = super().get_config() + + config = {"survival_probability": self.survival_probability} + + return {**base_config, **config} diff --git a/tensorflow_addons/layers/tests/stochastic_depth_test.py b/tensorflow_addons/layers/tests/stochastic_depth_test.py new file mode 100644 index 0000000000..1122016f57 --- /dev/null +++ b/tensorflow_addons/layers/tests/stochastic_depth_test.py @@ -0,0 +1,58 @@ +import pytest +import numpy as np +import tensorflow as tf + +from tensorflow_addons.layers.stochastic_depth import StochasticDepth +from tensorflow_addons.utils import test_utils + +_KEEP_SEED = 1111 +_DROP_SEED = 2222 + + +@pytest.mark.parametrize("seed", [_KEEP_SEED, _DROP_SEED]) +@pytest.mark.parametrize("training", [True, False]) +def stochastic_depth_test(seed, training): + np.random.seed(seed) + tf.random.set_seed(seed) + + survival_probability = 0.5 + + shortcut = np.asarray([[0.2, 0.1, 0.4]]).astype(np.float32) + residual = np.asarray([[0.2, 0.4, 0.5]]).astype(np.float32) + + if training: + if seed == _KEEP_SEED: + # shortcut + residual + expected_output = np.asarray([[0.4, 0.5, 0.9]]).astype(np.float32) + elif seed == _DROP_SEED: + # shortcut + expected_output = np.asarray([[0.2, 0.1, 0.4]]).astype(np.float32) + else: + # shortcut + p_l * residual + expected_output = np.asarray([[0.3, 0.3, 0.65]]).astype(np.float32) + + test_utils.layer_test( + StochasticDepth, + kwargs={"survival_probability": survival_probability}, + input_data=[shortcut, residual], + expected_output=expected_output, + ) + + +@pytest.mark.usefixtures("run_with_mixed_precision_policy") +def test_with_mixed_precision_policy(): + policy = tf.keras.mixed_precision.experimental.global_policy() + + shortcut = np.asarray([[0.2, 0.1, 0.4]]) + residual = np.asarray([[0.2, 0.4, 0.5]]) + + output = StochasticDepth()([shortcut, residual]) + + assert output.dtype == policy.compute_dtype + + +def test_serialization(): + stoch_depth = StochasticDepth(survival_probability=0.5) + serialized_stoch_depth = tf.keras.layers.serialize(stoch_depth) + new_layer = tf.keras.layers.deserialize(serialized_stoch_depth) + assert stoch_depth.get_config() == new_layer.get_config() From 5fd46e92df3d9ef0cb9eac49d0e674bca2a377e2 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Thu, 17 Sep 2020 13:47:47 +0500 Subject: [PATCH 091/259] fixed example --- .../metrics/multilabel_confusion_matrix.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index fe25dfb4e7..357a4aab67 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [0. 2.]] + [[2. 0.] + [0. 2.]] - [[0. 2.] - [2. 0.]]] + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [2. 0.]] + [[2. 0.] + [2. 0.]] - [[2. 2.] - [0. 0.]]] + [[2. 2.] + [0. 0.]]] """ @typechecked From e2006c3e9a35c87649f0c902f90112b188871092 Mon Sep 17 00:00:00 2001 From: Shubhanshu Mishra Date: Thu, 17 Sep 2020 14:04:26 -0400 Subject: [PATCH 092/259] Added filtered_input and constrained_decoding (#2166) * Added filtered_input and constrained_decoding - Fixes #607 We can have a common function for creating filtered_inputs used in crf_multitag_sequence_score This function can be reused to modify the input to crf_decode to support constrained decoding. * Fixed formatting and imports * Fixed documentation * Fixed formatting --- tensorflow_addons/text/__init__.py | 2 + tensorflow_addons/text/crf.py | 61 ++++++++- tensorflow_addons/text/tests/crf_test.py | 157 +++++++++++++++++++---- 3 files changed, 192 insertions(+), 28 deletions(-) diff --git a/tensorflow_addons/text/__init__.py b/tensorflow_addons/text/__init__.py index d9c8383dc5..dfb6379ec9 100644 --- a/tensorflow_addons/text/__init__.py +++ b/tensorflow_addons/text/__init__.py @@ -16,9 +16,11 @@ # Conditional Random Field from tensorflow_addons.text.crf import crf_binary_score +from tensorflow_addons.text.crf import crf_constrained_decode from tensorflow_addons.text.crf import crf_decode from tensorflow_addons.text.crf import crf_decode_backward from tensorflow_addons.text.crf import crf_decode_forward +from tensorflow_addons.text.crf import crf_filtered_inputs from tensorflow_addons.text.crf import crf_forward from tensorflow_addons.text.crf import crf_log_likelihood from tensorflow_addons.text.crf import crf_log_norm diff --git a/tensorflow_addons/text/crf.py b/tensorflow_addons/text/crf.py index 2707895e7d..8bc9901128 100644 --- a/tensorflow_addons/text/crf.py +++ b/tensorflow_addons/text/crf.py @@ -24,6 +24,32 @@ # https://github.com/tensorflow/tensorflow/issues/29075 is resolved +def crf_filtered_inputs(inputs: TensorLike, tag_bitmap: TensorLike) -> tf.Tensor: + """Constrains the inputs to filter out certain tags at each time step. + + tag_bitmap limits the allowed tags at each input time step. + This is useful when an observed output at a given time step needs to be + constrained to a selected set of tags. + + Args: + inputs: A [batch_size, max_seq_len, num_tags] tensor of unary potentials + to use as input to the CRF layer. + tag_bitmap: A [batch_size, max_seq_len, num_tags] boolean tensor + representing all active tags at each index for which to calculate the + unnormalized score. + Returns: + filtered_inputs: A [batch_size] vector of unnormalized sequence scores. + """ + + # set scores of filtered out inputs to be -inf. + filtered_inputs = tf.where( + tag_bitmap, + inputs, + tf.fill(tf.shape(inputs), tf.cast(float("-inf"), inputs.dtype)), + ) + return filtered_inputs + + def crf_sequence_score( inputs: TensorLike, tag_indices: TensorLike, @@ -107,11 +133,7 @@ def crf_multitag_sequence_score( """ tag_bitmap = tf.cast(tag_bitmap, dtype=tf.bool) sequence_lengths = tf.cast(sequence_lengths, dtype=tf.int32) - filtered_inputs = tf.where( - tag_bitmap, - inputs, - tf.fill(tf.shape(inputs), tf.cast(float("-inf"), inputs.dtype)), - ) + filtered_inputs = crf_filtered_inputs(inputs, tag_bitmap) # If max_seq_len is 1, we skip the score calculation and simply gather the # unary potentials of all active tags. @@ -559,3 +581,32 @@ def _multi_seq_fn(): return tf.cond( tf.equal(tf.shape(potentials)[1], 1), _single_seq_fn, _multi_seq_fn ) + + +def crf_constrained_decode( + potentials: TensorLike, + tag_bitmap: TensorLike, + transition_params: TensorLike, + sequence_length: TensorLike, +) -> tf.Tensor: + """Decode the highest scoring sequence of tags under constraints. + + This is a function for tensor. + + Args: + potentials: A [batch_size, max_seq_len, num_tags] tensor of + unary potentials. + tag_bitmap: A [batch_size, max_seq_len, num_tags] boolean tensor + representing all active tags at each index for which to calculate the + unnormalized score. + transition_params: A [num_tags, num_tags] matrix of + binary potentials. + sequence_length: A [batch_size] vector of true sequence lengths. + Returns: + decode_tags: A [batch_size, max_seq_len] matrix, with dtype `tf.int32`. + Contains the highest scoring tag indices. + best_score: A [batch_size] vector, containing the score of `decode_tags`. + """ + + filtered_potentials = crf_filtered_inputs(potentials, tag_bitmap) + return crf_decode(filtered_potentials, transition_params, sequence_length) diff --git a/tensorflow_addons/text/tests/crf_test.py b/tensorflow_addons/text/tests/crf_test.py index fc92a54a1f..d199d1971c 100644 --- a/tensorflow_addons/text/tests/crf_test.py +++ b/tensorflow_addons/text/tests/crf_test.py @@ -35,6 +35,82 @@ def calculate_sequence_score(inputs, transition_params, tag_indices, sequence_le return expected_unary_score + expected_binary_score +def brute_force_decode(sequence_lengths, inputs, transition_params): + num_words = inputs.shape[0] + num_tags = inputs.shape[1] + + all_sequence_scores = [] + all_sequences = [] + + tag_indices_iterator = itertools.product(range(num_tags), repeat=sequence_lengths) + inputs = tf.expand_dims(inputs, 0) + sequence_lengths = tf.expand_dims(sequence_lengths, 0) + transition_params = tf.constant(transition_params) + + # Compare the dynamic program with brute force computation. + for tag_indices in tag_indices_iterator: + tag_indices = list(tag_indices) + tag_indices.extend([0] * (num_words - sequence_lengths)) + all_sequences.append(tag_indices) + sequence_score = text.crf_sequence_score( + inputs=inputs, + tag_indices=tf.expand_dims(tag_indices, 0), + sequence_lengths=sequence_lengths, + transition_params=transition_params, + ) + sequence_score = tf.squeeze(sequence_score, [0]) + all_sequence_scores.append(sequence_score) + + expected_max_sequence_index = np.argmax(all_sequence_scores) + expected_max_sequence = all_sequences[expected_max_sequence_index] + expected_max_score = all_sequence_scores[expected_max_sequence_index] + return expected_max_sequence, expected_max_score + + +@pytest.mark.parametrize("dtype", [np.float16, np.float32]) +def test_crf_filtered_inputs(dtype): + # Test both the length-1 and regular cases. + sequence_lengths_list = [np.array(3, dtype=np.int32), np.array(1, dtype=np.int32)] + inputs_list = [ + np.array([[4, 5, -3], [3, -1, 3], [-1, 2, 1], [0, 0, 0]], dtype=dtype), + np.array([[4, 5, -3]], dtype=dtype), + ] + tag_bitmap_list = [ + np.array( + [ + [True, False, False], + [False, True, True], + [False, True, True], + [False, True, True], + ], + dtype=np.bool, + ), + np.array([[False, True, True]], dtype=np.bool), + ] + neg_inf = float("-inf") + expected_filtered_inputs_list = [ + np.array( + [[4, neg_inf, neg_inf], [neg_inf, -1, 3], [neg_inf, 2, 1], [neg_inf, 0, 0]], + dtype=dtype, + ), + np.array([[neg_inf, 5, -3]], dtype=dtype), + ] + for sequence_lengths, inputs, tag_bitmap, expected_filtered_inputs in zip( + sequence_lengths_list, + inputs_list, + tag_bitmap_list, + expected_filtered_inputs_list, + ): + filtered_inputs = text.crf_filtered_inputs( + inputs=tf.expand_dims(inputs, 0), tag_bitmap=tf.expand_dims(tag_bitmap, 0) + ) + filtered_inputs = tf.squeeze(filtered_inputs, [0]) + + test_utils.assert_allclose_according_to_type( + filtered_inputs, expected_filtered_inputs + ) + + @pytest.mark.parametrize("dtype", [np.float16, np.float32]) def test_crf_sequence_score(dtype): transition_params = np.array([[-3, 5, -2], [3, 4, 1], [1, 2, 1]], dtype=dtype) @@ -309,29 +385,9 @@ def test_crf_decode(dtype): for sequence_lengths, inputs, tag_indices in zip( sequence_lengths_list, inputs_list, tag_indices_list ): - num_words = inputs.shape[0] - num_tags = inputs.shape[1] - - all_sequence_scores = [] - all_sequences = [] - - # Compare the dynamic program with brute force computation. - for tag_indices in itertools.product(range(num_tags), repeat=sequence_lengths): - tag_indices = list(tag_indices) - tag_indices.extend([0] * (num_words - sequence_lengths)) - all_sequences.append(tag_indices) - sequence_score = text.crf_sequence_score( - inputs=tf.expand_dims(inputs, 0), - tag_indices=tf.expand_dims(tag_indices, 0), - sequence_lengths=tf.expand_dims(sequence_lengths, 0), - transition_params=tf.constant(transition_params), - ) - sequence_score = tf.squeeze(sequence_score, [0]) - all_sequence_scores.append(sequence_score) - - expected_max_sequence_index = np.argmax(all_sequence_scores) - expected_max_sequence = all_sequences[expected_max_sequence_index] - expected_max_score = all_sequence_scores[expected_max_sequence_index] + expected_max_sequence, expected_max_score = brute_force_decode( + sequence_lengths, inputs, transition_params + ) actual_max_sequence, actual_max_score = text.crf_decode( tf.expand_dims(inputs, 0), @@ -350,6 +406,61 @@ def test_crf_decode(dtype): ) +@pytest.mark.parametrize("dtype", [np.float16, np.float32]) +def test_crf_constrained_decode(dtype): + transition_params = np.array([[-3, 5, -2], [3, 4, 1], [1, 2, 1]], dtype=dtype) + # Test both the length-1 and regular cases. + sequence_lengths_list = [np.array(3, dtype=np.int32), np.array(1, dtype=np.int32)] + inputs_list = [ + np.array([[4, 5, -3], [3, -1, 3], [-1, 2, 1], [0, 0, 0]], dtype=dtype), + np.array([[4, 5, -3]], dtype=dtype), + ] + tag_bitmap_list = [ + np.array( + [ + [True, False, False], + [False, True, True], + [False, True, True], + [False, True, True], + ], + dtype=np.bool, + ), + np.array([[False, True, True]], dtype=np.bool), + ] + for sequence_lengths, inputs, tag_bitmap in zip( + sequence_lengths_list, inputs_list, tag_bitmap_list + ): + filtered_inputs = text.crf_filtered_inputs( + inputs=tf.expand_dims(inputs, 0), tag_bitmap=tf.expand_dims(tag_bitmap, 0) + ) + + expected_max_sequence, expected_max_score = text.crf_decode( + filtered_inputs, + tf.constant(transition_params), + tf.expand_dims(sequence_lengths, 0), + ) + + expected_max_sequence = tf.squeeze(expected_max_sequence, [0]) + expected_max_score = tf.squeeze(expected_max_score, [0]) + + actual_max_sequence, actual_max_score = text.crf_constrained_decode( + tf.expand_dims(inputs, 0), + tf.expand_dims(tag_bitmap, 0), + tf.constant(transition_params), + tf.expand_dims(sequence_lengths, 0), + ) + + actual_max_sequence = tf.squeeze(actual_max_sequence, [0]) + actual_max_score = tf.squeeze(actual_max_score, [0]) + + test_utils.assert_allclose_according_to_type( + actual_max_score, expected_max_score, 1e-6, 1e-6 + ) + assert list(actual_max_sequence[:sequence_lengths]) == list( + expected_max_sequence[:sequence_lengths] + ) + + def test_crf_decode_zero_seq_length(): """Test that crf_decode works when sequence_length contains one or more zeros.""" From 13e40e613df3ead8f190258b273aeccaff05dedc Mon Sep 17 00:00:00 2001 From: hp77 Date: Sat, 19 Sep 2020 04:05:09 +0530 Subject: [PATCH 093/259] Moved build_docs.py and BUILD into /tools/docs/ (#2167) * Moved build_docs.py and BUILD into /tools/docs/ * Modified paths in documentation * removing build_docs.py from BUILD * updating to bazel code format * Revert "updating to bazel code format" This reverts commit f97c2ad76052b2f561b31bd09a62302e2fc02257. * Revert "removing build_docs.py from BUILD" This reverts commit 3967d141219ba22b82de54209ba9b84eaa49d1bc. * Updated sanity_check.dockerfile with new path --- docs/README.md | 17 +++-------------- tools/docker/sanity_check.Dockerfile | 2 +- {docs => tools/docs}/BUILD | 0 tools/docs/Readme.md | 19 +++++++++++++++++++ {docs => tools/docs}/build_docs.py | 0 5 files changed, 23 insertions(+), 15 deletions(-) rename {docs => tools/docs}/BUILD (100%) create mode 100644 tools/docs/Readme.md rename {docs => tools/docs}/build_docs.py (100%) diff --git a/docs/README.md b/docs/README.md index ce58a5056f..9ce374aaff 100644 --- a/docs/README.md +++ b/docs/README.md @@ -21,22 +21,11 @@ python -m tensorflow_docs.tools.nbfmt {path to notebook file or directory} -## 2. Generated API docs -[tensorflow.org/addons/api_docs/python/tfa](https://tensorflow.org/addons/api_docs/python/tfa) -`build_docs.py` controls executed this docs generation. To test-run it: -```bash -# Install dependencies: -pip install -r tools/install_deps/doc_requirements.txt -# Build tool: -bazel build docs:build_docs -# Generate API doc: -# Use current branch -bazel-bin/docs/build_docs --git_branch=$(git rev-parse --abbrev-ref HEAD) -# or specified explicitly -bazel-bin/docs/build_docs --git_branch=master --output_dir=/tmp/tfa_api -``` + + + diff --git a/tools/docker/sanity_check.Dockerfile b/tools/docker/sanity_check.Dockerfile index 68393a9705..9300da5a6d 100644 --- a/tools/docker/sanity_check.Dockerfile +++ b/tools/docker/sanity_check.Dockerfile @@ -94,7 +94,7 @@ RUN apt-get update && apt-get install -y rsync COPY ./ /addons WORKDIR /addons RUN pip install --no-deps -e . -RUN python docs/build_docs.py +RUN python tools/docs/build_docs.py RUN touch /ok.txt # ------------------------------- diff --git a/docs/BUILD b/tools/docs/BUILD similarity index 100% rename from docs/BUILD rename to tools/docs/BUILD diff --git a/tools/docs/Readme.md b/tools/docs/Readme.md new file mode 100644 index 0000000000..7c574e5ad8 --- /dev/null +++ b/tools/docs/Readme.md @@ -0,0 +1,19 @@ +## 1. Generated API docs + +[tensorflow.org/addons/api_docs/python/tfa](https://tensorflow.org/addons/api_docs/python/tfa) + +`build_docs.py` controls executed this docs generation. To test-run it: + +```bash +# Install dependencies: +pip install -r tools/install_deps/doc_requirements.txt + +# Build tool: +bazel build //tools/docs:build_docs + +# Generate API doc: +# Use current branch +bazel-bin/tools/docs/build_docs --git_branch=$(git rev-parse --abbrev-ref HEAD) +# or specified explicitly +bazel-bin/tools/docs/build_docs --git_branch=master --output_dir=/tmp/tfa_api +``` diff --git a/docs/build_docs.py b/tools/docs/build_docs.py similarity index 100% rename from docs/build_docs.py rename to tools/docs/build_docs.py From 3aa7c61927a2425fd7a9b6ce760b8c82527f9f9c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 20 Sep 2020 22:03:44 +0500 Subject: [PATCH 094/259] added support for blankline --- .../metrics/multilabel_confusion_matrix.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 357a4aab67..89b1644e30 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,7 +45,7 @@ class MultiLabelConfusionMatrix(Metric): - false positives for class i in M(0,1) - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - + >>> # multilabel confusion matrix >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], ... dtype=tf.int32) @@ -54,15 +54,15 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[2. 0.] - [0. 2.]] + >>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE + array([[[2., 0.], + [0., 2.]], - [[2. 0.] - [0. 2.]] + [[2., 0.], + [0., 2.]], - [[0. 2.] - [2. 0.]]] + [[0., 2.], + [2., 0.]]], dtype=float32) >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -72,7 +72,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) #doctest: -DONT_ACCEPT_BLANKLINE Confusion matrix: [[[2. 0.] [0. 2.]] From 1661c61407a654eda008b5d59ca12fcde9f4db7b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 20 Sep 2020 22:23:47 +0500 Subject: [PATCH 095/259] 'space' --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 89b1644e30..33865a5411 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,7 +45,7 @@ class MultiLabelConfusionMatrix(Metric): - false positives for class i in M(0,1) - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - + >>> # multilabel confusion matrix >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], ... dtype=tf.int32) From ba99b5547bfbd3bd3ab4e867c345c8358221deda Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 20:30:40 +0500 Subject: [PATCH 096/259] update metrics cohen kappa --- tensorflow_addons/metrics/cohens_kappa.py | 41 ++++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cb473530b7..a09bd15f83 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,28 +38,29 @@ class CohenKappa(Metric): Usage: - ```python - actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) - preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) - weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) - - m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - m.update_state(actuals, preds) - print('Final result: ', m.result().numpy()) # Result: 0.61904764 - - # To use this with weights, sample_weight argument can be used. - m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - m.update_state(actuals, preds, sample_weight=weights) - print('Final result: ', m.result().numpy()) # Result: 0.37209308 - ``` - + >>> python + >>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) + >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) + >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) + + >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> m.update_state(actuals, preds) + >>> print('Final result: ', m.result().numpy()) + >>> # Result: 0.61904764 + + >>> # To use this with weights, sample_weight argument can be used. + >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> m.update_state(actuals, preds, sample_weight=weights) + >>> print('Final result: ', m.result().numpy()) + >>> # Result: 0.37209308 + Usage with tf.keras API: - ```python - model = tf.keras.models.Model(inputs, outputs) - model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) - model.compile('sgd', loss='mse') - ``` + >>> python + >>> model = tf.keras.models.Model(inputs, outputs) + >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) + >>> model.compile('sgd', loss='mse') + """ @typechecked From 6cc7a54502afcd392a84f5963755675e368ce483 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 20:51:22 +0500 Subject: [PATCH 097/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index a09bd15f83..f78d56851e 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -53,14 +53,14 @@ class CohenKappa(Metric): >>> m.update_state(actuals, preds, sample_weight=weights) >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.37209308 - + Usage with tf.keras API: >>> python >>> model = tf.keras.models.Model(inputs, outputs) >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) >>> model.compile('sgd', loss='mse') - + """ @typechecked From 1b1b602cf63bc7a3dc1d668147e1d3e079921dfc Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:05:13 +0500 Subject: [PATCH 098/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index f78d56851e..cf4ebe43e3 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,7 +38,7 @@ class CohenKappa(Metric): Usage: - >>> python + >>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) @@ -56,7 +56,6 @@ class CohenKappa(Metric): Usage with tf.keras API: - >>> python >>> model = tf.keras.models.Model(inputs, outputs) >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) >>> model.compile('sgd', loss='mse') From e549e9d5009ccbb4a6827a7b54df8d788b0b8a1b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:09:33 +0500 Subject: [PATCH 099/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cf4ebe43e3..266429caa2 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,13 +45,13 @@ class CohenKappa(Metric): >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds) - >>> print('Final result: ', m.result().numpy()) + >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds, sample_weight=weights) - >>> print('Final result: ', m.result().numpy()) + >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.37209308 Usage with tf.keras API: From 82e26b1942de2f19b5249a755f119235c9844b5e Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:30:54 +0500 Subject: [PATCH 100/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 9702b030ea..d69cee12f1 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -38,14 +38,13 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: Usage: - ```python - actuals = tf.constant([1, 1, 0, 0, 1, 0, 1, 0, 0, 1], - dtype=tf.int32) - predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], - dtype=tf.int32) - result = hamming_distance(actuals, predictions) - print('Hamming distance: ', result.numpy()) - ``` + >>> actuals = tf.constant([1, 1, 0, 0, 1, 0, 1, 0, 0, 1], + ... dtype=tf.int32) + >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], + ... dtype=tf.int32) + >>> result = hamming_distance(actuals, predictions) + ... print('Hamming distance: ', result.numpy()) + """ result = tf.not_equal(actuals, predictions) not_eq = tf.reduce_sum(tf.cast(result, tf.float32)) From 6d58948bac0985118106912b60d7923164143f4c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:34:10 +0500 Subject: [PATCH 101/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 266429caa2..a6f3f40fd7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,14 +45,14 @@ class CohenKappa(Metric): >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds) - >>> print('Final result: ', m.result().numpy()) - >>> # Result: 0.61904764 + >>> # print('Final result: ', m.result().numpy()) + >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds, sample_weight=weights) - >>> print('Final result: ', m.result().numpy()) - >>> # Result: 0.37209308 + >>> # print('Final result: ', m.result().numpy()) + >>> # Final result: 0.37209308 Usage with tf.keras API: From 5689900fd17db1d1742d3d2fb21b4aa4dbc3538f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:40:19 +0500 Subject: [PATCH 102/259] Update matthews_correlation_coefficient.py --- .../matthews_correlation_coefficient.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index e6afa53f61..a9ae447889 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -42,18 +42,18 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ((TP + FP) * (TP + FN) * (TN + FP ) * (TN + FN))^(1/2) Usage: - ```python - actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]], - dtype=tf.float32) - preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], - dtype=tf.float32) - # Matthews correlation coefficient - mcc = MatthewsCorrelationCoefficient(num_classes=1) - mcc.update_state(actuals, preds) - print('Matthews correlation coefficient is:', - mcc.result().numpy()) - # Matthews correlation coefficient is : -0.33333334 - ``` + + >>> actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]], + ... dtype=tf.float32) + >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], + ... dtype=tf.float32) + >>> # Matthews correlation coefficient + >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) + >>> mcc.update_state(actuals, preds) + >>> print('Matthews correlation coefficient is:', + ... mcc.result().numpy()) + >>> # Matthews correlation coefficient is : -0.33333334 + """ @typechecked From 6f7cc8905e9192389d5b86973b5ef793bbb9baa4 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:46:15 +0500 Subject: [PATCH 103/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index d13a355b33..1d0dc6f153 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -46,30 +46,29 @@ class MultiLabelConfusionMatrix(Metric): - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - ```python - # multilabel confusion matrix - y_true = tf.constant([[1, 0, 1], [0, 1, 0]], - dtype=tf.int32) - y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], - dtype=tf.int32) - output = MultiLabelConfusionMatrix(num_classes=3) - output.update_state(y_true, y_pred) - print('Confusion matrix:', output.result().numpy()) - - # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - [[0 1] [1 0]]] - - # if multiclass input is provided - y_true = tf.constant([[1, 0, 0], [0, 1, 0]], - dtype=tf.int32) - y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], - dtype=tf.int32) - output = MultiLabelConfusionMatrix(num_classes=3) - output.update_state(y_true, y_pred) - print('Confusion matrix:', output.result().numpy()) - - # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] - ``` + >>> # multilabel confusion matrix + >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], + ... dtype=tf.int32) + >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], + ... dtype=tf.int32) + >>> output = MultiLabelConfusionMatrix(num_classes=3) + >>> output.update_state(y_true, y_pred) + >>> print('Confusion matrix:', output.result().numpy()) + + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] + ... [[0 1] [1 0]]] + + >>> # if multiclass input is provided + >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], + ... dtype=tf.int32) + >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], + ... dtype=tf.int32) + >>> output = MultiLabelConfusionMatrix(num_classes=3) + >>> output.update_state(y_true, y_pred) + >>> print('Confusion matrix:', output.result().numpy()) + + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + """ @typechecked From b4f2c4c33bf057ef1985529e876f3007c6ee2df8 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:48:58 +0500 Subject: [PATCH 104/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 04c3866dbc..0039c77e46 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -60,13 +60,13 @@ class RSquare(Metric): of the same metric. Usage: - ```python - actuals = tf.constant([1, 4, 3], dtype=tf.float32) - preds = tf.constant([2, 4, 4], dtype=tf.float32) - result = tf.keras.metrics.RSquare() - result.update_state(actuals, preds) - print('R^2 score is: ', r1.result().numpy()) # 0.57142866 - ``` + + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) + >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) + >>> result = tf.keras.metrics.RSquare() + >>> result.update_state(actuals, preds) + >>> print('R^2 score is: ', r1.result().numpy()) # 0.57142866 + """ @typechecked From c5328d09509bb8ea9d3548805ee9f0d9c96f6616 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:53:40 +0500 Subject: [PATCH 105/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 0039c77e46..27a471cbfc 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -65,8 +65,9 @@ class RSquare(Metric): >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> result = tf.keras.metrics.RSquare() >>> result.update_state(actuals, preds) - >>> print('R^2 score is: ', r1.result().numpy()) # 0.57142866 - + >>> print('R^2 score is: ', r1.result().numpy()) + >>> # R^2 score is: 0.57142866 + """ @typechecked From b8ebbf817aa01e0fb90b2a27e39229acf33b09f8 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:18:30 +0500 Subject: [PATCH 106/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index a6f3f40fd7..c1446876e8 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds) + >>> # m.update_state(actuals, preds) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds, sample_weight=weights) + >>> # m.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.37209308 From 3c2d1a4fc4e56a6fed9eecb74095b836808eda08 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:23:44 +0500 Subject: [PATCH 107/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 1d0dc6f153..a50b442137 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> output.update_state(y_true, y_pred) - >>> print('Confusion matrix:', output.result().numpy()) + >>> # output.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] ... [[0 1] [1 0]]] @@ -64,8 +64,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> output.update_state(y_true, y_pred) - >>> print('Confusion matrix:', output.result().numpy()) + >>> # output.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From 05ce484fab080d4f9fc9dcf8124bd40c1211dafb Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:25:14 +0500 Subject: [PATCH 108/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index a9ae447889..23850b981f 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -50,7 +50,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> # Matthews correlation coefficient >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) - >>> print('Matthews correlation coefficient is:', + >>> # print('Matthews correlation coefficient is:', ... mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 From 9ce707881fa3bf5994b781e95f947c3b6cdbc028 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:20:40 +0500 Subject: [PATCH 109/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index d69cee12f1..5b52a15e25 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> result = hamming_distance(actuals, predictions) - ... print('Hamming distance: ', result.numpy()) + >>> print('Hamming distance: ', result.numpy()) """ result = tf.not_equal(actuals, predictions) From 78fdfec5137929b5a2c962727efbac9b989907a5 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:31:36 +0500 Subject: [PATCH 110/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 5b52a15e25..8519ccdf2c 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> result = hamming_distance(actuals, predictions) - >>> print('Hamming distance: ', result.numpy()) + >>> #print('Hamming distance: ', result.numpy()) """ result = tf.not_equal(actuals, predictions) From 4e2ca526049ce7277d2845d65a9daa62b2464cc7 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:44:29 +0500 Subject: [PATCH 111/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 27a471cbfc..5226150f77 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,7 +63,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tf.keras.metrics.RSquare() + >>> result = tf.keras.metrics.r_square.RSquare() >>> result.update_state(actuals, preds) >>> print('R^2 score is: ', r1.result().numpy()) >>> # R^2 score is: 0.57142866 From 738886ad9dd39d54b60d128f34957d8e1829798c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:27 +0500 Subject: [PATCH 112/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 23850b981f..533ed42a74 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -50,8 +50,9 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> # Matthews correlation coefficient >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) + >>> # uncomment the lines below to check the result >>> # print('Matthews correlation coefficient is:', - ... mcc.result().numpy()) + ... # mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 37e82206118ca955ddc6c51830dbe99fad51f0fe Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:33 +0500 Subject: [PATCH 113/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index a50b442137..e9b53e1ca5 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,7 +56,7 @@ class MultiLabelConfusionMatrix(Metric): >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - ... [[0 1] [1 0]]] + ... #[[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], From 27736751d2e869001e085a11b1a24102f09f2669 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:39 +0500 Subject: [PATCH 114/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 5226150f77..b85557866e 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,7 +63,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tf.keras.metrics.r_square.RSquare() + >>> result = tfa.metrics.r_square.RSquare() >>> result.update_state(actuals, preds) >>> print('R^2 score is: ', r1.result().numpy()) >>> # R^2 score is: 0.57142866 From 30f01a7927cff9cf3a9e3086b5e16376613a179d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:57:23 +0500 Subject: [PATCH 115/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 533ed42a74..674b0e4440 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,8 +51,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', - ... # mcc.result().numpy()) + >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From af599ef411bed48fa2f11e8b15b2da9b7dae8940 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 22:14:54 +0500 Subject: [PATCH 116/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index e9b53e1ca5..da10e306b4 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,8 +55,7 @@ class MultiLabelConfusionMatrix(Metric): >>> # output.update_state(y_true, y_pred) >>> # print('Confusion matrix:', output.result().numpy()) - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - ... #[[0 1] [1 0]]] + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], From 199ee0f141bf7b050e11f947e8c43c18b9d1bb03 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 22:28:26 +0500 Subject: [PATCH 117/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index b85557866e..46dde051f3 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,9 +63,9 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) - >>> print('R^2 score is: ', r1.result().numpy()) + >>> result = tfa.metrics.r_square.RSquare() + >>> # Uncomment the lines below to see the result + >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 """ From 120222f9985ac7003f53b659fbab4a547259c212 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 23:39:03 +0500 Subject: [PATCH 118/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 46dde051f3..fefe91dd3a 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,7 +63,8 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() + >>> result = tfa.metrics.r_square.RSquare() + >>> result.update_state(actuals, preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From 44fe8f6c73c47303edbbc397e24c4a27d63c2c8d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 12:36:12 +0500 Subject: [PATCH 119/259] keras Api example --- tensorflow_addons/metrics/cohens_kappa.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index c1446876e8..ab6f52b01b 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -56,9 +56,11 @@ class CohenKappa(Metric): Usage with tf.keras API: - >>> model = tf.keras.models.Model(inputs, outputs) - >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) - >>> model.compile('sgd', loss='mse') + >>> inputs = tf.keras.Input(shape=(10,)) + >>> x = tf.keras.layers.Dense(10)(inputs) + >>> outputs = tf.keras.layers.Dense(1)(x) + >>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs) + >>> model.compile('sgd', loss='mse', metrics=[tfm.CohenKappa(num_classes=3, sparse_labels=True)]) """ From 1230dcfaf8eceda15f1e2a7936be6e8c4079509c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 12:48:25 +0500 Subject: [PATCH 120/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index ab6f52b01b..b39c52298e 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -60,7 +60,7 @@ class CohenKappa(Metric): >>> x = tf.keras.layers.Dense(10)(inputs) >>> outputs = tf.keras.layers.Dense(1)(x) >>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs) - >>> model.compile('sgd', loss='mse', metrics=[tfm.CohenKappa(num_classes=3, sparse_labels=True)]) + >>> model.compile('sgd', loss='mse', metrics=[tfa.metrics.CohenKappa(num_classes=3, sparse_labels=True)]) """ From 3984ece817ec65a1188fc61359258de3365a79cc Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 13:09:00 +0500 Subject: [PATCH 121/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index fefe91dd3a..67bcd25710 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -59,15 +59,15 @@ class RSquare(Metric): ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) of the same metric. - Usage: - - >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) - >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) - >>> # Uncomment the lines below to see the result - >>> # print('R^2 score is: ', result(actuals, preds).numpy()) - >>> # R^2 score is: 0.57142866 + Usage: + + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) + >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) + >>> result = tfa.metrics.r_square.RSquare() + >>> result.update_state(actuals, preds) + >>> # Uncomment the lines below to see the result + >>> # print('R^2 score is: ', result(actuals, preds).numpy()) + >>> # R^2 score is: 0.57142866 """ From a0718ce932be3aed2da640995e2e8ca8c7a7c081 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 13:17:44 +0500 Subject: [PATCH 122/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 67bcd25710..4ba2551347 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -44,23 +44,23 @@ def _reduce_average( class RSquare(Metric): """Compute R^2 score. - This is also called the [coefficient of determination - ](https://en.wikipedia.org/wiki/Coefficient_of_determination). - It tells how close are data to the fitted regression line. + This is also called the [coefficient of determination + ](https://en.wikipedia.org/wiki/Coefficient_of_determination). + It tells how close are data to the fitted regression line. - - Highest score can be 1.0 and it indicates that the predictors + - Highest score can be 1.0 and it indicates that the predictors perfectly accounts for variation in the target. - - Score 0.0 indicates that the predictors do not + - Score 0.0 indicates that the predictors do not account for variation in the target. - - It can also be negative if the model is worse. + - It can also be negative if the model is worse. - The sample weighting for this metric implementation mimics the - behaviour of the [scikit-learn implementation - ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) - of the same metric. + The sample weighting for this metric implementation mimics the + behaviour of the [scikit-learn implementation + ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) + of the same metric. Usage: - + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> result = tfa.metrics.r_square.RSquare() From 20d26fe9dbbb7279f87c82ba2f6bdd21d39d8824 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 00:32:20 +0500 Subject: [PATCH 123/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 674b0e4440..9f56f158c5 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -48,7 +48,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], ... dtype=tf.float32) >>> # Matthews correlation coefficient - >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) + >>> mcc = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) >>> # uncomment the lines below to check the result >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) From 50a4ef13af5e02161e7ba297509980d38c1f0888 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 07:51:26 +0500 Subject: [PATCH 124/259] uncommented update_state --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index b39c52298e..efed0be879 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # m.update_state(actuals, preds) + >>> m.update_state(actuals, preds) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # m.update_state(actuals, preds, sample_weight=weights) + >>> m.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.37209308 From ae251acd0b7250de6419cbec0918d893f5cfeb94 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:20:40 +0500 Subject: [PATCH 125/259] requested_change --- tensorflow_addons/metrics/cohens_kappa.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index efed0be879..2040bc8101 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -43,15 +43,15 @@ class CohenKappa(Metric): >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) - >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds) - >>> # print('Final result: ', m.result().numpy()) + >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. - >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds, sample_weight=weights) - >>> # print('Final result: ', m.result().numpy()) + >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> metric.update_state(actuals, preds, sample_weight=weights) + >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 Usage with tf.keras API: From e8df84fc045a165992816e194bf048f0214aa062 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:21:25 +0500 Subject: [PATCH 126/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 2040bc8101..b2bd42cba7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,7 +44,7 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> # metric.update_state(y_true = actuals, y_pred = preds) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.61904764 From d1acae0054a1c2c8e3d92c742176811df1fcea3c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:25:14 +0500 Subject: [PATCH 127/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 4ba2551347..3002bb051f 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,8 +63,8 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) + >>> metric = tfa.metrics.r_square.RSquare() + >>> metric.update_state(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From 73263d66e9c1bc1d9cc6a6b1ef42e11dd683225b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:28:35 +0500 Subject: [PATCH 128/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index da10e306b4..7ec50a66de 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -51,9 +51,9 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) - >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> # output.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', output.result().numpy()) + >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> # metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', metric.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -62,9 +62,9 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> # output.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', output.result().numpy()) + >>> metric = MultiLabelConfusionMatrix(num_classes=3) + >>> # metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', metric.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From e607e61abcee904dec43ad1195c57f2379ae4e3d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:29:59 +0500 Subject: [PATCH 129/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 7ec50a66de..bc5d92d691 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -62,7 +62,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> metric = MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) From 5b6f6e2827dfa2ea01d16d67035363209c9bc85a Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:32:10 +0500 Subject: [PATCH 130/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 9f56f158c5..9227be1080 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -48,10 +48,10 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], ... dtype=tf.float32) >>> # Matthews correlation coefficient - >>> mcc = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> mcc.update_state(actuals, preds) + >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) + >>> metric.update_state(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) + >>> # print('Matthews correlation coefficient is:', metric.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 97bd12033c95a878813010e357f8e93f0aff807c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:46:05 +0500 Subject: [PATCH 131/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 49 +++++++++++++--------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 8519ccdf2c..a55ec61be0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,8 +42,8 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> result = hamming_distance(actuals, predictions) - >>> #print('Hamming distance: ', result.numpy()) + >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> #print('Hamming distance: ', metric.numpy()) """ result = tf.not_equal(actuals, predictions) @@ -83,31 +83,26 @@ def hamming_loss_fn( Usage: - ```python - # multi-class hamming loss - hl = HammingLoss(mode='multiclass', threshold=0.6) - actuals = tf.constant([[1, 0, 0, 0],[0, 0, 1, 0], - [0, 0, 0, 1],[0, 1, 0, 0]], - dtype=tf.float32) - predictions = tf.constant([[0.8, 0.1, 0.1, 0], - [0.2, 0, 0.8, 0], - [0.05, 0.05, 0.1, 0.8], - [1, 0, 0, 0]], - dtype=tf.float32) - hl.update_state(actuals, predictions) - print('Hamming loss: ', hl.result().numpy()) # 0.25 - - # multi-label hamming loss - hl = HammingLoss(mode='multilabel', threshold=0.8) - actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], - [0, 0, 0,1]], dtype=tf.int32) - predictions = tf.constant([[0.82, 0.5, 0.90, 0], - [0, 1, 0.4, 0.98], - [0.89, 0.79, 0, 0.3]], - dtype=tf.float32) - hl.update_state(actuals, predictions) - print('Hamming loss: ', hl.result().numpy()) # 0.16666667 - ``` + >>> # multi-class hamming loss + >>> hl = HammingLoss(mode='multiclass', threshold=0.6) + >>> actuals = tf.constant([[1, 0, 0, 0],[0, 0, 1, 0], + ... [0, 0, 0, 1],[0, 1, 0, 0]], dtype=tf.float32) + >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], + ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], + ... dtype=tf.float32) + >>> hl.update_state(y_true = actuals, y_pred = predictions) + >>> #uncomment the line below to see the result + >>> #print('Hamming loss: ', hl.result().numpy()) # 0.25 + + >>> # multi-label hamming loss + >>> hl = HammingLoss(mode='multilabel', threshold=0.8) + >>> actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], + ... [0, 0, 0,1]], dtype=tf.int32) + >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], + ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) + >>> hl.update_state(y_true = actuals, y_pred = predictions) + >>> #print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + """ if mode not in ["multiclass", "multilabel"]: raise TypeError("mode must be either multiclass or multilabel]") From 687c0b7e663a2dca4a9703369603961e2d1d2d37 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:47:35 +0500 Subject: [PATCH 132/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index bce8e15509..a123a9f224 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -35,9 +35,9 @@ class GeometricMean(Metric): Usage: - >>> m = tfa.metrics.GeometricMean() - >>> m.update_state([1, 3, 5, 7, 9]) - >>> m.result().numpy() + >>> metric = tfa.metrics.GeometricMean() + >>> metric.update_state([1, 3, 5, 7, 9]) + >>> metric.result().numpy() 3.9362833 """ From e1ec9c74b2fe29426778937f5d4d4fe416cd7e37 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:17:35 +0500 Subject: [PATCH 133/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index b2bd42cba7..cce36ef738 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -50,7 +50,7 @@ class CohenKappa(Metric): >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> metric.update_state(actuals, preds, sample_weight=weights) + >>> # metric.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 From 0250834f7d74724bc46787be6ae67abda143fbfa Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:38:34 +0500 Subject: [PATCH 134/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index a55ec61be0..6776077c2f 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,9 +90,9 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - >>> hl.update_state(y_true = actuals, y_pred = predictions) - >>> #uncomment the line below to see the result - >>> #print('Hamming loss: ', hl.result().numpy()) # 0.25 + >>> # hl.update_state(actuals, predictions) + >>> # uncomment the line below to see the result + >>> # print('Hamming loss: ', hl.result().numpy()) # 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -100,8 +100,8 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - >>> hl.update_state(y_true = actuals, y_pred = predictions) - >>> #print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + >>> # hl.update_state(actuals, predictions) + >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From cd166b42267f6deeb2aad27753ae8331da1e2b36 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:56:40 +0500 Subject: [PATCH 135/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index bc5d92d691..7cdd9deb26 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -51,7 +51,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) - >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) @@ -62,7 +62,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) From 27d58b09ff44dcec71f2df1611f85ebd028a366f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:58:48 +0500 Subject: [PATCH 136/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 6776077c2f..b7ccaa85d6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> metric = hamming_distance(actuals, predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From 3c266b0fdfd07ada79fe87fa4890c5ce722eef24 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 09:58:24 +0500 Subject: [PATCH 137/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index b7ccaa85d6..6776077c2f 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(actuals, predictions) + >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From edc9d2f558511b44aaae7784ce40c737204897dc Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:13:50 +0500 Subject: [PATCH 138/259] y_true, y_pred are not accepted in hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 6776077c2f..b7ccaa85d6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> metric = hamming_distance(actuals, predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From dc27bafaa55b82118d43e14219630a98bd7bdb52 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:24:37 +0500 Subject: [PATCH 139/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index b7ccaa85d6..9f2a78e7b0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,9 +90,10 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - >>> # hl.update_state(actuals, predictions) + + >>> hl.update_state(actuals, predictions) >>> # uncomment the line below to see the result - >>> # print('Hamming loss: ', hl.result().numpy()) # 0.25 + >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -100,7 +101,8 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - >>> # hl.update_state(actuals, predictions) + + >>> hl.update_state(actuals, predictions) >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ From fea2499ee9166f99ccfe6729545761e34152e6e9 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:28:11 +0500 Subject: [PATCH 140/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 9f2a78e7b0..c4a15911a0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,7 +90,7 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - + >>> hl.update_state(actuals, predictions) >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 @@ -101,7 +101,7 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - + >>> hl.update_state(actuals, predictions) >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 From e4cf1aae6edc717c5965118a60eb1b3d792c9a5d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:04:52 +0500 Subject: [PATCH 141/259] assigned metric.update_Status to result --- tensorflow_addons/metrics/cohens_kappa.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cce36ef738..356f958b51 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # metric.update_state(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', metric.result().numpy()) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # metric.update_state(actuals, preds, sample_weight=weights) + >>> result = metric.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 From 2589f43cf935df1d71b91d4283ebd8f6e91db822 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:12:16 +0500 Subject: [PATCH 142/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 9227be1080..fa0570bcd9 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -49,9 +49,9 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ... dtype=tf.float32) >>> # Matthews correlation coefficient >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', metric.result().numpy()) + >>> # print('Matthews correlation coefficient is:', result.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 3162aa9be330f0e2ce7d39e6d2b6d5f4abde51ee Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:14:40 +0500 Subject: [PATCH 143/259] assigned m.update_status (yactual,ypred) to result --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 7cdd9deb26..de34960af6 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> # metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', metric.result().numpy()) + >>> result = metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', result.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -63,8 +63,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> # metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', metric.result().numpy()) + >>> result = metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', result.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From 36410b8f3c7a89ce0663046900a3249ea9c051f1 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:15:53 +0500 Subject: [PATCH 144/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 3002bb051f..b5a510bede 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,7 +64,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> metric = tfa.metrics.r_square.RSquare() - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From 0097957cede418d4e17baf93b2643f6fad30ed87 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:27:27 +0500 Subject: [PATCH 145/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index b5a510bede..d1592c77b4 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,9 +64,10 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> metric = tfa.metrics.r_square.RSquare() - >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result - >>> # print('R^2 score is: ', result(actuals, preds).numpy()) + >>> # print('R^2 score is: ', result.numpy()) >>> # R^2 score is: 0.57142866 """ From 430894eca3a3b6e2213c4ce6a3ef21af433d391b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:31:38 +0500 Subject: [PATCH 146/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index de34960af6..c4ec7d5c90 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,9 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> result = metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', result.result().numpy()) + >>> metric.update_state(y_true, y_pred) + >>> result = metric(y_true, y_pred) + >>> # print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -63,8 +64,9 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> result = metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', result.result().numpy()) + >>> metric.update_state(y_true, y_pred) + >>> result = metric(y_true, y_pred) + >>> # print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From 64d28c81fe1e737b75ab1cf728d1ba04a799b9bd Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:32:39 +0500 Subject: [PATCH 147/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index fa0570bcd9..cc5c9f4c15 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -49,9 +49,10 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ... dtype=tf.float32) >>> # Matthews correlation coefficient >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', result.result().numpy()) + >>> # print('Matthews correlation coefficient is:', result.numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From ca7ddc8c2a7a38981243af188b4ba28608f3731a Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:34:04 +0500 Subject: [PATCH 148/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index a123a9f224..9e6f6ea648 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -38,7 +38,7 @@ class GeometricMean(Metric): >>> metric = tfa.metrics.GeometricMean() >>> metric.update_state([1, 3, 5, 7, 9]) >>> metric.result().numpy() - 3.9362833 + >>> # 3.9362833 """ From 9a8608221054310c56810968f2a5e55111a3fa26 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:37:56 +0500 Subject: [PATCH 149/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 356f958b51..612aec63e3 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,14 +44,16 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> result = metric.update_state(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.result().numpy()) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> result = metric.update_state(actuals, preds, sample_weight=weights) - >>> # print('Final result: ', metric.result().numpy()) + >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) + >>> result = metric(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.numpy()) >>> # Final result: 0.37209308 Usage with tf.keras API: From 53017f175a25c5994c15ee0ee161c22bdbec8951 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:43:52 +0500 Subject: [PATCH 150/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index 9e6f6ea648..a123a9f224 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -38,7 +38,7 @@ class GeometricMean(Metric): >>> metric = tfa.metrics.GeometricMean() >>> metric.update_state([1, 3, 5, 7, 9]) >>> metric.result().numpy() - >>> # 3.9362833 + 3.9362833 """ From e5629b1770e3150746faade9148a4e0dc7eac8dc Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:49:59 +0500 Subject: [PATCH 151/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index c4a15911a0..e6733781bd 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> metric = hamming_distance(actuals, predictions) - >>> #print('Hamming distance: ', metric.numpy()) + >>> print('Hamming distance: ', metric.numpy()) """ result = tf.not_equal(actuals, predictions) @@ -92,6 +92,7 @@ def hamming_loss_fn( ... dtype=tf.float32) >>> hl.update_state(actuals, predictions) + tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 @@ -103,7 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + >>> print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From adeda64f5bac5033396ee73ae10c08dc5da9537c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:53:39 +0500 Subject: [PATCH 152/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 612aec63e3..86518c41a1 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,16 +45,17 @@ class CohenKappa(Metric): >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds) + tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.numpy()) - >>> # Final result: 0.61904764 + >>>print('Final result: ', result.numpy()) + Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.numpy()) - >>> # Final result: 0.37209308 + >>> print('Final result: ', result.numpy()) + Final result: 0.37209308 Usage with tf.keras API: From 753e36ea48da9dba0e7ecaad68b6ade05d95e71d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:58:19 +0500 Subject: [PATCH 153/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index cc5c9f4c15..c1ba9e2f96 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,9 +51,8 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> metric.update_state(y_true = actuals, y_pred = preds) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', result.numpy()) - >>> # Matthews correlation coefficient is : -0.33333334 + >>> print('Matthews correlation coefficient is:', result.numpy()) + Matthews correlation coefficient is : -0.33333334 """ From 4a936e2aa66eeda8106243f68597b2b271748555 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:08:10 +0500 Subject: [PATCH 154/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index c4ec7d5c90..8a067f7bb9 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -54,7 +54,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> # print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -66,7 +66,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> # print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From 63d7807e1d4c2bb90c8fcaa9b61f827901dabc3a Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:44:49 +0500 Subject: [PATCH 155/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 8a067f7bb9..be3bf82a2e 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,8 +55,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] + Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -67,8 +66,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] """ From 86213f80bf78ed9a864db54badbbfbe6e80f49dc Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:45:25 +0500 Subject: [PATCH 156/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index c1ba9e2f96..68cc2ff9e9 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,8 +51,8 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> metric.update_state(y_true = actuals, y_pred = preds) >>> result = metric(y_true = actuals, y_pred = preds) - >>> print('Matthews correlation coefficient is:', result.numpy()) - Matthews correlation coefficient is : -0.33333334 + >>> print('Matthews correlation coefficient is :', result.numpy()) + Matthews correlation coefficient is : [-0.33333334] """ From d9ff82e4d4eaffb73ed7cd73a179764bf43c19ac Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:53:07 +0500 Subject: [PATCH 157/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index e6733781bd..4e8e914f44 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -44,7 +44,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> metric = hamming_distance(actuals, predictions) >>> print('Hamming distance: ', metric.numpy()) - + Hamming distance: 0.3 """ result = tf.not_equal(actuals, predictions) not_eq = tf.reduce_sum(tf.cast(result, tf.float32)) @@ -94,8 +94,8 @@ def hamming_loss_fn( >>> hl.update_state(actuals, predictions) tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result - >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 - + >>> print('Hamming loss: ', hl.result().numpy()) + Hamming loss: 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) >>> actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], @@ -104,8 +104,8 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> print('Hamming loss: ', hl.result().numpy()) # 0.16666667 - + >>> print('Hamming loss: ', hl.result().numpy()) + Hamming loss: 0.16666667 """ if mode not in ["multiclass", "multilabel"]: raise TypeError("mode must be either multiclass or multilabel]") From ed0dc7e90ec9706774f32588ddd42b297a712a06 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:57:41 +0500 Subject: [PATCH 158/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 4e8e914f44..21bb0c3ccb 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -94,7 +94,7 @@ def hamming_loss_fn( >>> hl.update_state(actuals, predictions) tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result - >>> print('Hamming loss: ', hl.result().numpy()) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -104,7 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> print('Hamming loss: ', hl.result().numpy()) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From bfa3613ed80e21d252d0fb7cc8a41320131ed062 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:05:16 +0500 Subject: [PATCH 159/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 86518c41a1..e42883a4c7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -47,7 +47,7 @@ class CohenKappa(Metric): >>> metric.update_state(y_true = actuals, y_pred = preds) tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) >>> result = metric(y_true = actuals, y_pred = preds) - >>>print('Final result: ', result.numpy()) + >>> print('Final result: ', result.numpy()) Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. From dd00321f51d992183b8702fc4ac4f4b9e70af170 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:08:40 +0500 Subject: [PATCH 160/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 21bb0c3ccb..cffde3014e 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -92,7 +92,7 @@ def hamming_loss_fn( ... dtype=tf.float32) >>> hl.update_state(actuals, predictions) - tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 + >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.25 From 589fbed0fd4545220a8fca7ac8341e6e65bc067c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:27:24 +0500 Subject: [PATCH 161/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index e42883a4c7..00faf0dc59 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,7 +45,12 @@ class CohenKappa(Metric): >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds) - tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) + >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) Final result: 0.61904764 From 5b7a9157bb4885c3bff325140180f1bc1eff3742 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:29:14 +0500 Subject: [PATCH 162/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index cffde3014e..63c40f26e6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -104,6 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.16666667 """ From 2342ae5b5da8cc1c01f20ca4bbeab93d00bfeecc Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:35:27 +0500 Subject: [PATCH 163/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index be3bf82a2e..f26ff0f7a3 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,7 +55,14 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] + Confusion matrix: [[[2. 0.] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -66,7 +73,14 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + Confusion matrix: [[[2. 0.] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ From 37c72ba0d41123cddc43cfd74d53c1a6a06905b1 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:59:54 +0500 Subject: [PATCH 164/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 00faf0dc59..5aaa800a89 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -58,6 +58,12 @@ class CohenKappa(Metric): >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) + >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) Final result: 0.37209308 From 0a9a261021c292c990cecbb09c4a7d20d7613fa5 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:14:01 +0500 Subject: [PATCH 165/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index f26ff0f7a3..693ce6a857 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,14 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] - + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From 6a1fb84fa8d8935a1fbfd4428f8ad6d46dc9d79b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:26:36 +0500 Subject: [PATCH 166/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 5aaa800a89..c3d459563c 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -66,7 +66,7 @@ class CohenKappa(Metric): [ 0., 0., 2., 0., 7.]], dtype=float32)> >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) - Final result: 0.37209308 + Final result: 0.42080373 Usage with tf.keras API: From d3ed8edc70ee31da17593d9210a5e5ea546d442d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:29:20 +0500 Subject: [PATCH 167/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 693ce6a857..357a4aab67 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,12 +56,12 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [0. 2.]] - - [[0. 2.] + [[2. 0.] + [0. 2.]] + + [[0. 2.] [2. 0.]]] >>> # if multiclass input is provided @@ -74,12 +74,12 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] - [[2. 0.] - [2. 0.]] - - [[2. 2.] + [[2. 2.] [0. 0.]]] """ From c7d881b707219b5749eea71dff1fea9bc494d423 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 08:29:29 +0500 Subject: [PATCH 168/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 357a4aab67..9ca016fc9e 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From 37bd5edac61189ac16a4fcc9b71b3ad1b18c3a62 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 08:32:01 +0500 Subject: [PATCH 169/259] unified diff --- .../metrics/multilabel_confusion_matrix.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 9ca016fc9e..aa7e131d1d 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From dfc6dc0c23ca9561fee483e637a47b4099169f0c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 09:05:46 +0500 Subject: [PATCH 170/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index aa7e131d1d..fe25dfb4e7 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,7 +56,7 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] [[2. 0.] [0. 2.]] @@ -74,7 +74,7 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] [[2. 0.] [2. 0.]] From 19c0a6cb4ae7c8e198ff523290b8db7345625f94 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Thu, 17 Sep 2020 13:47:47 +0500 Subject: [PATCH 171/259] fixed example --- .../metrics/multilabel_confusion_matrix.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index fe25dfb4e7..357a4aab67 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [0. 2.]] + [[2. 0.] + [0. 2.]] - [[0. 2.] - [2. 0.]]] + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [2. 0.]] + [[2. 0.] + [2. 0.]] - [[2. 2.] - [0. 0.]]] + [[2. 2.] + [0. 0.]]] """ @typechecked From 6c0e971c7f1d26932921c06d6432e17510159aca Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 20 Sep 2020 22:03:44 +0500 Subject: [PATCH 172/259] added support for blankline --- .../metrics/multilabel_confusion_matrix.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 357a4aab67..89b1644e30 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,7 +45,7 @@ class MultiLabelConfusionMatrix(Metric): - false positives for class i in M(0,1) - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - + >>> # multilabel confusion matrix >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], ... dtype=tf.int32) @@ -54,15 +54,15 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[2. 0.] - [0. 2.]] + >>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE + array([[[2., 0.], + [0., 2.]], - [[2. 0.] - [0. 2.]] + [[2., 0.], + [0., 2.]], - [[0. 2.] - [2. 0.]]] + [[0., 2.], + [2., 0.]]], dtype=float32) >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -72,7 +72,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) #doctest: -DONT_ACCEPT_BLANKLINE Confusion matrix: [[[2. 0.] [0. 2.]] From ce34419bd35ae9262d26a6bc60619a93b731f527 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 20 Sep 2020 22:23:47 +0500 Subject: [PATCH 173/259] 'space' --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 89b1644e30..33865a5411 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,7 +45,7 @@ class MultiLabelConfusionMatrix(Metric): - false positives for class i in M(0,1) - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - + >>> # multilabel confusion matrix >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], ... dtype=tf.int32) From 9aa25983feb91afb5cb256d8ca9eeb72b43ee7eb Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 20:30:40 +0500 Subject: [PATCH 174/259] update metrics cohen kappa --- tensorflow_addons/metrics/cohens_kappa.py | 41 ++++++++++++----------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cb473530b7..a09bd15f83 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,28 +38,29 @@ class CohenKappa(Metric): Usage: - ```python - actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) - preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) - weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) - - m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - m.update_state(actuals, preds) - print('Final result: ', m.result().numpy()) # Result: 0.61904764 - - # To use this with weights, sample_weight argument can be used. - m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - m.update_state(actuals, preds, sample_weight=weights) - print('Final result: ', m.result().numpy()) # Result: 0.37209308 - ``` - + >>> python + >>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) + >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) + >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) + + >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> m.update_state(actuals, preds) + >>> print('Final result: ', m.result().numpy()) + >>> # Result: 0.61904764 + + >>> # To use this with weights, sample_weight argument can be used. + >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> m.update_state(actuals, preds, sample_weight=weights) + >>> print('Final result: ', m.result().numpy()) + >>> # Result: 0.37209308 + Usage with tf.keras API: - ```python - model = tf.keras.models.Model(inputs, outputs) - model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) - model.compile('sgd', loss='mse') - ``` + >>> python + >>> model = tf.keras.models.Model(inputs, outputs) + >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) + >>> model.compile('sgd', loss='mse') + """ @typechecked From 46c44c02ac59a05f6c68af058fcbdde41d88f996 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 20:51:22 +0500 Subject: [PATCH 175/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index a09bd15f83..f78d56851e 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -53,14 +53,14 @@ class CohenKappa(Metric): >>> m.update_state(actuals, preds, sample_weight=weights) >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.37209308 - + Usage with tf.keras API: >>> python >>> model = tf.keras.models.Model(inputs, outputs) >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) >>> model.compile('sgd', loss='mse') - + """ @typechecked From 867fa7f0c37e502c89ccdc8271be57421cf525e3 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:05:13 +0500 Subject: [PATCH 176/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index f78d56851e..cf4ebe43e3 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,7 +38,7 @@ class CohenKappa(Metric): Usage: - >>> python + >>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) @@ -56,7 +56,6 @@ class CohenKappa(Metric): Usage with tf.keras API: - >>> python >>> model = tf.keras.models.Model(inputs, outputs) >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) >>> model.compile('sgd', loss='mse') From 7c3557e6a65e9b7e21b88541c7b8649eacfcabd1 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:09:33 +0500 Subject: [PATCH 177/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cf4ebe43e3..266429caa2 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,13 +45,13 @@ class CohenKappa(Metric): >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds) - >>> print('Final result: ', m.result().numpy()) + >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds, sample_weight=weights) - >>> print('Final result: ', m.result().numpy()) + >>> print('Final result: ', m.result().numpy()) >>> # Result: 0.37209308 Usage with tf.keras API: From 22516db28a964e7c3812e773196bd4986e181660 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:30:54 +0500 Subject: [PATCH 178/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 9702b030ea..d69cee12f1 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -38,14 +38,13 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: Usage: - ```python - actuals = tf.constant([1, 1, 0, 0, 1, 0, 1, 0, 0, 1], - dtype=tf.int32) - predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], - dtype=tf.int32) - result = hamming_distance(actuals, predictions) - print('Hamming distance: ', result.numpy()) - ``` + >>> actuals = tf.constant([1, 1, 0, 0, 1, 0, 1, 0, 0, 1], + ... dtype=tf.int32) + >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], + ... dtype=tf.int32) + >>> result = hamming_distance(actuals, predictions) + ... print('Hamming distance: ', result.numpy()) + """ result = tf.not_equal(actuals, predictions) not_eq = tf.reduce_sum(tf.cast(result, tf.float32)) From d1cabfb40351666787f42f1d3440add3760d7939 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:34:10 +0500 Subject: [PATCH 179/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 266429caa2..a6f3f40fd7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,14 +45,14 @@ class CohenKappa(Metric): >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds) - >>> print('Final result: ', m.result().numpy()) - >>> # Result: 0.61904764 + >>> # print('Final result: ', m.result().numpy()) + >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> m.update_state(actuals, preds, sample_weight=weights) - >>> print('Final result: ', m.result().numpy()) - >>> # Result: 0.37209308 + >>> # print('Final result: ', m.result().numpy()) + >>> # Final result: 0.37209308 Usage with tf.keras API: From e51d1823cdde97dedeee9d02b55e49627cbf1131 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:40:19 +0500 Subject: [PATCH 180/259] Update matthews_correlation_coefficient.py --- .../matthews_correlation_coefficient.py | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index e6afa53f61..a9ae447889 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -42,18 +42,18 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ((TP + FP) * (TP + FN) * (TN + FP ) * (TN + FN))^(1/2) Usage: - ```python - actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]], - dtype=tf.float32) - preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], - dtype=tf.float32) - # Matthews correlation coefficient - mcc = MatthewsCorrelationCoefficient(num_classes=1) - mcc.update_state(actuals, preds) - print('Matthews correlation coefficient is:', - mcc.result().numpy()) - # Matthews correlation coefficient is : -0.33333334 - ``` + + >>> actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]], + ... dtype=tf.float32) + >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], + ... dtype=tf.float32) + >>> # Matthews correlation coefficient + >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) + >>> mcc.update_state(actuals, preds) + >>> print('Matthews correlation coefficient is:', + ... mcc.result().numpy()) + >>> # Matthews correlation coefficient is : -0.33333334 + """ @typechecked From 7daf2f2c029e4d40ed61d27e971a371b967f161d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:46:15 +0500 Subject: [PATCH 181/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 47 +++++++++---------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index d13a355b33..1d0dc6f153 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -46,30 +46,29 @@ class MultiLabelConfusionMatrix(Metric): - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - ```python - # multilabel confusion matrix - y_true = tf.constant([[1, 0, 1], [0, 1, 0]], - dtype=tf.int32) - y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], - dtype=tf.int32) - output = MultiLabelConfusionMatrix(num_classes=3) - output.update_state(y_true, y_pred) - print('Confusion matrix:', output.result().numpy()) - - # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - [[0 1] [1 0]]] - - # if multiclass input is provided - y_true = tf.constant([[1, 0, 0], [0, 1, 0]], - dtype=tf.int32) - y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], - dtype=tf.int32) - output = MultiLabelConfusionMatrix(num_classes=3) - output.update_state(y_true, y_pred) - print('Confusion matrix:', output.result().numpy()) - - # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] - ``` + >>> # multilabel confusion matrix + >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], + ... dtype=tf.int32) + >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], + ... dtype=tf.int32) + >>> output = MultiLabelConfusionMatrix(num_classes=3) + >>> output.update_state(y_true, y_pred) + >>> print('Confusion matrix:', output.result().numpy()) + + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] + ... [[0 1] [1 0]]] + + >>> # if multiclass input is provided + >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], + ... dtype=tf.int32) + >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], + ... dtype=tf.int32) + >>> output = MultiLabelConfusionMatrix(num_classes=3) + >>> output.update_state(y_true, y_pred) + >>> print('Confusion matrix:', output.result().numpy()) + + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + """ @typechecked From 591fae3590e87fbe8fb27e6cd2a22ca0045d56c3 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:48:58 +0500 Subject: [PATCH 182/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 04c3866dbc..0039c77e46 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -60,13 +60,13 @@ class RSquare(Metric): of the same metric. Usage: - ```python - actuals = tf.constant([1, 4, 3], dtype=tf.float32) - preds = tf.constant([2, 4, 4], dtype=tf.float32) - result = tf.keras.metrics.RSquare() - result.update_state(actuals, preds) - print('R^2 score is: ', r1.result().numpy()) # 0.57142866 - ``` + + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) + >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) + >>> result = tf.keras.metrics.RSquare() + >>> result.update_state(actuals, preds) + >>> print('R^2 score is: ', r1.result().numpy()) # 0.57142866 + """ @typechecked From da7076500b7b55cd1d41c2e1d689944031bc0ec0 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:53:40 +0500 Subject: [PATCH 183/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 0039c77e46..27a471cbfc 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -65,8 +65,9 @@ class RSquare(Metric): >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> result = tf.keras.metrics.RSquare() >>> result.update_state(actuals, preds) - >>> print('R^2 score is: ', r1.result().numpy()) # 0.57142866 - + >>> print('R^2 score is: ', r1.result().numpy()) + >>> # R^2 score is: 0.57142866 + """ @typechecked From 127bd4e4ece15b910ec903a6e300e9705f23efd4 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:18:30 +0500 Subject: [PATCH 184/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index a6f3f40fd7..c1446876e8 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds) + >>> # m.update_state(actuals, preds) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds, sample_weight=weights) + >>> # m.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.37209308 From 2984c085ee60ea95c12c4b18cc4c73163845c5a8 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:23:44 +0500 Subject: [PATCH 185/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 1d0dc6f153..a50b442137 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> output.update_state(y_true, y_pred) - >>> print('Confusion matrix:', output.result().numpy()) + >>> # output.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] ... [[0 1] [1 0]]] @@ -64,8 +64,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> output.update_state(y_true, y_pred) - >>> print('Confusion matrix:', output.result().numpy()) + >>> # output.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From 558717be1557864b1ebba4fee783017b3db8c6d3 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 22:25:14 +0500 Subject: [PATCH 186/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index a9ae447889..23850b981f 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -50,7 +50,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> # Matthews correlation coefficient >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) - >>> print('Matthews correlation coefficient is:', + >>> # print('Matthews correlation coefficient is:', ... mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 From 6888bd29fd4d3bb1c899e8521bcf0f132384ef57 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:20:40 +0500 Subject: [PATCH 187/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index d69cee12f1..5b52a15e25 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> result = hamming_distance(actuals, predictions) - ... print('Hamming distance: ', result.numpy()) + >>> print('Hamming distance: ', result.numpy()) """ result = tf.not_equal(actuals, predictions) From f36b72ead4ba95c9aadd1377da927b821a7c9225 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:31:36 +0500 Subject: [PATCH 188/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 5b52a15e25..8519ccdf2c 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> result = hamming_distance(actuals, predictions) - >>> print('Hamming distance: ', result.numpy()) + >>> #print('Hamming distance: ', result.numpy()) """ result = tf.not_equal(actuals, predictions) From c62394a884c4437882d8824ee01b1691db98e990 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 20:44:29 +0500 Subject: [PATCH 189/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 27a471cbfc..5226150f77 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,7 +63,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tf.keras.metrics.RSquare() + >>> result = tf.keras.metrics.r_square.RSquare() >>> result.update_state(actuals, preds) >>> print('R^2 score is: ', r1.result().numpy()) >>> # R^2 score is: 0.57142866 From cd3b849a28c1e31170fafe9c1df787e2d515b4ae Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:27 +0500 Subject: [PATCH 190/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 23850b981f..533ed42a74 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -50,8 +50,9 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> # Matthews correlation coefficient >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) + >>> # uncomment the lines below to check the result >>> # print('Matthews correlation coefficient is:', - ... mcc.result().numpy()) + ... # mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 620ddc281218be222343d27ebd52820fe6bf4c61 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:33 +0500 Subject: [PATCH 191/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index a50b442137..e9b53e1ca5 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,7 +56,7 @@ class MultiLabelConfusionMatrix(Metric): >>> # print('Confusion matrix:', output.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - ... [[0 1] [1 0]]] + ... #[[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], From 8ad555aed814d1a7d3388c5f0d306c2edafbf0d7 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:01:39 +0500 Subject: [PATCH 192/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 5226150f77..b85557866e 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,7 +63,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tf.keras.metrics.r_square.RSquare() + >>> result = tfa.metrics.r_square.RSquare() >>> result.update_state(actuals, preds) >>> print('R^2 score is: ', r1.result().numpy()) >>> # R^2 score is: 0.57142866 From d7b0c6bab5a2b46dd630a74b5acf9267afe4b84f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 21:57:23 +0500 Subject: [PATCH 193/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 533ed42a74..674b0e4440 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,8 +51,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', - ... # mcc.result().numpy()) + >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 09f691209cf7807a0a84cb5c0e4d37cf99046e9c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 22:14:54 +0500 Subject: [PATCH 194/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index e9b53e1ca5..da10e306b4 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,8 +55,7 @@ class MultiLabelConfusionMatrix(Metric): >>> # output.update_state(y_true, y_pred) >>> # print('Confusion matrix:', output.result().numpy()) - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] - ... #[[0 1] [1 0]]] + >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], From f3419c253aa7d6f81e9819079f0b9b85f35e8732 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 22:28:26 +0500 Subject: [PATCH 195/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index b85557866e..46dde051f3 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,9 +63,9 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) - >>> print('R^2 score is: ', r1.result().numpy()) + >>> result = tfa.metrics.r_square.RSquare() + >>> # Uncomment the lines below to see the result + >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 """ From 8cfed7639562ab47381ffcac88ecbb4013e449f0 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Fri, 4 Sep 2020 23:39:03 +0500 Subject: [PATCH 196/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 46dde051f3..fefe91dd3a 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,7 +63,8 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() + >>> result = tfa.metrics.r_square.RSquare() + >>> result.update_state(actuals, preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From 5a832eb739803381fdc7c1cb1fc69f04f759688d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 12:36:12 +0500 Subject: [PATCH 197/259] keras Api example --- tensorflow_addons/metrics/cohens_kappa.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index c1446876e8..ab6f52b01b 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -56,9 +56,11 @@ class CohenKappa(Metric): Usage with tf.keras API: - >>> model = tf.keras.models.Model(inputs, outputs) - >>> model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) - >>> model.compile('sgd', loss='mse') + >>> inputs = tf.keras.Input(shape=(10,)) + >>> x = tf.keras.layers.Dense(10)(inputs) + >>> outputs = tf.keras.layers.Dense(1)(x) + >>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs) + >>> model.compile('sgd', loss='mse', metrics=[tfm.CohenKappa(num_classes=3, sparse_labels=True)]) """ From 552503a13cab01b9f93af5e9ccfd99be8d07f8b5 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 12:48:25 +0500 Subject: [PATCH 198/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index ab6f52b01b..b39c52298e 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -60,7 +60,7 @@ class CohenKappa(Metric): >>> x = tf.keras.layers.Dense(10)(inputs) >>> outputs = tf.keras.layers.Dense(1)(x) >>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs) - >>> model.compile('sgd', loss='mse', metrics=[tfm.CohenKappa(num_classes=3, sparse_labels=True)]) + >>> model.compile('sgd', loss='mse', metrics=[tfa.metrics.CohenKappa(num_classes=3, sparse_labels=True)]) """ From 1e4bf35dab699202c3ee04bbae28e77ecab9a7c4 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 13:09:00 +0500 Subject: [PATCH 199/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index fefe91dd3a..67bcd25710 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -59,15 +59,15 @@ class RSquare(Metric): ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) of the same metric. - Usage: - - >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) - >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) - >>> # Uncomment the lines below to see the result - >>> # print('R^2 score is: ', result(actuals, preds).numpy()) - >>> # R^2 score is: 0.57142866 + Usage: + + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) + >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) + >>> result = tfa.metrics.r_square.RSquare() + >>> result.update_state(actuals, preds) + >>> # Uncomment the lines below to see the result + >>> # print('R^2 score is: ', result(actuals, preds).numpy()) + >>> # R^2 score is: 0.57142866 """ From 21a6a23200d7bc3f336ebc2257bd3702f34cb6ce Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sat, 5 Sep 2020 13:17:44 +0500 Subject: [PATCH 200/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 67bcd25710..4ba2551347 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -44,23 +44,23 @@ def _reduce_average( class RSquare(Metric): """Compute R^2 score. - This is also called the [coefficient of determination - ](https://en.wikipedia.org/wiki/Coefficient_of_determination). - It tells how close are data to the fitted regression line. + This is also called the [coefficient of determination + ](https://en.wikipedia.org/wiki/Coefficient_of_determination). + It tells how close are data to the fitted regression line. - - Highest score can be 1.0 and it indicates that the predictors + - Highest score can be 1.0 and it indicates that the predictors perfectly accounts for variation in the target. - - Score 0.0 indicates that the predictors do not + - Score 0.0 indicates that the predictors do not account for variation in the target. - - It can also be negative if the model is worse. + - It can also be negative if the model is worse. - The sample weighting for this metric implementation mimics the - behaviour of the [scikit-learn implementation - ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) - of the same metric. + The sample weighting for this metric implementation mimics the + behaviour of the [scikit-learn implementation + ](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html) + of the same metric. Usage: - + >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> result = tfa.metrics.r_square.RSquare() From 0b8e7328b03d897b3083eb944d7a8bfd41da8859 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 00:32:20 +0500 Subject: [PATCH 201/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 674b0e4440..9f56f158c5 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -48,7 +48,7 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], ... dtype=tf.float32) >>> # Matthews correlation coefficient - >>> mcc = MatthewsCorrelationCoefficient(num_classes=1) + >>> mcc = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> mcc.update_state(actuals, preds) >>> # uncomment the lines below to check the result >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) From 080ae65d9b1546a870cd8327fdfa6c4234cb1755 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 07:51:26 +0500 Subject: [PATCH 202/259] uncommented update_state --- tensorflow_addons/metrics/cohens_kappa.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index b39c52298e..efed0be879 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # m.update_state(actuals, preds) + >>> m.update_state(actuals, preds) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # m.update_state(actuals, preds, sample_weight=weights) + >>> m.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', m.result().numpy()) >>> # Final result: 0.37209308 From 04ed329ddb51851c53797bfd1f9bcb20e0b07544 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:20:40 +0500 Subject: [PATCH 203/259] requested_change --- tensorflow_addons/metrics/cohens_kappa.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index efed0be879..2040bc8101 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -43,15 +43,15 @@ class CohenKappa(Metric): >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) - >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds) - >>> # print('Final result: ', m.result().numpy()) + >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. - >>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> m.update_state(actuals, preds, sample_weight=weights) - >>> # print('Final result: ', m.result().numpy()) + >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> metric.update_state(actuals, preds, sample_weight=weights) + >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 Usage with tf.keras API: From 5b5da62ce345735dc6cc80c8639c420a6487ce64 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:21:25 +0500 Subject: [PATCH 204/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 2040bc8101..b2bd42cba7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,7 +44,7 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> # metric.update_state(y_true = actuals, y_pred = preds) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.61904764 From 480c103c5e88fe914126c67c04e9ea96a8d2864a Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:25:14 +0500 Subject: [PATCH 205/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 4ba2551347..3002bb051f 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -63,8 +63,8 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) - >>> result = tfa.metrics.r_square.RSquare() - >>> result.update_state(actuals, preds) + >>> metric = tfa.metrics.r_square.RSquare() + >>> metric.update_state(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From 12cd059c56c6831e34847456f8bd244334246385 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:28:35 +0500 Subject: [PATCH 206/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index da10e306b4..7ec50a66de 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -51,9 +51,9 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) - >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> # output.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', output.result().numpy()) + >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> # metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', metric.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -62,9 +62,9 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> output = MultiLabelConfusionMatrix(num_classes=3) - >>> # output.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', output.result().numpy()) + >>> metric = MultiLabelConfusionMatrix(num_classes=3) + >>> # metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', metric.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From f3f920e2adc5cb9e31a84f953164553e0cf5855b Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:29:59 +0500 Subject: [PATCH 207/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 7ec50a66de..bc5d92d691 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -62,7 +62,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> metric = MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) From cf1c24b7d1c2efdc3c949424706c7ffebd9d9663 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:32:10 +0500 Subject: [PATCH 208/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 9f56f158c5..9227be1080 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -48,10 +48,10 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]], ... dtype=tf.float32) >>> # Matthews correlation coefficient - >>> mcc = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> mcc.update_state(actuals, preds) + >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) + >>> metric.update_state(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', mcc.result().numpy()) + >>> # print('Matthews correlation coefficient is:', metric.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 3349e872bced972be3b51cbb3ff7c43fb642eea2 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:46:05 +0500 Subject: [PATCH 209/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 49 +++++++++++++--------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 8519ccdf2c..a55ec61be0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,8 +42,8 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> result = hamming_distance(actuals, predictions) - >>> #print('Hamming distance: ', result.numpy()) + >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> #print('Hamming distance: ', metric.numpy()) """ result = tf.not_equal(actuals, predictions) @@ -83,31 +83,26 @@ def hamming_loss_fn( Usage: - ```python - # multi-class hamming loss - hl = HammingLoss(mode='multiclass', threshold=0.6) - actuals = tf.constant([[1, 0, 0, 0],[0, 0, 1, 0], - [0, 0, 0, 1],[0, 1, 0, 0]], - dtype=tf.float32) - predictions = tf.constant([[0.8, 0.1, 0.1, 0], - [0.2, 0, 0.8, 0], - [0.05, 0.05, 0.1, 0.8], - [1, 0, 0, 0]], - dtype=tf.float32) - hl.update_state(actuals, predictions) - print('Hamming loss: ', hl.result().numpy()) # 0.25 - - # multi-label hamming loss - hl = HammingLoss(mode='multilabel', threshold=0.8) - actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], - [0, 0, 0,1]], dtype=tf.int32) - predictions = tf.constant([[0.82, 0.5, 0.90, 0], - [0, 1, 0.4, 0.98], - [0.89, 0.79, 0, 0.3]], - dtype=tf.float32) - hl.update_state(actuals, predictions) - print('Hamming loss: ', hl.result().numpy()) # 0.16666667 - ``` + >>> # multi-class hamming loss + >>> hl = HammingLoss(mode='multiclass', threshold=0.6) + >>> actuals = tf.constant([[1, 0, 0, 0],[0, 0, 1, 0], + ... [0, 0, 0, 1],[0, 1, 0, 0]], dtype=tf.float32) + >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], + ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], + ... dtype=tf.float32) + >>> hl.update_state(y_true = actuals, y_pred = predictions) + >>> #uncomment the line below to see the result + >>> #print('Hamming loss: ', hl.result().numpy()) # 0.25 + + >>> # multi-label hamming loss + >>> hl = HammingLoss(mode='multilabel', threshold=0.8) + >>> actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], + ... [0, 0, 0,1]], dtype=tf.int32) + >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], + ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) + >>> hl.update_state(y_true = actuals, y_pred = predictions) + >>> #print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + """ if mode not in ["multiclass", "multilabel"]: raise TypeError("mode must be either multiclass or multilabel]") From c4a0d61b14bb19e128fbbef36f4aee3e6f523b0d Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 09:47:35 +0500 Subject: [PATCH 210/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index bce8e15509..a123a9f224 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -35,9 +35,9 @@ class GeometricMean(Metric): Usage: - >>> m = tfa.metrics.GeometricMean() - >>> m.update_state([1, 3, 5, 7, 9]) - >>> m.result().numpy() + >>> metric = tfa.metrics.GeometricMean() + >>> metric.update_state([1, 3, 5, 7, 9]) + >>> metric.result().numpy() 3.9362833 """ From ad3e74461cd28abbf68d01bbdfcfe595ecec7ea7 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:17:35 +0500 Subject: [PATCH 211/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index b2bd42cba7..cce36ef738 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -50,7 +50,7 @@ class CohenKappa(Metric): >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> metric.update_state(actuals, preds, sample_weight=weights) + >>> # metric.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 From 1e784d25bbf97f74389ca4d950c8d4a9db897ea6 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:38:34 +0500 Subject: [PATCH 212/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index a55ec61be0..6776077c2f 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,9 +90,9 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - >>> hl.update_state(y_true = actuals, y_pred = predictions) - >>> #uncomment the line below to see the result - >>> #print('Hamming loss: ', hl.result().numpy()) # 0.25 + >>> # hl.update_state(actuals, predictions) + >>> # uncomment the line below to see the result + >>> # print('Hamming loss: ', hl.result().numpy()) # 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -100,8 +100,8 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - >>> hl.update_state(y_true = actuals, y_pred = predictions) - >>> #print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + >>> # hl.update_state(actuals, predictions) + >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From 4684cbea6da5cb72ff9e719c267f9fd53684779f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:56:40 +0500 Subject: [PATCH 213/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index bc5d92d691..7cdd9deb26 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -51,7 +51,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) - >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) @@ -62,7 +62,7 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) - >>> metric = tfa.metric.MultiLabelConfusionMatrix(num_classes=3) + >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> # metric.update_state(y_true, y_pred) >>> # print('Confusion matrix:', metric.result().numpy()) From 9aa6a97dd5e3f78c37410bb9f2754de925f69178 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 6 Sep 2020 10:58:48 +0500 Subject: [PATCH 214/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 6776077c2f..b7ccaa85d6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> metric = hamming_distance(actuals, predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From b9a205765d05c4dc2e5141e10f2e7476ebc353bc Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 09:58:24 +0500 Subject: [PATCH 215/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index b7ccaa85d6..6776077c2f 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(actuals, predictions) + >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From 6b767ead7a3fa1dcf10e63e2d69f4fac22ae2ae2 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:13:50 +0500 Subject: [PATCH 216/259] y_true, y_pred are not accepted in hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 6776077c2f..b7ccaa85d6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -42,7 +42,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) - >>> metric = hamming_distance(y_true = actuals, y_pred = predictions) + >>> metric = hamming_distance(actuals, predictions) >>> #print('Hamming distance: ', metric.numpy()) """ From 1d74c2e42fc784c699ee5504544638f2c6ee8f92 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:24:37 +0500 Subject: [PATCH 217/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index b7ccaa85d6..9f2a78e7b0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,9 +90,10 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - >>> # hl.update_state(actuals, predictions) + + >>> hl.update_state(actuals, predictions) >>> # uncomment the line below to see the result - >>> # print('Hamming loss: ', hl.result().numpy()) # 0.25 + >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -100,7 +101,8 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - >>> # hl.update_state(actuals, predictions) + + >>> hl.update_state(actuals, predictions) >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ From 45145cf76591b7b600b492f2f5dac3269d0bf6ed Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 10:28:11 +0500 Subject: [PATCH 218/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 9f2a78e7b0..c4a15911a0 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -90,7 +90,7 @@ def hamming_loss_fn( >>> predictions = tf.constant([[0.8, 0.1, 0.1, 0], ... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]], ... dtype=tf.float32) - + >>> hl.update_state(actuals, predictions) >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 @@ -101,7 +101,7 @@ def hamming_loss_fn( ... [0, 0, 0,1]], dtype=tf.int32) >>> predictions = tf.constant([[0.82, 0.5, 0.90, 0], ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) - + >>> hl.update_state(actuals, predictions) >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 From 09c6fb8b0b7572a3814693a18523939975c378ab Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:04:52 +0500 Subject: [PATCH 219/259] assigned metric.update_Status to result --- tensorflow_addons/metrics/cohens_kappa.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cce36ef738..356f958b51 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,13 +44,13 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # metric.update_state(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', metric.result().numpy()) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.result().numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> # metric.update_state(actuals, preds, sample_weight=weights) + >>> result = metric.update_state(actuals, preds, sample_weight=weights) >>> # print('Final result: ', metric.result().numpy()) >>> # Final result: 0.37209308 From e49474e1f3ab12f4ef94b34d5a94bbcaedb64a90 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:12:16 +0500 Subject: [PATCH 220/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 9227be1080..fa0570bcd9 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -49,9 +49,9 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ... dtype=tf.float32) >>> # Matthews correlation coefficient >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', metric.result().numpy()) + >>> # print('Matthews correlation coefficient is:', result.result().numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 4cd4ae65672357be7f873aa238801089c8adb8f0 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:14:40 +0500 Subject: [PATCH 221/259] assigned m.update_status (yactual,ypred) to result --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 7cdd9deb26..de34960af6 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> # metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', metric.result().numpy()) + >>> result = metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', result.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -63,8 +63,8 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> # metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', metric.result().numpy()) + >>> result = metric.update_state(y_true, y_pred) + >>> # print('Confusion matrix:', result.result().numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From f29455bd895317390b0475effaa255062737b20a Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:15:53 +0500 Subject: [PATCH 222/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 3002bb051f..b5a510bede 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,7 +64,7 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> metric = tfa.metrics.r_square.RSquare() - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric.update_state(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result >>> # print('R^2 score is: ', result(actuals, preds).numpy()) >>> # R^2 score is: 0.57142866 From 095e814ac97c63f1551efb1ccd4940cdf38c1513 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:27:27 +0500 Subject: [PATCH 223/259] Update r_square.py --- tensorflow_addons/metrics/r_square.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index b5a510bede..d1592c77b4 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -64,9 +64,10 @@ class RSquare(Metric): >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) >>> metric = tfa.metrics.r_square.RSquare() - >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) >>> # Uncomment the lines below to see the result - >>> # print('R^2 score is: ', result(actuals, preds).numpy()) + >>> # print('R^2 score is: ', result.numpy()) >>> # R^2 score is: 0.57142866 """ From 8cef4bbdaad11ba302acb1476213b16db4afa76c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:31:38 +0500 Subject: [PATCH 224/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index de34960af6..c4ec7d5c90 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -52,8 +52,9 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> result = metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', result.result().numpy()) + >>> metric.update_state(y_true, y_pred) + >>> result = metric(y_true, y_pred) + >>> # print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -63,8 +64,9 @@ class MultiLabelConfusionMatrix(Metric): >>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) - >>> result = metric.update_state(y_true, y_pred) - >>> # print('Confusion matrix:', result.result().numpy()) + >>> metric.update_state(y_true, y_pred) + >>> result = metric(y_true, y_pred) + >>> # print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From ee80f3692c1edb516786c04647216fb8aefff6f9 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:32:39 +0500 Subject: [PATCH 225/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index fa0570bcd9..cc5c9f4c15 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -49,9 +49,10 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): ... dtype=tf.float32) >>> # Matthews correlation coefficient >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) - >>> result = metric.update_state(y_true = actuals, y_pred = preds) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', result.result().numpy()) + >>> # print('Matthews correlation coefficient is:', result.numpy()) >>> # Matthews correlation coefficient is : -0.33333334 """ From 4f37fda1538e6cb83922e98566081cf5304c8374 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:34:04 +0500 Subject: [PATCH 226/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index a123a9f224..9e6f6ea648 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -38,7 +38,7 @@ class GeometricMean(Metric): >>> metric = tfa.metrics.GeometricMean() >>> metric.update_state([1, 3, 5, 7, 9]) >>> metric.result().numpy() - 3.9362833 + >>> # 3.9362833 """ From 31c87421a68756731487b0afca60ddb5f5aaa0a6 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:37:56 +0500 Subject: [PATCH 227/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 356f958b51..612aec63e3 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -44,14 +44,16 @@ class CohenKappa(Metric): >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> result = metric.update_state(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.result().numpy()) + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.numpy()) >>> # Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> result = metric.update_state(actuals, preds, sample_weight=weights) - >>> # print('Final result: ', metric.result().numpy()) + >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) + >>> result = metric(y_true = actuals, y_pred = preds) + >>> # print('Final result: ', result.numpy()) >>> # Final result: 0.37209308 Usage with tf.keras API: From 83660c8e5080445e83b4415c213a4045e3438bfd Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:43:52 +0500 Subject: [PATCH 228/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index 9e6f6ea648..a123a9f224 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -38,7 +38,7 @@ class GeometricMean(Metric): >>> metric = tfa.metrics.GeometricMean() >>> metric.update_state([1, 3, 5, 7, 9]) >>> metric.result().numpy() - >>> # 3.9362833 + 3.9362833 """ From 4e83541bbffb87fa462363a4ef3b86087af10e1c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:49:59 +0500 Subject: [PATCH 229/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index c4a15911a0..e6733781bd 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,7 +43,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1], ... dtype=tf.int32) >>> metric = hamming_distance(actuals, predictions) - >>> #print('Hamming distance: ', metric.numpy()) + >>> print('Hamming distance: ', metric.numpy()) """ result = tf.not_equal(actuals, predictions) @@ -92,6 +92,7 @@ def hamming_loss_fn( ... dtype=tf.float32) >>> hl.update_state(actuals, predictions) + tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 @@ -103,7 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> # print('Hamming loss: ', hl.result().numpy()) # 0.16666667 + >>> print('Hamming loss: ', hl.result().numpy()) # 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From 1b961edbf29e8611af2e1c77e76fbcd51d99a7ce Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:53:39 +0500 Subject: [PATCH 230/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 612aec63e3..86518c41a1 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,16 +45,17 @@ class CohenKappa(Metric): >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds) + tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.numpy()) - >>> # Final result: 0.61904764 + >>>print('Final result: ', result.numpy()) + Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # print('Final result: ', result.numpy()) - >>> # Final result: 0.37209308 + >>> print('Final result: ', result.numpy()) + Final result: 0.37209308 Usage with tf.keras API: From 6957efbda035c2bd17266a20f53fe59c039f7bd3 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:58:19 +0500 Subject: [PATCH 231/259] Update matthews_correlation_coefficient.py --- .../metrics/matthews_correlation_coefficient.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index cc5c9f4c15..c1ba9e2f96 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,9 +51,8 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> metric.update_state(y_true = actuals, y_pred = preds) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # uncomment the lines below to check the result - >>> # print('Matthews correlation coefficient is:', result.numpy()) - >>> # Matthews correlation coefficient is : -0.33333334 + >>> print('Matthews correlation coefficient is:', result.numpy()) + Matthews correlation coefficient is : -0.33333334 """ From 058cea42bc6c3a96a59b3a277ed707d1cfd5a210 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:08:10 +0500 Subject: [PATCH 232/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index c4ec7d5c90..8a067f7bb9 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -54,7 +54,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> # print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] @@ -66,7 +66,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> # print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] From a3cf087f74d1f48c6e8f55840726be786c7f358f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:44:49 +0500 Subject: [PATCH 233/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 8a067f7bb9..be3bf82a2e 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,8 +55,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] + Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -67,8 +66,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - - >>> # Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] """ From 8d57debe5b083a0202ece79f700024989bad8330 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:45:25 +0500 Subject: [PATCH 234/259] Update matthews_correlation_coefficient.py --- tensorflow_addons/metrics/matthews_correlation_coefficient.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index c1ba9e2f96..68cc2ff9e9 100644 --- a/tensorflow_addons/metrics/matthews_correlation_coefficient.py +++ b/tensorflow_addons/metrics/matthews_correlation_coefficient.py @@ -51,8 +51,8 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric): >>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1) >>> metric.update_state(y_true = actuals, y_pred = preds) >>> result = metric(y_true = actuals, y_pred = preds) - >>> print('Matthews correlation coefficient is:', result.numpy()) - Matthews correlation coefficient is : -0.33333334 + >>> print('Matthews correlation coefficient is :', result.numpy()) + Matthews correlation coefficient is : [-0.33333334] """ From 48d451685a4727ab832ce11635b643570a64889f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:53:07 +0500 Subject: [PATCH 235/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index e6733781bd..4e8e914f44 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -44,7 +44,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: ... dtype=tf.int32) >>> metric = hamming_distance(actuals, predictions) >>> print('Hamming distance: ', metric.numpy()) - + Hamming distance: 0.3 """ result = tf.not_equal(actuals, predictions) not_eq = tf.reduce_sum(tf.cast(result, tf.float32)) @@ -94,8 +94,8 @@ def hamming_loss_fn( >>> hl.update_state(actuals, predictions) tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result - >>> print('Hamming loss: ', hl.result().numpy()) # 0.25 - + >>> print('Hamming loss: ', hl.result().numpy()) + Hamming loss: 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) >>> actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1], @@ -104,8 +104,8 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> print('Hamming loss: ', hl.result().numpy()) # 0.16666667 - + >>> print('Hamming loss: ', hl.result().numpy()) + Hamming loss: 0.16666667 """ if mode not in ["multiclass", "multilabel"]: raise TypeError("mode must be either multiclass or multilabel]") From c774e0f702954f926c2dd938a33259dfeae05911 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 00:57:41 +0500 Subject: [PATCH 236/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 4e8e914f44..21bb0c3ccb 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -94,7 +94,7 @@ def hamming_loss_fn( >>> hl.update_state(actuals, predictions) tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 >>> # uncomment the line below to see the result - >>> print('Hamming loss: ', hl.result().numpy()) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.25 >>> # multi-label hamming loss >>> hl = HammingLoss(mode='multilabel', threshold=0.8) @@ -104,7 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> print('Hamming loss: ', hl.result().numpy()) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.16666667 """ if mode not in ["multiclass", "multilabel"]: From 5540c9ac2ae6232fa0f3a5db7aca1d2ac5ba6750 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:05:16 +0500 Subject: [PATCH 237/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 86518c41a1..e42883a4c7 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -47,7 +47,7 @@ class CohenKappa(Metric): >>> metric.update_state(y_true = actuals, y_pred = preds) tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) >>> result = metric(y_true = actuals, y_pred = preds) - >>>print('Final result: ', result.numpy()) + >>> print('Final result: ', result.numpy()) Final result: 0.61904764 >>> # To use this with weights, sample_weight argument can be used. From 987b2b655f830bc999f7e25131ef2201d21a4e84 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:08:40 +0500 Subject: [PATCH 238/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 21bb0c3ccb..cffde3014e 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -92,7 +92,7 @@ def hamming_loss_fn( ... dtype=tf.float32) >>> hl.update_state(actuals, predictions) - tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0 + >>> # uncomment the line below to see the result >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.25 From a4b1f4536a0861d2f6187d1235c8be9311a771a7 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:27:24 +0500 Subject: [PATCH 239/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index e42883a4c7..00faf0dc59 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -45,7 +45,12 @@ class CohenKappa(Metric): >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds) - tf.Tensor: shape=(5, 5), dtype=float32, numpy = array([[0., 0., 0., 0., 0.],[0., 2., 0., 0., 0.],[0., 0., 0., 0., 1.],[0., 0., 0., 1., 0.], [0., 0., 1., 0., 3.]], dtype=float32) + >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) Final result: 0.61904764 From 4135dcbc5fb49906beaf8b364053a23b4cd1b433 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:29:14 +0500 Subject: [PATCH 240/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index cffde3014e..63c40f26e6 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -104,6 +104,7 @@ def hamming_loss_fn( ... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32) >>> hl.update_state(actuals, predictions) + >>> print('Hamming loss: ', hl.result().numpy()) Hamming loss: 0.16666667 """ From c22457a822e675807d63993e9229e56b2d6cc171 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:35:27 +0500 Subject: [PATCH 241/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index be3bf82a2e..f26ff0f7a3 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -55,7 +55,14 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]] [[0 1] [1 0]]] + Confusion matrix: [[[2. 0.] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -66,7 +73,14 @@ class MultiLabelConfusionMatrix(Metric): >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]] + Confusion matrix: [[[2. 0.] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ From 7bca624c5cf7a3a0c291029dbb88c776432ea6e8 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 01:59:54 +0500 Subject: [PATCH 242/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 00faf0dc59..5aaa800a89 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -58,6 +58,12 @@ class CohenKappa(Metric): >>> # To use this with weights, sample_weight argument can be used. >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) >>> metric.update_state(y_true = actuals, y_pred = preds, sample_weight=weights) + >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) Final result: 0.37209308 From 8dab2dea9e518cfe1ccbd2fbb335d09b83af034c Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:14:01 +0500 Subject: [PATCH 243/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index f26ff0f7a3..693ce6a857 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,14 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] - + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From 193d93f2938a0c94e44991735a9288cf1432c4e3 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:26:36 +0500 Subject: [PATCH 244/259] Update cohens_kappa.py --- tensorflow_addons/metrics/cohens_kappa.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 5aaa800a89..c3d459563c 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -66,7 +66,7 @@ class CohenKappa(Metric): [ 0., 0., 2., 0., 7.]], dtype=float32)> >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) - Final result: 0.37209308 + Final result: 0.42080373 Usage with tf.keras API: From 39795109722965f2e3d1f309b5ced98da805fc77 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 02:29:20 +0500 Subject: [PATCH 245/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 693ce6a857..357a4aab67 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,12 +56,12 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [0. 2.]] - - [[0. 2.] + [[2. 0.] + [0. 2.]] + + [[0. 2.] [2. 0.]]] >>> # if multiclass input is provided @@ -74,12 +74,12 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] - [[2. 0.] - [2. 0.]] - - [[2. 2.] + [[2. 2.] [0. 0.]]] """ From 4e1ec0d65daca8658a4b3e980888a4e43be5b50e Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 08:29:29 +0500 Subject: [PATCH 246/259] Update multilabel_confusion_matrix.py --- .../metrics/multilabel_confusion_matrix.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 357a4aab67..9ca016fc9e 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From 857a2b9a6a4e4f55d70f7e2c249687fefe2568e2 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 08:32:01 +0500 Subject: [PATCH 247/259] unified diff --- .../metrics/multilabel_confusion_matrix.py | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 9ca016fc9e..aa7e131d1d 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [0. 2.]] - - [[0. 2.] - [2. 0.]]] + [0. 2.]] + + [[2. 0.] + [0. 2.]] + + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] - - [[2. 0.] - [2. 0.]] - - [[2. 2.] - [0. 0.]]] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] """ @typechecked From 109f211da035ed528c45d8c601bfcfef6a09e354 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 15 Sep 2020 09:05:46 +0500 Subject: [PATCH 248/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index aa7e131d1d..fe25dfb4e7 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,7 +56,7 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] [[2. 0.] [0. 2.]] @@ -74,7 +74,7 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] [[2. 0.] [2. 0.]] From 7ebb7419aa7d3053773dac433ec2366fbe5b9594 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Thu, 17 Sep 2020 13:47:47 +0500 Subject: [PATCH 249/259] fixed example --- .../metrics/multilabel_confusion_matrix.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index fe25dfb4e7..357a4aab67 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -56,13 +56,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [0. 2.]] + [[2. 0.] + [0. 2.]] - [[0. 2.] - [2. 0.]]] + [[0. 2.] + [2. 0.]]] >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -74,13 +74,13 @@ class MultiLabelConfusionMatrix(Metric): >>> result = metric(y_true, y_pred) >>> print('Confusion matrix:', result.numpy()) Confusion matrix: [[[2. 0.] - [0. 2.]] + [0. 2.]] - [[2. 0.] - [2. 0.]] + [[2. 0.] + [2. 0.]] - [[2. 2.] - [0. 0.]]] + [[2. 2.] + [0. 0.]]] """ @typechecked From 3f1a30a9d052025c5f6333323a331860b6549a4f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 20 Sep 2020 22:03:44 +0500 Subject: [PATCH 250/259] added support for blankline --- .../metrics/multilabel_confusion_matrix.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 357a4aab67..89b1644e30 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,7 +45,7 @@ class MultiLabelConfusionMatrix(Metric): - false positives for class i in M(0,1) - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - + >>> # multilabel confusion matrix >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], ... dtype=tf.int32) @@ -54,15 +54,15 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> print('Confusion matrix:', result.numpy()) - Confusion matrix: [[[2. 0.] - [0. 2.]] + >>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE + array([[[2., 0.], + [0., 2.]], - [[2. 0.] - [0. 2.]] + [[2., 0.], + [0., 2.]], - [[0. 2.] - [2. 0.]]] + [[0., 2.], + [2., 0.]]], dtype=float32) >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], @@ -72,7 +72,7 @@ class MultiLabelConfusionMatrix(Metric): >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) >>> result = metric(y_true, y_pred) - >>> print('Confusion matrix:', result.numpy()) + >>> print('Confusion matrix:', result.numpy()) #doctest: -DONT_ACCEPT_BLANKLINE Confusion matrix: [[[2. 0.] [0. 2.]] From 568f75c4ed67a9e41e388c23d642ce45d90fb14f Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 20 Sep 2020 22:23:47 +0500 Subject: [PATCH 251/259] 'space' --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 89b1644e30..33865a5411 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,7 +45,7 @@ class MultiLabelConfusionMatrix(Metric): - false positives for class i in M(0,1) - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - + >>> # multilabel confusion matrix >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], ... dtype=tf.int32) From eaf045a40541985e07b3d1f19d50d5f6126fdedc Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 20:30:40 +0500 Subject: [PATCH 252/259] update metrics cohen kappa --- tensorflow_addons/metrics/cohens_kappa.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index c3d459563c..19f61e61a0 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,7 +38,6 @@ class CohenKappa(Metric): Usage: - >>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) >>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32) >>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32) From 6b567f35d20559e84cb364d19b88ddfb294543ba Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:30:54 +0500 Subject: [PATCH 253/259] Update hamming.py --- tensorflow_addons/metrics/hamming.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 63c40f26e6..a0390f379f 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -45,6 +45,7 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor: >>> metric = hamming_distance(actuals, predictions) >>> print('Hamming distance: ', metric.numpy()) Hamming distance: 0.3 + """ result = tf.not_equal(actuals, predictions) not_eq = tf.reduce_sum(tf.cast(result, tf.float32)) From ac6b80b98e1561658b201c8f9a20eb5e58d5a55e Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Wed, 2 Sep 2020 21:46:15 +0500 Subject: [PATCH 254/259] Update multilabel_confusion_matrix.py --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 33865a5411..4a79b341ed 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -81,6 +81,7 @@ class MultiLabelConfusionMatrix(Metric): [[2. 2.] [0. 0.]]] + """ @typechecked From f0b20ef2f319b40f20eeb4051b942ee012c4ae49 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 14:34:04 +0500 Subject: [PATCH 255/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index a123a9f224..9e6f6ea648 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -38,7 +38,7 @@ class GeometricMean(Metric): >>> metric = tfa.metrics.GeometricMean() >>> metric.update_state([1, 3, 5, 7, 9]) >>> metric.result().numpy() - 3.9362833 + >>> # 3.9362833 """ From a43194b09ee3b1892ec4f6a50a50502165dce869 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 14 Sep 2020 23:43:52 +0500 Subject: [PATCH 256/259] Update geometric_mean.py --- tensorflow_addons/metrics/geometric_mean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/geometric_mean.py b/tensorflow_addons/metrics/geometric_mean.py index 9e6f6ea648..a123a9f224 100644 --- a/tensorflow_addons/metrics/geometric_mean.py +++ b/tensorflow_addons/metrics/geometric_mean.py @@ -38,7 +38,7 @@ class GeometricMean(Metric): >>> metric = tfa.metrics.GeometricMean() >>> metric.update_state([1, 3, 5, 7, 9]) >>> metric.result().numpy() - >>> # 3.9362833 + 3.9362833 """ From 796d4cbb19d9f446528f2ffc77ff63df4cd19f49 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 20 Sep 2020 22:03:44 +0500 Subject: [PATCH 257/259] added support for blankline --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 4a79b341ed..df22719436 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,7 +45,7 @@ class MultiLabelConfusionMatrix(Metric): - false positives for class i in M(0,1) - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - + >>> # multilabel confusion matrix >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], ... dtype=tf.int32) From 4a0edb41a1502f0499433e385ba18abfbf81dd62 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Sun, 20 Sep 2020 22:23:47 +0500 Subject: [PATCH 258/259] 'space' --- tensorflow_addons/metrics/multilabel_confusion_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index df22719436..4a79b341ed 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,7 +45,7 @@ class MultiLabelConfusionMatrix(Metric): - false positives for class i in M(0,1) - false negatives for class i in M(1,0) - true positives for class i in M(1,1) - + >>> # multilabel confusion matrix >>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], ... dtype=tf.int32) From e7c574c5d64c2ff7dfdccd3ac79c31f7857f81b0 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 21 Sep 2020 13:33:10 +0500 Subject: [PATCH 259/259] r_square file --- tensorflow_addons/metrics/r_square.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index d1592c77b4..5e98869c96 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -66,9 +66,8 @@ class RSquare(Metric): >>> metric = tfa.metrics.r_square.RSquare() >>> metric.update_state(y_true = actuals, y_pred = preds) >>> result = metric(y_true = actuals, y_pred = preds) - >>> # Uncomment the lines below to see the result - >>> # print('R^2 score is: ', result.numpy()) - >>> # R^2 score is: 0.57142866 + >>> print('R^2 score is: ', result.numpy()) + R^2 score is: 0.57142854 """