Skip to content

#2066-doc-update-metrics-latest #2171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 32 additions & 19 deletions tensorflow_addons/metrics/cohens_kappa.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
<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.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)
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
array([[ 0., 0., 0., 0., 0.],
[ 0., 6., 0., 0., 0.],
[ 0., 0., 0., 0., 10.],
[ 0., 0., 0., 2., 0.],
[ 0., 0., 2., 0., 7.]], dtype=float32)>
>>> 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
Expand Down
6 changes: 3 additions & 3 deletions tensorflow_addons/metrics/geometric_mean.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

"""
Expand Down
63 changes: 30 additions & 33 deletions tensorflow_addons/metrics/hamming.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
<tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0>
>>> 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)
<tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=3.0>
>>> hl.result().numpy()
0.16666667

"""
if mode not in ["multiclass", "multilabel"]:
raise TypeError("mode must be either multiclass or multilabel]")
Expand Down
24 changes: 12 additions & 12 deletions tensorflow_addons/metrics/matthews_correlation_coefficient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 36 additions & 24 deletions tensorflow_addons/metrics/multilabel_confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.]],
<BLANKLINE>
[[1., 0.],
[0., 1.]],
<BLANKLINE>
[[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.]],
<BLANKLINE>
[[1., 0.],
[1., 0.]],
<BLANKLINE>
[[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
Expand Down
16 changes: 9 additions & 7 deletions tensorflow_addons/metrics/r_square.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down