Skip to content

Commit 710903f

Browse files
authored
#2066-doc-update-metrics-latest (#2171)
* all_done * cohen_kapa_space * requested changes
1 parent 13e40e6 commit 710903f

File tree

6 files changed

+122
-98
lines changed

6 files changed

+122
-98
lines changed

tensorflow_addons/metrics/cohens_kappa.py

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,41 @@ class CohenKappa(Metric):
3838
3939
Usage:
4040
41-
```python
42-
actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32)
43-
preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32)
44-
weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32)
45-
46-
m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True)
47-
m.update_state(actuals, preds)
48-
print('Final result: ', m.result().numpy()) # Result: 0.61904764
49-
50-
# To use this with weights, sample_weight argument can be used.
51-
m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True)
52-
m.update_state(actuals, preds, sample_weight=weights)
53-
print('Final result: ', m.result().numpy()) # Result: 0.37209308
54-
```
41+
>>> y_true = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32)
42+
>>> y_pred = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32)
43+
>>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32)
44+
>>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True)
45+
>>> metric.update_state(y_true , y_pred)
46+
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
47+
array([[0., 0., 0., 0., 0.],
48+
[0., 2., 0., 0., 0.],
49+
[0., 0., 0., 0., 1.],
50+
[0., 0., 0., 1., 0.],
51+
[0., 0., 1., 0., 3.]], dtype=float32)>
52+
>>> result = metric.result()
53+
>>> result.numpy()
54+
0.61904764
55+
>>> # To use this with weights, sample_weight argument can be used.
56+
>>> metric = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True)
57+
>>> metric.update_state(y_true , y_pred , sample_weight=weights)
58+
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
59+
array([[ 0., 0., 0., 0., 0.],
60+
[ 0., 6., 0., 0., 0.],
61+
[ 0., 0., 0., 0., 10.],
62+
[ 0., 0., 0., 2., 0.],
63+
[ 0., 0., 2., 0., 7.]], dtype=float32)>
64+
>>> result = metric.result()
65+
>>> result.numpy()
66+
0.37209308
5567
5668
Usage with tf.keras API:
5769
58-
```python
59-
model = tf.keras.models.Model(inputs, outputs)
60-
model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs))
61-
model.compile('sgd', loss='mse')
62-
```
70+
>>> inputs = tf.keras.Input(shape=(10,))
71+
>>> x = tf.keras.layers.Dense(10)(inputs)
72+
>>> outputs = tf.keras.layers.Dense(1)(x)
73+
>>> model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
74+
>>> model.compile('sgd', loss='mse', metrics=[tfa.metrics.CohenKappa(num_classes=3, sparse_labels=True)])
75+
6376
"""
6477

6578
@typechecked

tensorflow_addons/metrics/geometric_mean.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ class GeometricMean(Metric):
3535
3636
Usage:
3737
38-
>>> m = tfa.metrics.GeometricMean()
39-
>>> m.update_state([1, 3, 5, 7, 9])
40-
>>> m.result().numpy()
38+
>>> metric = tfa.metrics.GeometricMean()
39+
>>> metric.update_state([1, 3, 5, 7, 9])
40+
>>> metric.result().numpy()
4141
3.9362833
4242
4343
"""

tensorflow_addons/metrics/hamming.py

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def hamming_distance(actuals: TensorLike, predictions: TensorLike) -> tf.Tensor:
3838
3939
Usage:
4040
41-
```python
42-
actuals = tf.constant([1, 1, 0, 0, 1, 0, 1, 0, 0, 1],
43-
dtype=tf.int32)
44-
predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1],
45-
dtype=tf.int32)
46-
result = hamming_distance(actuals, predictions)
47-
print('Hamming distance: ', result.numpy())
48-
```
41+
>>> actuals = tf.constant([1, 1, 0, 0, 1, 0, 1, 0, 0, 1],
42+
... dtype=tf.int32)
43+
>>> predictions = tf.constant([1, 0, 0, 0, 1, 0, 0, 1, 0, 1],
44+
... dtype=tf.int32)
45+
>>> metric = hamming_distance(actuals, predictions)
46+
>>> metric.numpy()
47+
0.3
48+
4949
"""
5050
result = tf.not_equal(actuals, predictions)
5151
not_eq = tf.reduce_sum(tf.cast(result, tf.float32))
@@ -84,31 +84,28 @@ def hamming_loss_fn(
8484
8585
Usage:
8686
87-
```python
88-
# multi-class hamming loss
89-
hl = HammingLoss(mode='multiclass', threshold=0.6)
90-
actuals = tf.constant([[1, 0, 0, 0],[0, 0, 1, 0],
91-
[0, 0, 0, 1],[0, 1, 0, 0]],
92-
dtype=tf.float32)
93-
predictions = tf.constant([[0.8, 0.1, 0.1, 0],
94-
[0.2, 0, 0.8, 0],
95-
[0.05, 0.05, 0.1, 0.8],
96-
[1, 0, 0, 0]],
97-
dtype=tf.float32)
98-
hl.update_state(actuals, predictions)
99-
print('Hamming loss: ', hl.result().numpy()) # 0.25
100-
101-
# multi-label hamming loss
102-
hl = HammingLoss(mode='multilabel', threshold=0.8)
103-
actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1],
104-
[0, 0, 0,1]], dtype=tf.int32)
105-
predictions = tf.constant([[0.82, 0.5, 0.90, 0],
106-
[0, 1, 0.4, 0.98],
107-
[0.89, 0.79, 0, 0.3]],
108-
dtype=tf.float32)
109-
hl.update_state(actuals, predictions)
110-
print('Hamming loss: ', hl.result().numpy()) # 0.16666667
111-
```
87+
>>> # multi-class hamming loss
88+
>>> hl = HammingLoss(mode='multiclass', threshold=0.6)
89+
>>> actuals = tf.constant([[1, 0, 0, 0],[0, 0, 1, 0],
90+
... [0, 0, 0, 1],[0, 1, 0, 0]], dtype=tf.float32)
91+
>>> predictions = tf.constant([[0.8, 0.1, 0.1, 0],
92+
... [0.2, 0, 0.8, 0],[0.05, 0.05, 0.1, 0.8],[1, 0, 0, 0]],
93+
... dtype=tf.float32)
94+
>>> hl.update_state(actuals, predictions)
95+
<tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=4.0>
96+
>>> hl.result().numpy()
97+
0.25
98+
>>> # multi-label hamming loss
99+
>>> hl = HammingLoss(mode='multilabel', threshold=0.8)
100+
>>> actuals = tf.constant([[1, 0, 1, 0],[0, 1, 0, 1],
101+
... [0, 0, 0,1]], dtype=tf.int32)
102+
>>> predictions = tf.constant([[0.82, 0.5, 0.90, 0],
103+
... [0, 1, 0.4, 0.98],[0.89, 0.79, 0, 0.3]],dtype=tf.float32)
104+
>>> hl.update_state(actuals, predictions)
105+
<tf.Variable 'UnreadVariable' shape=() dtype=float32, numpy=3.0>
106+
>>> hl.result().numpy()
107+
0.16666667
108+
112109
"""
113110
if mode not in ["multiclass", "multilabel"]:
114111
raise TypeError("mode must be either multiclass or multilabel]")

tensorflow_addons/metrics/matthews_correlation_coefficient.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,18 @@ class MatthewsCorrelationCoefficient(tf.keras.metrics.Metric):
4242
((TP + FP) * (TP + FN) * (TN + FP ) * (TN + FN))^(1/2)
4343
4444
Usage:
45-
```python
46-
actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]],
47-
dtype=tf.float32)
48-
preds = tf.constant([[1.0], [0.0], [1.0], [1.0]],
49-
dtype=tf.float32)
50-
# Matthews correlation coefficient
51-
mcc = MatthewsCorrelationCoefficient(num_classes=1)
52-
mcc.update_state(actuals, preds)
53-
print('Matthews correlation coefficient is:',
54-
mcc.result().numpy())
55-
# Matthews correlation coefficient is : -0.33333334
56-
```
45+
46+
>>> actuals = tf.constant([[1.0], [1.0], [1.0], [0.0]],
47+
... dtype=tf.float32)
48+
>>> preds = tf.constant([[1.0], [0.0], [1.0], [1.0]],
49+
... dtype=tf.float32)
50+
>>> # Matthews correlation coefficient
51+
>>> metric = tfa.metrics.MatthewsCorrelationCoefficient(num_classes=1)
52+
>>> metric.update_state(y_true = actuals, y_pred = preds)
53+
>>> result = metric(y_true = actuals, y_pred = preds)
54+
>>> result.numpy()
55+
array([-0.33333334], dtype=float32)
56+
5757
"""
5858

5959
@typechecked

tensorflow_addons/metrics/multilabel_confusion_matrix.py

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,43 @@ class MultiLabelConfusionMatrix(Metric):
4545
- false positives for class i in M(0,1)
4646
- false negatives for class i in M(1,0)
4747
- true positives for class i in M(1,1)
48+
Usage:
49+
50+
>>> # multilabel confusion matrix
51+
>>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]],
52+
... dtype=tf.int32)
53+
>>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]],
54+
... dtype=tf.int32)
55+
>>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3)
56+
>>> metric.update_state(y_true, y_pred)
57+
>>> result = metric.result()
58+
>>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE
59+
array([[[1., 0.],
60+
[0., 1.]],
61+
<BLANKLINE>
62+
[[1., 0.],
63+
[0., 1.]],
64+
<BLANKLINE>
65+
[[0., 1.],
66+
[1., 0.]]], dtype=float32)
67+
>>> # if multiclass input is provided
68+
>>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]],
69+
... dtype=tf.int32)
70+
>>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]],
71+
... dtype=tf.int32)
72+
>>> metric = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3)
73+
>>> metric.update_state(y_true, y_pred)
74+
>>> result = metric.result()
75+
>>> result.numpy() #doctest: -DONT_ACCEPT_BLANKLINE
76+
array([[[1., 0.],
77+
[0., 1.]],
78+
<BLANKLINE>
79+
[[1., 0.],
80+
[1., 0.]],
81+
<BLANKLINE>
82+
[[1., 1.],
83+
[0., 0.]]], dtype=float32)
4884
49-
```python
50-
# multilabel confusion matrix
51-
y_true = tf.constant([[1, 0, 1], [0, 1, 0]],
52-
dtype=tf.int32)
53-
y_pred = tf.constant([[1, 0, 0],[0, 1, 1]],
54-
dtype=tf.int32)
55-
output = MultiLabelConfusionMatrix(num_classes=3)
56-
output.update_state(y_true, y_pred)
57-
print('Confusion matrix:', output.result().numpy())
58-
59-
# Confusion matrix: [[[1 0] [0 1]] [[1 0] [0 1]]
60-
[[0 1] [1 0]]]
61-
62-
# if multiclass input is provided
63-
y_true = tf.constant([[1, 0, 0], [0, 1, 0]],
64-
dtype=tf.int32)
65-
y_pred = tf.constant([[1, 0, 0],[0, 0, 1]],
66-
dtype=tf.int32)
67-
output = MultiLabelConfusionMatrix(num_classes=3)
68-
output.update_state(y_true, y_pred)
69-
print('Confusion matrix:', output.result().numpy())
70-
71-
# Confusion matrix: [[[1 0] [0 1]] [[1 0] [1 0]] [[1 1] [0 0]]]
72-
```
7385
"""
7486

7587
@typechecked

tensorflow_addons/metrics/r_square.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ class RSquare(Metric):
6060
of the same metric.
6161
6262
Usage:
63-
```python
64-
actuals = tf.constant([1, 4, 3], dtype=tf.float32)
65-
preds = tf.constant([2, 4, 4], dtype=tf.float32)
66-
result = tf.keras.metrics.RSquare()
67-
result.update_state(actuals, preds)
68-
print('R^2 score is: ', r1.result().numpy()) # 0.57142866
69-
```
63+
64+
>>> y_true = tf.constant([1, 4, 3], dtype=tf.float32)
65+
>>> y_pred = tf.constant([2, 4, 4], dtype=tf.float32)
66+
>>> metric = tfa.metrics.r_square.RSquare()
67+
>>> metric.update_state(y_true, y_pred)
68+
>>> result = metric.result()
69+
>>> result.numpy()
70+
0.57142854
71+
7072
"""
7173

7274
@typechecked

0 commit comments

Comments
 (0)