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

withgradient and gradient are incoherent for scalar input #165

Closed
gdalle opened this issue Mar 25, 2024 · 3 comments
Closed

withgradient and gradient are incoherent for scalar input #165

gdalle opened this issue Mar 25, 2024 · 3 comments

Comments

@gdalle
Copy link

gdalle commented Mar 25, 2024

julia> Tracker.gradient(abs2, 3)
(6.0 (tracked),)

julia> Tracker.withgradient(abs2, 3)
(val = 9, grad = (nothing,))
@mcabbott
Copy link
Member

mcabbott commented Mar 25, 2024

Yes, as documented but a potential surprise:

help?> Tracker.withgradient
  withgradient(f, xs...)

  This computes the value f(xs...) and the gradient with respect to xs. However, it differs from
  gradient in several other respects:

,,,

    •  Only objects satisfying Optimisers.isnumeric are regarded as parameters, thus in
       particular arrays of integers are ignored.
help?> Optimisers.isnumeric
  isnumeric(x) -> Bool

  Returns true on any parameter to be adjusted by Optimisers.jl, namely arrays of non-integer
  numbers. Returns false on all other types.

...

@gdalle
Copy link
Author

gdalle commented Mar 25, 2024

Okay thanks. The behavior is the same for scalar floats btw, but now I understand why.

julia> Tracker.gradient(abs2, 3.)
(6.0 (tracked),)

julia> Tracker.withgradient(abs2, 3.)
(val = 9.0, grad = (nothing,))

@gdalle gdalle closed this as completed Mar 25, 2024
@mcabbott
Copy link
Member

Yea I don't know that this is ideal, nor ideally documented. Possibly it's a reason for Flux to own this function.

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

No branches or pull requests

2 participants