From 95bc563d8084d4c91fa929f55c994de708c1a0a6 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 21 Sep 2020 21:38:22 +0500 Subject: [PATCH 1/3] all_done --- tensorflow_addons/metrics/cohens_kappa.py | 52 +++++++++------ tensorflow_addons/metrics/hamming.py | 64 +++++++++---------- .../matthews_correlation_coefficient.py | 24 +++---- .../metrics/multilabel_confusion_matrix.py | 60 ++++++++++------- tensorflow_addons/metrics/r_square.py | 16 +++-- 5 files changed, 120 insertions(+), 96 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cb473530b7..04191d61d3 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,28 +38,40 @@ 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 - ``` - + >>> 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) + >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) + >>> 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) + >>> 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.42080373 + Usage with tf.keras API: + >>> 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=[tfa.metrics.CohenKappa(num_classes=3, sparse_labels=True)]) - ```python - model = tf.keras.models.Model(inputs, outputs) - model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs)) - model.compile('sgd', loss='mse') - ``` """ @typechecked diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index 9702b030ea..fb5ac1e994 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -38,14 +38,14 @@ 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) + >>> 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)) @@ -84,31 +84,29 @@ 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(actuals, predictions) + + >>> # uncomment the line below to see the result + >>> 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], + ... [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()) + Hamming loss: 0.16666667 + """ if mode not in ["multiclass", "multilabel"]: raise TypeError("mode must be either multiclass or multilabel]") diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index e6afa53f61..68cc2ff9e9 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 + >>> 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] + """ @typechecked diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index d13a355b33..4900d87352 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -45,31 +45,43 @@ 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) + Usage: + + >>> # 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) + >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) + >>> metric.update_state(y_true, y_pred) + >>> result = metric(y_true, y_pred) + >>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE + array([[[2., 0.], + [0., 2.]], + + [[2., 0.], + [0., 2.]], + + [[0., 2.], + [2., 0.]]], dtype=float32) + >>> # 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) + >>> 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()) #doctest: -DONT_ACCEPT_BLANKLINE + Confusion matrix: [[[2. 0.] + [0. 2.]] + + [[2. 0.] + [2. 0.]] + + [[2. 2.] + [0. 0.]]] - ```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]]] - ``` """ @typechecked diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 04c3866dbc..466b722ab2 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -60,13 +60,15 @@ 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) + >>> metric = tfa.metrics.r_square.RSquare() + >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> result = metric(y_true = actuals, y_pred = preds) + >>> print('R^2 score is: ', result.numpy()) + R^2 score is: 0.57142854 + """ @typechecked From 4834a745851cbd8bcc37ba8df99473ff90024ee9 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Mon, 21 Sep 2020 22:03:49 +0500 Subject: [PATCH 2/3] cohen_kapa_space --- 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 04191d61d3..9530d63341 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -64,7 +64,7 @@ class CohenKappa(Metric): >>> result = metric(y_true = actuals, y_pred = preds) >>> print('Final result: ', result.numpy()) Final result: 0.42080373 - + Usage with tf.keras API: >>> inputs = tf.keras.Input(shape=(10,)) >>> x = tf.keras.layers.Dense(10)(inputs) From 417204938aa51fdd85932eb0b8a6e0f8834ff7a7 Mon Sep 17 00:00:00 2001 From: nataliyah123 Date: Tue, 22 Sep 2020 13:03:27 +0500 Subject: [PATCH 3/3] requested changes --- tensorflow_addons/metrics/cohens_kappa.py | 21 ++++++------- tensorflow_addons/metrics/geometric_mean.py | 6 ++-- tensorflow_addons/metrics/hamming.py | 13 ++++---- .../matthews_correlation_coefficient.py | 4 +-- .../metrics/multilabel_confusion_matrix.py | 30 +++++++++---------- tensorflow_addons/metrics/r_square.py | 12 ++++---- 6 files changed, 43 insertions(+), 43 deletions(-) diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index 9530d63341..68ab834c13 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,34 +38,35 @@ 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) + >>> y_true = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32) + >>> y_pred = 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) >>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True) - >>> metric.update_state(y_true = actuals, y_pred = preds) + >>> metric.update_state(y_true , y_pred) - >>> result = metric(y_true = actuals, y_pred = preds) - >>> print('Final result: ', result.numpy()) - Final result: 0.61904764 + >>> result = metric.result() + >>> result.numpy() + 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) + >>> metric.update_state(y_true , y_pred , sample_weight=weights) - >>> result = metric(y_true = actuals, y_pred = preds) - >>> print('Final result: ', result.numpy()) - Final result: 0.42080373 + >>> result = metric.result() + >>> result.numpy() + 0.37209308 Usage with tf.keras API: + >>> inputs = tf.keras.Input(shape=(10,)) >>> x = tf.keras.layers.Dense(10)(inputs) >>> outputs = tf.keras.layers.Dense(1)(x) 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 """ diff --git a/tensorflow_addons/metrics/hamming.py b/tensorflow_addons/metrics/hamming.py index fb5ac1e994..5f6f5ae699 100644 --- a/tensorflow_addons/metrics/hamming.py +++ b/tensorflow_addons/metrics/hamming.py @@ -43,8 +43,8 @@ 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()) - Hamming distance: 0.3 + >>> metric.numpy() + 0.3 """ result = tf.not_equal(actuals, predictions) @@ -93,9 +93,8 @@ def hamming_loss_fn( ... dtype=tf.float32) >>> hl.update_state(actuals, predictions) - >>> # uncomment the line below to see the result - >>> print('Hamming loss: ', hl.result().numpy()) - Hamming loss: 0.25 + >>> 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], @@ -104,8 +103,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()) - Hamming loss: 0.16666667 + >>> hl.result().numpy() + 0.16666667 """ if mode not in ["multiclass", "multilabel"]: diff --git a/tensorflow_addons/metrics/matthews_correlation_coefficient.py b/tensorflow_addons/metrics/matthews_correlation_coefficient.py index 68cc2ff9e9..ee1fdd81f1 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] + >>> result.numpy() + array([-0.33333334], dtype=float32) """ diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index 4900d87352..b1a8b4ff45 100644 --- a/tensorflow_addons/metrics/multilabel_confusion_matrix.py +++ b/tensorflow_addons/metrics/multilabel_confusion_matrix.py @@ -54,16 +54,16 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3) >>> metric.update_state(y_true, y_pred) - >>> result = metric(y_true, y_pred) + >>> result = metric.result() >>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE - array([[[2., 0.], - [0., 2.]], + array([[[1., 0.], + [0., 1.]], - [[2., 0.], - [0., 2.]], + [[1., 0.], + [0., 1.]], - [[0., 2.], - [2., 0.]]], dtype=float32) + [[0., 1.], + [1., 0.]]], dtype=float32) >>> # if multiclass input is provided >>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], ... dtype=tf.int32) @@ -71,16 +71,16 @@ class MultiLabelConfusionMatrix(Metric): ... dtype=tf.int32) >>> 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()) #doctest: -DONT_ACCEPT_BLANKLINE - Confusion matrix: [[[2. 0.] - [0. 2.]] + >>> result = metric.result() + >>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE + array([[[1., 0.], + [0., 1.]], - [[2. 0.] - [2. 0.]] + [[1., 0.], + [1., 0.]], - [[2. 2.] - [0. 0.]]] + [[1., 1.], + [0., 0.]]], dtype=float32) """ diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 466b722ab2..cea83e3a8a 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -61,13 +61,13 @@ class RSquare(Metric): Usage: - >>> actuals = tf.constant([1, 4, 3], dtype=tf.float32) - >>> preds = tf.constant([2, 4, 4], dtype=tf.float32) + >>> y_true = tf.constant([1, 4, 3], dtype=tf.float32) + >>> y_pred = 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(y_true = actuals, y_pred = preds) - >>> print('R^2 score is: ', result.numpy()) - R^2 score is: 0.57142854 + >>> metric.update_state(y_true, y_pred) + >>> result = metric.result() + >>> result.numpy() + 0.57142854 """