-
-
Notifications
You must be signed in to change notification settings - Fork 16
Allow gradients in fmap #1
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1ac81f0
handle dmap1(f, x, dx)
f8cb762
rename util fn
ae01fe9
rebase
9638615
make dx a vararg
3acf19e
Merge branch 'master' into dg/grad
9b04a35
add comments
DhairyaLGandhi bf36147
Merge branch 'dg/grad' of https://github.com/FluxML/Functors.jl into …
10ec1ae
whitespace
d1d29da
Merge remote-tracking branch 'origin/master' into dg/grad
DhairyaLGandhi 13348e8
invariant functor call
DhairyaLGandhi 3e00276
rebase
DhairyaLGandhi ac8a900
handle nothing-nothing
DhairyaLGandhi 3354cfb
add Zygote to test
DhairyaLGandhi 101f643
add test for generic f-map
DhairyaLGandhi b5b872b
Merge branch 'master' into dg/grad
DhairyaLGandhi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,10 +75,29 @@ Equivalent to `functor(x)[1]`. | |
""" | ||
children(x) = functor(x)[1] | ||
|
||
function functor_tuple(f, x::Tuple, dx::Tuple) | ||
map(x, dx) do x, x̄ | ||
_default_walk(f, x, x̄) | ||
end | ||
end | ||
functor_tuple(f, x, dx) = f(x, dx) | ||
functor_tuple(f, x, ::Nothing) = x | ||
|
||
# @functor Chain | ||
# Chain -> func = (layers = (Dense,Dense),), gs -> (layers...) | ||
function _default_walk(f, x, dx) | ||
func, re = functor(x) | ||
map(func, dx) do x, x̄ | ||
# functor_tuple(f, x, x̄) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should work with the other change? |
||
f(x, x̄) | ||
end |> re | ||
end | ||
|
||
function _default_walk(f, x) | ||
func, re = functor(x) | ||
re(map(f, func)) | ||
end | ||
_default_walk(f, ::Nothing, ::Nothing) = nothing | ||
|
||
""" | ||
fmap(f, x; exclude = isleaf, walk = Functors._default_walk) | ||
|
@@ -205,3 +224,11 @@ function fcollect(x; output = [], cache = Base.IdSet(), exclude = v -> false) | |
end | ||
return output | ||
end | ||
|
||
DhairyaLGandhi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Allow gradients and other constructs that match the structure of the functor | ||
# to allow for `map` style computations and return a modified version of the struct. | ||
# This way we can use `fmap` to update the params with their gradients | ||
function fmap(f, x, dx...; cache = IdDict()) | ||
haskey(cache, x) && return cache[x] | ||
cache[x] = isleaf(x) ? f(x, dx...) : _default_walk((x...) -> fmap(f, x..., cache = cache), x, dx...) | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
using Functors, Test | ||
using Zygote | ||
|
||
@testset "Functors.jl" begin | ||
|
||
include("basics.jl") | ||
include("base.jl") | ||
|
||
include("basics.jl") | ||
include("update.jl") | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
@testset "Generalized fmap over equivalent functors" begin | ||
struct M{F,T,S} | ||
σ::F | ||
W::T | ||
b::S | ||
end | ||
|
||
@functor M | ||
|
||
(m::M)(x) = m.σ.(m.W * x .+ m.b) | ||
|
||
m = M(identity, ones(Float32, 3, 4), zeros(Float32, 3)) | ||
x = ones(Float32, 4, 2) | ||
m̄, _ = gradient((m,x) -> sum(m(x)), m, x) | ||
m̂ = Functors.fmap(m, m̄) do x, y | ||
isnothing(x) && return y | ||
isnothing(y) && return x | ||
x .- 0.1f0 .* y | ||
end | ||
|
||
@test m̂.W ≈ fill(0.8f0, size(m.W)) | ||
@test m̂.b ≈ fill(-0.2f0, size(m.b)) | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't think
x
is a tuple