Flux.jl/src/tree.jl

29 lines
664 B
Julia
Raw Normal View History

2017-09-27 20:11:21 +00:00
children(x) = ()
mapchildren(f, x) = x
function treelike(T, fs = fieldnames(T))
@eval begin
children(x::$T) = ($([:(x.$f) for f in fs]...),)
mapchildren(f, x::$T) = $T(f.(children(x))...)
end
end
2017-09-27 20:58:34 +00:00
# TODO: prewalk/postwalk with correct caching
# This is only correct in general for idempotent functions
2017-10-16 23:07:15 +00:00
isleaf(x) = isempty(children(x))
2017-09-27 20:11:21 +00:00
2017-10-16 23:07:15 +00:00
fmap(f, x) = isleaf(x) ? f(x) : mapchildren(x -> fmap(f, x), x)
ffor(f, x) = isleaf(x) ? f(x) : foreach(x -> ffor(f, x), children(x))
2017-09-27 20:58:34 +00:00
using DataFlow: OSet
2017-09-27 20:11:21 +00:00
function params(m)
2017-10-10 11:16:32 +00:00
ps, seen = [], OSet()
2017-10-16 23:07:15 +00:00
ffor(m) do p
p isa TrackedArray && p seen &&
(push!(ps, p); push!(seen, p))
end
2017-10-10 11:16:32 +00:00
return ps
2017-09-27 20:11:21 +00:00
end