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
Clean all those safe_div, _safe_div methods #21798
Clean all those safe_div, _safe_div methods #21798
Conversation
ff007af
to
c05bb4e
Compare
@@ -104,7 +78,8 @@ def _safe_mean(losses, num_present): | |||
then zero is returned. | |||
""" | |||
total_loss = math_ops.reduce_sum(losses) | |||
return _safe_div(total_loss, num_present) | |||
return math_ops.div_no_nan(total_loss, num_present, | |||
negative_to_zero=True, name="value") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd much prefer to replace div_no_nan(x, y, negative_to_zero=True, ...)
with div_no_nan(x, tf.max(y, 0), ...)
and not add the negative_to_zero
argument, which I doesn't look to me like it makes things simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reasonable. I'd like to revise code as suggested later.
01caf86
to
dd634a2
Compare
@martinwicke negative_to_zero was removed. |
@@ -104,7 +78,9 @@ def _safe_mean(losses, num_present): | |||
then zero is returned. | |||
""" | |||
total_loss = math_ops.reduce_sum(losses) | |||
return _safe_div(total_loss, num_present) | |||
return math_ops.div_no_nan(total_loss, | |||
math_ops.maximum(num_present, 0), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think maximum
is unnecessary op here, because num_present should be non-negative in practice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does something break when you remove the maximum? This could be another case of us needing the double-tap-where trick to avoid NaNs in the gradient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, I don't remove the maximum. Test failed after I resolved the conflict with the lastest master. I'll tell you when I fix it later. :-)
dd634a2
to
38f8110
Compare
@akanimax @martinwicke could you comment or reassign? Thanks. |
…eate_negative_as_zero
Hey, what am I doing wrong? It seems that both reviewers are selected by github automatically. |
663cd26
to
e9cfe88
Compare
e9cfe88
to
2dd5fb6
Compare
@alextp Hi, I think all tests pass, and the failures are unrelated. |
Hi, any update here? thanks |
…_negative_as_zero PiperOrigin-RevId: 214290400
tf.div_no_nan was introduced by #21621, which is designed to replace all those safe_div methods here.
Ref: #21784
@martinwicke please add a API design label, because we add a new argument: negative_to_zero.