|
| 1 | +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# ============================================================================== |
| 15 | +"""Tests for Cohen's Kappa Metric.""" |
| 16 | + |
| 17 | +from __future__ import absolute_import |
| 18 | +from __future__ import division |
| 19 | +from __future__ import print_function |
| 20 | + |
| 21 | +import tensorflow as tf |
| 22 | +from tensorflow_addons.metrics import CohenKappa |
| 23 | +from tensorflow_addons.utils import test_utils |
| 24 | + |
| 25 | + |
| 26 | +@test_utils.run_all_in_graph_and_eager_modes |
| 27 | +class CohenKappaTest(tf.test.TestCase): |
| 28 | + def test_config(self): |
| 29 | + kp_obj = CohenKappa(name='cohen_kappa', num_classes=5) |
| 30 | + self.assertEqual(kp_obj.name, 'cohen_kappa') |
| 31 | + self.assertEqual(kp_obj.dtype, tf.float32) |
| 32 | + self.assertEqual(kp_obj.num_classes, 5) |
| 33 | + |
| 34 | + # Check save and restore config |
| 35 | + kb_obj2 = CohenKappa.from_config(kp_obj.get_config()) |
| 36 | + self.assertEqual(kb_obj2.name, 'cohen_kappa') |
| 37 | + self.assertEqual(kb_obj2.dtype, tf.float32) |
| 38 | + self.assertEqual(kp_obj.num_classes, 5) |
| 39 | + |
| 40 | + def initialize_vars(self): |
| 41 | + kp_obj1 = CohenKappa(num_classes=5) |
| 42 | + kp_obj2 = CohenKappa(num_classes=5, weightage='linear') |
| 43 | + kp_obj3 = CohenKappa(num_classes=5, weightage='quadratic') |
| 44 | + |
| 45 | + self.evaluate(tf.compat.v1.variables_initializer(kp_obj1.variables)) |
| 46 | + self.evaluate(tf.compat.v1.variables_initializer(kp_obj2.variables)) |
| 47 | + self.evaluate(tf.compat.v1.variables_initializer(kp_obj3.variables)) |
| 48 | + return kp_obj1, kp_obj2, kp_obj3 |
| 49 | + |
| 50 | + def update_obj_states(self, obj1, obj2, obj3, actuals, preds, weights): |
| 51 | + update_op1 = obj1.update_state(actuals, preds, sample_weight=weights) |
| 52 | + update_op2 = obj2.update_state(actuals, preds, sample_weight=weights) |
| 53 | + update_op3 = obj3.update_state(actuals, preds, sample_weight=weights) |
| 54 | + |
| 55 | + self.evaluate(update_op1) |
| 56 | + self.evaluate(update_op2) |
| 57 | + self.evaluate(update_op3) |
| 58 | + |
| 59 | + def check_results(self, objs, values): |
| 60 | + obj1, obj2, obj3 = objs |
| 61 | + val1, val2, val3 = values |
| 62 | + |
| 63 | + self.assertAllClose(val1, self.evaluate(obj1.result()), atol=1e-5) |
| 64 | + self.assertAllClose(val2, self.evaluate(obj2.result()), atol=1e-5) |
| 65 | + self.assertAllClose(val3, self.evaluate(obj3.result()), atol=1e-5) |
| 66 | + |
| 67 | + def test_kappa_random_score(self): |
| 68 | + actuals = [4, 4, 3, 4, 2, 4, 1, 1] |
| 69 | + preds = [4, 4, 3, 4, 4, 2, 1, 1] |
| 70 | + actuals = tf.constant(actuals, dtype=tf.int32) |
| 71 | + preds = tf.constant(preds, dtype=tf.int32) |
| 72 | + |
| 73 | + # Initialize |
| 74 | + kp_obj1, kp_obj2, kp_obj3 = self.initialize_vars() |
| 75 | + |
| 76 | + # Update |
| 77 | + self.update_obj_states(kp_obj1, kp_obj2, kp_obj3, actuals, preds, None) |
| 78 | + |
| 79 | + # Check results |
| 80 | + self.check_results([kp_obj1, kp_obj2, kp_obj3], |
| 81 | + [0.61904761, 0.62790697, 0.68932038]) |
| 82 | + |
| 83 | + def test_kappa_perfect_score(self): |
| 84 | + actuals = [4, 4, 3, 3, 2, 2, 1, 1] |
| 85 | + preds = [4, 4, 3, 3, 2, 2, 1, 1] |
| 86 | + actuals = tf.constant(actuals, dtype=tf.int32) |
| 87 | + preds = tf.constant(preds, dtype=tf.int32) |
| 88 | + |
| 89 | + # Initialize |
| 90 | + kp_obj1, kp_obj2, kp_obj3 = self.initialize_vars() |
| 91 | + |
| 92 | + # Update |
| 93 | + self.update_obj_states(kp_obj1, kp_obj2, kp_obj3, actuals, preds, None) |
| 94 | + |
| 95 | + # Check results |
| 96 | + self.check_results([kp_obj1, kp_obj2, kp_obj3], [1.0, 1.0, 1.0]) |
| 97 | + |
| 98 | + def test_kappa_worse_than_random(self): |
| 99 | + actuals = [4, 4, 3, 3, 2, 2, 1, 1] |
| 100 | + preds = [1, 2, 4, 1, 3, 3, 4, 4] |
| 101 | + actuals = tf.constant(actuals, dtype=tf.int32) |
| 102 | + preds = tf.constant(preds, dtype=tf.int32) |
| 103 | + |
| 104 | + # Initialize |
| 105 | + kp_obj1, kp_obj2, kp_obj3 = self.initialize_vars() |
| 106 | + |
| 107 | + # Update |
| 108 | + self.update_obj_states(kp_obj1, kp_obj2, kp_obj3, actuals, preds, None) |
| 109 | + |
| 110 | + # check results |
| 111 | + self.check_results([kp_obj1, kp_obj2, kp_obj3], |
| 112 | + [-0.3333333, -0.52380952, -0.72727272]) |
| 113 | + |
| 114 | + def test_kappa_with_sample_weights(self): |
| 115 | + actuals = [4, 4, 3, 3, 2, 2, 1, 1] |
| 116 | + preds = [1, 2, 4, 1, 3, 3, 4, 4] |
| 117 | + weights = [1, 1, 2, 5, 10, 2, 3, 3] |
| 118 | + actuals = tf.constant(actuals, dtype=tf.int32) |
| 119 | + preds = tf.constant(preds, dtype=tf.int32) |
| 120 | + weights = tf.constant(weights, dtype=tf.int32) |
| 121 | + |
| 122 | + # Initialize |
| 123 | + kp_obj1, kp_obj2, kp_obj3 = self.initialize_vars() |
| 124 | + |
| 125 | + # Update |
| 126 | + self.update_obj_states(kp_obj1, kp_obj2, kp_obj3, actuals, preds, |
| 127 | + weights) |
| 128 | + |
| 129 | + # check results |
| 130 | + self.check_results([kp_obj1, kp_obj2, kp_obj3], |
| 131 | + [-0.25473321, -0.38992332, -0.60695344]) |
| 132 | + |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + tf.test.main() |
0 commit comments