Skip to content

Commit 65e6329

Browse files
author
autoih
committed
fix docstr issue and add example
1 parent 4e7f4b5 commit 65e6329

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

tensorflow_addons/metrics/cohens_kappa.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,37 @@ class CohenKappa(Metric):
3434
A score of 0 means agreement by chance.
3535
Note: As of now, this implementation considers all labels
3636
while calculating the Cohen's Kappa score.
37+
3738
Usage:
38-
```python
39-
actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32)
40-
preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32)
41-
weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32)
42-
m = tfa.metrics.CohenKappa(num_classes=5)
43-
m.update_state(actuals, preds)
44-
print('Final result: ', m.result().numpy()) # Result: 0.61904764
45-
# To use this with weights, sample_weight argument can be used.
46-
m = tfa.metrics.CohenKappa(num_classes=5)
47-
m.update_state(actuals, preds, sample_weight=weights)
48-
print('Final result: ', m.result().numpy()) # Result: 0.37209308
49-
```
39+
40+
>>> actuals = np.array([4, 4, 3, 4, 2, 4, 1, 1], dtype=np.int32)
41+
>>> preds = np.array([4, 4, 3, 4, 4, 2, 1, 1], dtype=np.int32)
42+
>>> weights = np.array([1, 1, 2, 5, 10, 2, 3, 3], dtype=np.int32)
43+
>>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True)
44+
>>> m.update_state(actuals, preds)
45+
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
46+
array([[0., 0., 0., 0., 0.],
47+
[0., 2., 0., 0., 0.],
48+
[0., 0., 0., 0., 1.],
49+
[0., 0., 0., 1., 0.],
50+
[0., 0., 1., 0., 3.]], dtype=float32)>
51+
>>> m.result().numpy()
52+
0.61904764
53+
>>> m = tfa.metrics.CohenKappa(num_classes=5, sparse_labels=True)
54+
>>> m.update_state(actuals, preds, sample_weight=weights)
55+
<tf.Tensor: shape=(5, 5), dtype=float32, numpy=
56+
array([[ 0., 0., 0., 0., 0.],
57+
[ 0., 6., 0., 0., 0.],
58+
[ 0., 0., 0., 0., 10.],
59+
[ 0., 0., 0., 2., 0.],
60+
[ 0., 0., 2., 0., 7.]], dtype=float32)>
61+
>>> m.result().numpy()
62+
0.37209308
63+
5064
Usage with tf.keras API:
51-
```python
5265
model = tf.keras.models.Model(inputs, outputs)
5366
model.add_metric(tfa.metrics.CohenKappa(num_classes=5)(outputs))
5467
model.compile('sgd', loss='mse')
55-
```
5668
"""
5769

5870
@typechecked

tensorflow_addons/metrics/multilabel_confusion_matrix.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@ class MultiLabelConfusionMatrix(Metric):
4646
- false negatives for class i in M(1,0)
4747
- true positives for class i in M(1,1)
4848
49+
Usage:
50+
51+
>>> y_true = tf.constant([[1, 0, 1], [0, 1, 0]], dtype=tf.int32)
52+
>>> y_pred = tf.constant([[1, 0, 0],[0, 1, 1]], dtype=tf.int32)
53+
>>> output1 = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3)
54+
>>> output1.update_state(y_true, y_pred)
55+
>>> output1.result()
56+
<tf.Tensor: shape=(3, 2, 2), dtype=float32, numpy=
57+
array([[[1., 0.],
58+
[0., 1.]],
59+
<BLANKLINE>
60+
[[1., 0.],
61+
[0., 1.]],
62+
<BLANKLINE>
63+
[[0., 1.],
64+
[1., 0.]]], dtype=float32)>
65+
>>> y_true = tf.constant([[1, 0, 0], [0, 1, 0]], dtype=tf.int32)
66+
>>> y_pred = tf.constant([[1, 0, 0],[0, 0, 1]], dtype=tf.int32)
67+
>>> output2 = tfa.metrics.MultiLabelConfusionMatrix(num_classes=3)
68+
>>> output2.update_state(y_true, y_pred)
69+
>>> output2.result()
70+
<tf.Tensor: shape=(3, 2, 2), dtype=float32, numpy=
71+
array([[[1., 0.],
72+
[0., 1.]],
73+
<BLANKLINE>
74+
[[1., 0.],
75+
[1., 0.]],
76+
<BLANKLINE>
77+
[[1., 1.],
78+
[0., 0.]]], dtype=float32)>
4979
"""
5080

5181
@typechecked

0 commit comments

Comments
 (0)