-
Notifications
You must be signed in to change notification settings - Fork 617
rsquare metric #283
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
rsquare metric #283
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b3eb769
rsquare
SSaishruthi 0459949
Addressing issues
SSaishruthi 73a6209
Addressing issues
SSaishruthi ff816a6
Updates to rsquare
SSaishruthi f79e2b2
new updates
SSaishruthi 6170d40
new updates
SSaishruthi a8d16d3
new updates
SSaishruthi 6877826
doc correction
SSaishruthi 168242e
doc correction
SSaishruthi 3a26d91
build correction
SSaishruthi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,13 @@ | |
| | Submodule | Maintainers | Contact Info | | ||
| |:---------- |:------------- |:--------------| | ||
| | cohens_kappa| Aakash Nain | [email protected]| | ||
| | r_square| Saishruthi Swaminathan| [email protected]| | ||
|
|
||
| ## 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 | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| # 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 R^2 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 | ||
|
|
||
|
|
||
| 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. | ||
|
|
||
| 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): | ||
| 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") | ||
| 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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| # 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 = 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 | ||
| 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 = 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 | ||
| 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 = 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 | ||
| r2_obj = self.initialize_vars() | ||
| # Update | ||
| self.update_obj_states(r2_obj, actuals, preds) | ||
| # Check results | ||
| self.check_results(r2_obj, 0.7376327) | ||
Squadrick marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| tf.test.main() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.