Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Fix pearson correlation.py #3101

Merged
merged 9 commits into from
Aug 20, 2019

Conversation

JackKuo666
Copy link
Contributor

@JackKuo666 JackKuo666 commented Jul 27, 2019

Fixes #3102. Since the input tensor may be, for example(a batch of label), a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the denominator is assigned a value of 1.

Since the input tensor may be, for example, a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the denominator is assigned a value of 1.
Since the input tensor may be, for example, a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the denominator is assigned a value of 1.
Since the input tensor may be, for example, a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the pearson_r is assigned a value of 0.
Since the input tensor may be, for example, a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the pearson_r is assigned a value of 0.
Copy link
Contributor

@matt-gardner matt-gardner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the bug fix! Does the test you added fail without the changes that you made?

There are some minor pylint things to fix (you can see them here: http://build.allennlp.org/viewLog.html?buildId=18000&buildTypeId=AllenNLP_AllenNLPPullRequests&tab=buildLog). After that, this should be good to merge.

Copy link
Contributor Author

@JackKuo666 JackKuo666 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for fixing this bug is that in some input data, a batch's predictions or labels may be exactly the same. Then when the pearson_correlation.py is called, it will appear that the predictions_variance or labels_variance of the batch is 0 when calculating the variance, resulting in:

ZeroDivisionError: float division by zero

The two tests are separated because: in most cases, the data is constructed like predictions_1; but in a few cases, for example: predictions_2, the data of such a batch is exactly the same.

@JackKuo666
Copy link
Contributor Author

@matt-gardner please review

@DeNeutoy
Copy link
Contributor

@matt-gardner can you take another look at this?

Copy link
Contributor

@matt-gardner matt-gardner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, LGTM! Sorry to be super slow with this, I've been traveling for the last few weeks.

@matt-gardner matt-gardner merged commit 18daa29 into allenai:master Aug 20, 2019
reiyw pushed a commit to reiyw/allennlp that referenced this pull request Nov 12, 2019
* fix bug: ZeroDivisionError: float division by zero

Since the input tensor may be, for example, a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the denominator is assigned a value of 1.

* fix bug: ZeroDivisionError: float division by zero

Since the input tensor may be, for example, a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the denominator is assigned a value of 1.

* fix bug: ZeroDivisionError: float division by zero 

Since the input tensor may be, for example, a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the pearson_r is assigned a value of 0.

* fix bug: ZeroDivisionError: float division by zero 

Since the input tensor may be, for example, a tensor ([[0.,0.,0.,0.],
[0.,0.,0.,0. ]), there will be a case where (math.sqrt(predictions_variance) or math.sqrt(labels_variance)) is zero, so a judgment is added here to prevent the denominator from being zero. If it is zero, the pearson_r is assigned a value of 0.

* fix some  pylint things

fix some  pylint things

* Update pearson_correlation.py

* Update pearson_correlation_test.py

* Update pearson_correlation_test.py
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ZeroDivisionError: float division by zero [pearson_correlation.py]
3 participants