FluxML / FluxML/Functors.jl

`fmapreduce`

Open
#35 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Julia
Stars
123
Forks
18
Avg merge
47m
Merged PRs (30d)
1

Description

This package probably wants a way to write `mapreduce`, to replace e.g. `sum(norm(p) for p in params(m))` in Flux. This seems like the minimal attempt, but it's not Zygote-friendly. Can this be fixed, and is there a better way?
```julia
julia> using Functors, Zygote

julia> const INIT = Base._InitialValue();

julia> function fmapreduce(f, op, x; init = INIT, walk = (f, x) -> foreach(f, Functors.children(x)), kw...)
fmap(x; walk, kw...) do y
init = init===INIT ? f(y) : op(init, f(y))
end
init===INIT ? Base.mapreduce_empty(f, op) : init
end
fmapreduce (generic function with 1 method)

julia> m = ([1,2], (x=[3,4], y=5), 6);

julia> fmapreduce(sum, +, m)
21

julia> gradient(fmapreduce, sum, +, m)
(nothing, nothing, nothing)
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.