Compact layer factory #2
Replies: 2 comments 11 replies
-
Thanks! I think the
What do you think? @mcabbott? You could also have it so that |
Beta Was this translation helpful? Give feedback.
-
BTW I think it would be great to collect examples of nontrivial uses of such factory layers, things which aren't trivially Transformers.jl has some macros etc. for constructing its models, which I don't understand well. Example from here: julia> using Transformers
julia> N = 3; using Transformers: PositionEmbedding, Stack, @nntopo, Transformer, Dropout
julia> const encoder = todevice(Stack(
@nntopo(e → pe:(e, pe) → x → x → $N),
PositionEmbedding(512),
.+,
Dropout(0.1),
[Transformer(512, 8, 64, 2048) for i = 1:N]...
))
Stack(PositionEmbedding(512), Base.Broadcast.BroadcastFunction(+), Dropout(0.1), Transformer(head=8, head_size=64, pwffn_size=2048, size=512), Transformer(head=8, head_size=64, pwffn_size=2048, size=512), Transformer(head=8, head_size=64, pwffn_size=2048, size=512), Transformer(head=8, head_size=64, pwffn_size=2048, size=512), Transformer(head=8, head_size=64, pwffn_size=2048, size=512))
julia> encoder.topo
NNTopo{"e → (pe:(e, pe) → (x → (x → 5)))"}
topo_func(model, e)
pe = model[1](e)
x = model[2](e, pe)
x = model[3](x)
x = model[4](x)
x = model[5](x)
x = model[6](x)
x = model[7](x)
x = model[8](x)
x
end |
Beta Was this translation helpful? Give feedback.
-
Transferring over from FluxML/Flux.jl#2107.
@MilesCranmer proposed two implementations of a "layer factory." You can find examples of this in other frameworks: Flax's
@compact
, Haiku's transforms.Ideally, we would be able to just use Julia's
let
block syntax if Functors.jl was opt-out instead of opt-in. This may happen in Functors.jl v0.5.One option is making a factory struct which we can
@functor
. Like so:The above code is just an illustration of the idea. A better implementation is
which should work in Functors v0.4+ (since
Base.Fix1
is@functor
ed).Another option is to use macros. Like so:
Beta Was this translation helpful? Give feedback.
All reactions