diff --git a/tensorflow_addons/metrics/cohens_kappa.py b/tensorflow_addons/metrics/cohens_kappa.py index cb473530b7..68ab834c13 100644 --- a/tensorflow_addons/metrics/cohens_kappa.py +++ b/tensorflow_addons/metrics/cohens_kappa.py @@ -38,28 +38,41 @@ 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 - ``` + >>> 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 , y_pred) + + >>> 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 , y_pred , sample_weight=weights) + + >>> result = metric.result() + >>> result.numpy() + 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') - ``` + >>> 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)]) + """ @typechecked 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 9702b030ea..5f6f5ae699 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) + >>> metric.numpy() + 0.3 + """ result = tf.not_equal(actuals, predictions) not_eq = tf.reduce_sum(tf.cast(result, tf.float32)) @@ -84,31 +84,28 @@ 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) + + >>> 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) + + >>> hl.result().numpy() + 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..ee1fdd81f1 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) + >>> result.numpy() + array([-0.33333334], dtype=float32) + """ @typechecked diff --git a/tensorflow_addons/metrics/multilabel_confusion_matrix.py b/tensorflow_addons/metrics/multilabel_confusion_matrix.py index d13a355b33..b1a8b4ff45 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.result() + >>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE + array([[[1., 0.], + [0., 1.]], + + [[1., 0.], + [0., 1.]], + + [[0., 1.], + [1., 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.result() + >>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE + array([[[1., 0.], + [0., 1.]], + + [[1., 0.], + [1., 0.]], + + [[1., 1.], + [0., 0.]]], dtype=float32) - ```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..cea83e3a8a 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 - ``` + + >>> 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, y_pred) + >>> result = metric.result() + >>> result.numpy() + 0.57142854 + """ @typechecked