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

Allow gradients in fmap #1

Merged
merged 15 commits into from
Nov 21, 2021
Merged

Allow gradients in fmap #1

merged 15 commits into from
Nov 21, 2021

Conversation

DhairyaLGandhi
Copy link
Member

No description provided.

@DhairyaLGandhi DhairyaLGandhi changed the title Allow gradients in map Allow gradients in fmap Apr 24, 2020
@willtebbutt
Copy link
Member

Any chance of some documentation alongside this PR explaining what the purpose of the two-argument version of fmap / fmap1 is about.

Additionally, why do we need a separate functor_tuple function, as opposed to just implementing a method of functor for Tuple?

Dhairya Gandhi added 2 commits June 8, 2020 14:08
@DhairyaLGandhi
Copy link
Member Author

Its to make Optimisers.jl work by returning a slightly trained version of the model. The tuple function is so that we return a tuple of layers in a chain. I should revisit to see if its necessary still but otherwise this should be merged so we can release Optimisers.jl

@willtebbutt
Copy link
Member

I agree that this should probably go in, but a comment of some kind explaining what you've said above would be great.

@DhairyaLGandhi
Copy link
Member Author

bump!

Copy link
Member

@willtebbutt willtebbutt left a comment

Choose a reason for hiding this comment

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

The changes look get. Perhaps some tests and a patch bump?

@ToucheSir
Copy link
Member

Woops, meant to say LGTM once the tests land.

@DhairyaLGandhi
Copy link
Member Author

Allows writing something like

Functors.fmap(x, y) do x, y
  isnothing(x) && return y
  isnothing(y) && return x
  f(x,y)
end

where x and y are basically NamedTuples out of Zygote, or a mix of the actual object tree (think Chain, model struct etc) and the corresponding gradients.

Copy link
Member

@darsnack darsnack left a comment

Choose a reason for hiding this comment

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

Looks good!

@@ -44,10 +44,29 @@ Equivalent to `functor(x)[1]`.
"""
children(x) = functor(x)[1]

function functor_tuple(f, x::Tuple, dx::Tuple)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think x is a tuple

Suggested change
function functor_tuple(f, x::Tuple, dx::Tuple)
function functor_tuple(f, x, dx::Tuple)

function _default_walk(f, x, dx)
func, re = functor(x)
map(func, dx) do x, x̄
# functor_tuple(f, x, x̄)
Copy link
Member

Choose a reason for hiding this comment

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

This should work with the other change?

@ToucheSir
Copy link
Member

@DhairyaLGandhi this already has my approval, so feel free to merge any time :)

@ToucheSir ToucheSir merged commit 40c547c into master Nov 21, 2021
@DhairyaLGandhi DhairyaLGandhi deleted the dg/grad branch November 22, 2021 10:06
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.

4 participants