Skip to content
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

small fix in the backward rule of norm #131

Merged
merged 3 commits into from
Jun 14, 2024
Merged

Conversation

tangwei94
Copy link
Contributor

Previously, the backward of norm will become NAN if the norm is zero.

Copy link

codecov bot commented Jun 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.94%. Comparing base (b5096da) to head (6e9bec0).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #131   +/-   ##
=======================================
  Coverage   81.94%   81.94%           
=======================================
  Files          42       42           
  Lines        5666     5667    +1     
=======================================
+ Hits         4643     4644    +1     
  Misses       1023     1023           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -172,7 +172,9 @@ end
function ChainRulesCore.rrule(::typeof(norm), a::AbstractTensorMap, p::Real=2)
p == 2 || error("currently only implemented for p = 2")
n = norm(a, p)
norm_pullback(Δn) = NoTangent(), a * (Δn' + Δn) / (n * 2), NoTangent()
function norm_pullback(Δn)
return NoTangent(), a * (Δn' + Δn) / (n * 2 + eps(real(eltype(a)))), NoTangent()
Copy link
Owner

Choose a reason for hiding this comment

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

Could you change this to a * (Δn' + Δn) / 2 / hypot(n, eps(one(n))) ? I think that is slightly nicer, in that, if n==1., then n+eps() is no longer exactly one, but hypot(1.,eps()) is still exactly 1. due to machine precision.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is indeed better. I have modified this line according to the suggestion.

@Jutho
Copy link
Owner

Jutho commented Jun 13, 2024

Thanks; that's an important fix. I made one suggestion in the code.

ext/TensorKitChainRulesCoreExt.jl Outdated Show resolved Hide resolved
@@ -172,7 +172,9 @@ end
function ChainRulesCore.rrule(::typeof(norm), a::AbstractTensorMap, p::Real=2)
p == 2 || error("currently only implemented for p = 2")
n = norm(a, p)
norm_pullback(Δn) = NoTangent(), a * (Δn' + Δn) / (n * 2), NoTangent()
function norm_pullback(Δn)
return NoTangent(), a * (Δn' + Δn) / (n * 2 + eps(real(eltype(a)))), NoTangent()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is indeed better. I have modified this line according to the suggestion.

@Jutho Jutho merged commit b026cf2 into Jutho:master Jun 14, 2024
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants