From b3eb7692cf891ffb5b9b81c5158ead05ecfb11ec Mon Sep 17 00:00:00 2001 From: saishruthi Date: Wed, 12 Jun 2019 11:08:19 -0700 Subject: [PATCH 01/10] rsquare --- tensorflow_addons/metrics/r_square.py | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 tensorflow_addons/metrics/r_square.py diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py new file mode 100644 index 0000000000..fc4c0eecc4 --- /dev/null +++ b/tensorflow_addons/metrics/r_square.py @@ -0,0 +1,44 @@ +import tensorflow as tf +from tensorflow.keras.metrics import Metric + + +class RSquare(Metric): + """ + Compute R^2 score + + This is also called as coefficient of determination. + It tells how close are data to the fitted regression line. + + - Highest score can be 1.0 and it indicates that the predictors + perfectly accounts for variation in the target. + - Score 0.0 indicates that the predictors do not + account for variation in the target. + - It can also be negative if the model is worse. + """ + def __init__(self, name='r-square'): + super(RSquare, self).__init__(name=name, dtype=tf.float32) + self.squared_sum = self.add_weight("squared_sum", initializer="zeros") + self.sum = self.add_weight("sum", initializer="zeros") + self.res = self.add_weight("residual", initializer="zeros") + self.count = self.add_weight("count", initializer="zeros") + + def update_state(self, y_true, y_pred): + y_true = tf.convert_to_tensor(y_true, tf.float32) + y_pred = tf.convert_to_tensor(y_pred, tf.float32) + self.squared_sum.assign_add(tf.reduce_sum(y_true ** 2)), + self.sum.assign_add(tf.reduce_sum(y_true)), + self.res.assign_add(tf.reduce_sum( + tf.square(tf.subtract(y_true, y_pred)))), + self.count.assign_add(tf.cast(tf.shape(y_true)[0], tf.float32)) + + def result(self): + mean = self.sum / self.count + total = self.squared_sum - 2 * self.sum * mean + self.count * mean ** 2 + return 1 - (self.res / total) + + def reset_states(self): + # The state of the metric will be reset at the start of each epoch. + self.squared_sum.assign(0.0) + self.sum.assign(0.0) + self.res.assign(0.0) + self.count.assign(0.0) From 0459949f6f3202f7206dc180e3ae65c3f70af47b Mon Sep 17 00:00:00 2001 From: saishruthi Date: Thu, 13 Jun 2019 15:18:28 -0700 Subject: [PATCH 02/10] Addressing issues --- tensorflow_addons/metrics/r_square.py | 28 +++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index fc4c0eecc4..18b7d95aad 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -1,3 +1,23 @@ +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Implements F1 scores.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + import tensorflow as tf from tensorflow.keras.metrics import Metric @@ -15,7 +35,7 @@ class RSquare(Metric): account for variation in the target. - It can also be negative if the model is worse. """ - def __init__(self, name='r-square'): + def __init__(self, name='r_square'): super(RSquare, self).__init__(name=name, dtype=tf.float32) self.squared_sum = self.add_weight("squared_sum", initializer="zeros") self.sum = self.add_weight("sum", initializer="zeros") @@ -25,10 +45,10 @@ def __init__(self, name='r-square'): def update_state(self, y_true, y_pred): y_true = tf.convert_to_tensor(y_true, tf.float32) y_pred = tf.convert_to_tensor(y_pred, tf.float32) - self.squared_sum.assign_add(tf.reduce_sum(y_true ** 2)), - self.sum.assign_add(tf.reduce_sum(y_true)), + self.squared_sum.assign_add(tf.reduce_sum(y_true ** 2)) + self.sum.assign_add(tf.reduce_sum(y_true)) self.res.assign_add(tf.reduce_sum( - tf.square(tf.subtract(y_true, y_pred)))), + tf.square(tf.subtract(y_true, y_pred)))) self.count.assign_add(tf.cast(tf.shape(y_true)[0], tf.float32)) def result(self): From 73a6209360c155a5bd45fef348b2c22cdad0508f Mon Sep 17 00:00:00 2001 From: saishruthi Date: Thu, 13 Jun 2019 16:53:23 -0700 Subject: [PATCH 03/10] Addressing issues --- tensorflow_addons/metrics/__init__.py | 3 +- tensorflow_addons/metrics/r_square_test.py | 81 ++++++++++++++++++++++ 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tensorflow_addons/metrics/r_square_test.py diff --git a/tensorflow_addons/metrics/__init__.py b/tensorflow_addons/metrics/__init__.py index 4610eb870d..6bb4197fee 100644 --- a/tensorflow_addons/metrics/__init__.py +++ b/tensorflow_addons/metrics/__init__.py @@ -18,4 +18,5 @@ from __future__ import division from __future__ import print_function -from tensorflow_addons.metrics.cohens_kappa import CohenKappa \ No newline at end of file +from tensorflow_addons.metrics.cohens_kappa import CohenKappa +from tensorflow_addons.metrics.r_square import RSquare diff --git a/tensorflow_addons/metrics/r_square_test.py b/tensorflow_addons/metrics/r_square_test.py new file mode 100644 index 0000000000..275dbad386 --- /dev/null +++ b/tensorflow_addons/metrics/r_square_test.py @@ -0,0 +1,81 @@ +# Copyright 2019 The TensorFlow Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ============================================================================== +"""Tests for R-Square Metric.""" + +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import tensorflow as tf +from tensorflow_addons.metrics import RSquare + + +class RSquareTest(tf.test.TestCase): + def test_config(self): + r2_obj = RSquare(name='r_square') + self.assertEqual(r2_obj.name, 'r_square') + self.assertEqual(r2_obj.dtype, tf.float32) + # Check save and restore config + r2_obj2 = RSquare.from_config(r2_obj.get_config()) + self.assertEqual(r2_obj2.name, 'r_square') + self.assertEqual(r2_obj2.dtype, tf.float32) + + def initialize_vars(self): + r2_obj = RSquare() + self.evaluate(tf.compat.v1.variables_initializer(r2_obj.variables)) + return r2_obj + + def update_obj_states(self, obj, actuals, preds): + update_op = obj.update_state(actuals, preds) + self.evaluate(update_op) + + def check_results(self, obj, value): + self.assertAllClose(value, self.evaluate(obj.result()), atol=1e-5) + + def test_r2_perfect_score(self): + actuals = [100, 700, 40, 5.7] + preds = [100, 700, 40, 5.7] + actuals = tf.constant(actuals, dtype=tf.float32) + preds = tf.constant(preds, dtype=tf.float32) + # Initialize + r2_obj = self.initialize_vars() + # Update + self.update_obj_states(r2_obj, actuals, preds) + # Check results + self.check_results(r2_obj, 1.0) + + def test_r2_worst_score(self): + actuals = [10, 600, 4, 9.77] + preds = [1, 70, 40, 5.7] + actuals = tf.constant(actuals, dtype=tf.float32) + preds = tf.constant(preds, dtype=tf.float32) + # Initialize + r2_obj = self.initialize_vars() + # Update + self.update_obj_states(r2_obj, actuals, preds) + # Check results + self.check_results(r2_obj, -0.073607) + + def test_r2_random_score(self): + actuals = [10, 600, 3, 9.77] + preds = [1, 340, 40, 5.7] + actuals = tf.constant(actuals, dtype=tf.float32) + preds = tf.constant(preds, dtype=tf.float32) + # Initialize + r2_obj = self.initialize_vars() + # Update + self.update_obj_states(r2_obj, actuals, preds) + # Check results + self.check_results(r2_obj, 0.7376327) From ff816a69e6ff3ffdd7bd94d1a39f006559910cde Mon Sep 17 00:00:00 2001 From: saishruthi Date: Fri, 14 Jun 2019 12:38:49 -0700 Subject: [PATCH 04/10] Updates to rsquare --- tensorflow_addons/metrics/BUILD | 25 ++++++++++++++++++++++ tensorflow_addons/metrics/README.md | 2 ++ tensorflow_addons/metrics/r_square.py | 4 ++-- tensorflow_addons/metrics/r_square_test.py | 16 ++++++++------ 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/tensorflow_addons/metrics/BUILD b/tensorflow_addons/metrics/BUILD index b1208e465e..e1f095461c 100644 --- a/tensorflow_addons/metrics/BUILD +++ b/tensorflow_addons/metrics/BUILD @@ -26,3 +26,28 @@ py_test( ":metrics", ], ) + +py_library( + name = "metrics", + srcs = [ + "__init__.py", + "r_square.py", + ], + srcs_version = "PY2AND3", + deps = [ + "//tensorflow_addons/utils", + ], +) + +py_test( + name = "r_square_test", + size = "small", + srcs = [ + "r_square_test.py", + ], + main = "r_square_test.py", + srcs_version = "PY2AND3", + deps = [ + ":metrics", + ], +) diff --git a/tensorflow_addons/metrics/README.md b/tensorflow_addons/metrics/README.md index 4a978fbe08..4558983dee 100644 --- a/tensorflow_addons/metrics/README.md +++ b/tensorflow_addons/metrics/README.md @@ -4,11 +4,13 @@ | Submodule | Maintainers | Contact Info | |:---------- |:------------- |:--------------| | cohens_kappa| Aakash Nain | aakashnain@outlook.com| +| r_square| Saishruthi Swaminathan| saishruthi.tn@gmail.com| ## Contents | Submodule | Metric | Reference | |:----------------------- |:-------------------|:---------------| | cohens_kappa| CohenKappa|[Cohen's Kappa](https://en.wikipedia.org/wiki/Cohen%27s_kappa)| +| r_square| RSquare|[R-Sqaure](https://en.wikipedia.org/wiki/Coefficient_of_determination)| ## Contribution Guidelines diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 18b7d95aad..999eaa7116 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -35,8 +35,8 @@ class RSquare(Metric): account for variation in the target. - It can also be negative if the model is worse. """ - def __init__(self, name='r_square'): - super(RSquare, self).__init__(name=name, dtype=tf.float32) + def __init__(self, name='r_square', dtype=tf.float32): + super(RSquare, self).__init__(name=name, dtype=dtype) self.squared_sum = self.add_weight("squared_sum", initializer="zeros") self.sum = self.add_weight("sum", initializer="zeros") self.res = self.add_weight("residual", initializer="zeros") diff --git a/tensorflow_addons/metrics/r_square_test.py b/tensorflow_addons/metrics/r_square_test.py index 275dbad386..6a9b2a4f20 100644 --- a/tensorflow_addons/metrics/r_square_test.py +++ b/tensorflow_addons/metrics/r_square_test.py @@ -45,8 +45,8 @@ def check_results(self, obj, value): self.assertAllClose(value, self.evaluate(obj.result()), atol=1e-5) def test_r2_perfect_score(self): - actuals = [100, 700, 40, 5.7] - preds = [100, 700, 40, 5.7] + actuals = tf.constant([100, 700, 40, 5.7], dtype=tf.float32) + preds = tf.constant([100, 700, 40, 5.7], dtype=tf.float32) actuals = tf.constant(actuals, dtype=tf.float32) preds = tf.constant(preds, dtype=tf.float32) # Initialize @@ -57,8 +57,8 @@ def test_r2_perfect_score(self): self.check_results(r2_obj, 1.0) def test_r2_worst_score(self): - actuals = [10, 600, 4, 9.77] - preds = [1, 70, 40, 5.7] + actuals = tf.constant([10, 600, 4, 9.77], dtype=tf.float32) + preds = tf.constant([1, 70, 40, 5.7], dtype=tf.float32) actuals = tf.constant(actuals, dtype=tf.float32) preds = tf.constant(preds, dtype=tf.float32) # Initialize @@ -69,8 +69,8 @@ def test_r2_worst_score(self): self.check_results(r2_obj, -0.073607) def test_r2_random_score(self): - actuals = [10, 600, 3, 9.77] - preds = [1, 340, 40, 5.7] + actuals = tf.constant([10, 600, 3, 9.77], dtype=tf.float32) + preds = tf.constant([1, 340, 40, 5.7], dtype=tf.float32) actuals = tf.constant(actuals, dtype=tf.float32) preds = tf.constant(preds, dtype=tf.float32) # Initialize @@ -79,3 +79,7 @@ def test_r2_random_score(self): self.update_obj_states(r2_obj, actuals, preds) # Check results self.check_results(r2_obj, 0.7376327) + + +if __name__ == '__main__': + tf.test.main() From f79e2b2c9390d573f606b78f89e6f6fcc1e59ea1 Mon Sep 17 00:00:00 2001 From: saishruthi Date: Sun, 16 Jun 2019 21:33:41 -0700 Subject: [PATCH 05/10] new updates --- tensorflow_addons/metrics/BUILD | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/tensorflow_addons/metrics/BUILD b/tensorflow_addons/metrics/BUILD index e1f095461c..209c029403 100644 --- a/tensorflow_addons/metrics/BUILD +++ b/tensorflow_addons/metrics/BUILD @@ -7,6 +7,7 @@ py_library( srcs = [ "__init__.py", "cohens_kappa.py", + "r_square.py" ], srcs_version = "PY2AND3", deps = [ @@ -27,18 +28,6 @@ py_test( ], ) -py_library( - name = "metrics", - srcs = [ - "__init__.py", - "r_square.py", - ], - srcs_version = "PY2AND3", - deps = [ - "//tensorflow_addons/utils", - ], -) - py_test( name = "r_square_test", size = "small", From 6170d40ff9edaf9ba2055beb34f72e02b35f4889 Mon Sep 17 00:00:00 2001 From: saishruthi Date: Sun, 16 Jun 2019 21:40:30 -0700 Subject: [PATCH 06/10] new updates --- .DS_Store | Bin 0 -> 6148 bytes tensorflow_addons/.DS_Store | Bin 0 -> 6148 bytes tensorflow_addons/metrics/r_square.py | 12 ++++++------ 3 files changed, 6 insertions(+), 6 deletions(-) create mode 100644 .DS_Store create mode 100644 tensorflow_addons/.DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..9a5a64f9bdabbf90b12188346737743c46d7c560 GIT binary patch literal 6148 zcmeHKQE$^Q5I(mB+#*CLA;zO$_ST1%ioza{qON<`+oT|P0MsT*p^-v7H7U^uo$@c( zKiW^&-`Sn*7FA^uFQ9e0i_YKqd}qg>BRd8FF$md{a(K@1Q6k)sUVc6NU z8DIwf6$7+CC{#kvVP(;59azxsBegdONzkUd1fg>1Ijk&V3yQF*h&ENYD~7P?=vOYz zb68ol=^)I=*pC}oxEqQvqoZG$a1fqFE|~#lV3UEmt+wg>Kfe9`znR1}W`G&ER}6^8 z$R7=HO73jknjD?A3Uz`?LUEPFbqX5hD8^ViieI1_LBApc(Q{Z?L=Ou82pAf;US5T32oZYyFBq8|6+t%qvy?;wO)58i}`9#k|j!3Js~O={6v$y?|f`3Sy_ zGrL<+s|PP4Wd>%y+42VYXNxzx&;vm)IxF#TF zS(eYMi-W;Nqoy_*!p)t&u|>%05G!|?IRFe~sU)G}{z1kY%E zS>&zTjXOzvg^^L!D4UTPU Date: Sun, 16 Jun 2019 21:40:43 -0700 Subject: [PATCH 07/10] new updates --- .DS_Store | Bin 6148 -> 0 bytes tensorflow_addons/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store delete mode 100644 tensorflow_addons/.DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 9a5a64f9bdabbf90b12188346737743c46d7c560..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKQE$^Q5I(mB+#*CLA;zO$_ST1%ioza{qON<`+oT|P0MsT*p^-v7H7U^uo$@c( zKiW^&-`Sn*7FA^uFQ9e0i_YKqd}qg>BRd8FF$md{a(K@1Q6k)sUVc6NU z8DIwf6$7+CC{#kvVP(;59azxsBegdONzkUd1fg>1Ijk&V3yQF*h&ENYD~7P?=vOYz zb68ol=^)I=*pC}oxEqQvqoZG$a1fqFE|~#lV3UEmt+wg>Kfe9`znR1}W`G&ER}6^8 z$R7=HO73jknjD?A3Uz`?LUEPFbqX5hD8^ViieI1_LBApc(Q{Z?L=Ou82pAf;US5T32oZYyFBq8|6+t%qvy?;wO)58i}`9#k|j!3Js~O={6v$y?|f`3Sy_ zGrL<+s|PP4Wd>%y+42VYXNxzx&;vm)IxF#TF zS(eYMi-W;Nqoy_*!p)t&u|>%05G!|?IRFe~sU)G}{z1kY%E zS>&zTjXOzvg^^L!D4UTPU Date: Sun, 16 Jun 2019 21:50:44 -0700 Subject: [PATCH 08/10] doc correction --- tensorflow_addons/metrics/r_square.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index 860b613c05..a858dc5593 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. # ============================================================================== -"""Implements F1 scores.""" +"""Implements R^2 scores.""" from __future__ import absolute_import from __future__ import division From 168242e9cd04d7a9f2f7fd909f31c49b61267497 Mon Sep 17 00:00:00 2001 From: saishruthi Date: Sun, 16 Jun 2019 21:59:01 -0700 Subject: [PATCH 09/10] doc correction --- tensorflow_addons/metrics/r_square.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/tensorflow_addons/metrics/r_square.py b/tensorflow_addons/metrics/r_square.py index a858dc5593..da32654fd0 100644 --- a/tensorflow_addons/metrics/r_square.py +++ b/tensorflow_addons/metrics/r_square.py @@ -25,14 +25,23 @@ class RSquare(Metric): """Compute R^2 score. - This is also called as coefficient of determination. - It tells how close are data to the fitted regression line. + This is also called as coefficient of determination. + It tells how close are data to the fitted regression line. - - Highest score can be 1.0 and it indicates that the predictors - perfectly accounts for variation in the target. - - Score 0.0 indicates that the predictors do not - account for variation in the target. - - It can also be negative if the model is worse. + - Highest score can be 1.0 and it indicates that the predictors + perfectly accounts for variation in the target. + - Score 0.0 indicates that the predictors do not + account for variation in the target. + - It can also be negative if the model is worse. + + 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 + ``` """ def __init__(self, name='r_square', dtype=tf.float32): From 3a26d9178abe85b0955d4aa4a97e7d5d9ff85331 Mon Sep 17 00:00:00 2001 From: saishruthi Date: Sun, 16 Jun 2019 22:55:33 -0700 Subject: [PATCH 10/10] build correction --- tensorflow_addons/metrics/BUILD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tensorflow_addons/metrics/BUILD b/tensorflow_addons/metrics/BUILD index 209c029403..541ec88bb9 100644 --- a/tensorflow_addons/metrics/BUILD +++ b/tensorflow_addons/metrics/BUILD @@ -7,7 +7,7 @@ py_library( srcs = [ "__init__.py", "cohens_kappa.py", - "r_square.py" + "r_square.py", ], srcs_version = "PY2AND3", deps = [