-
A simple implementation: struct BroadcastLayer{T <: NamedTuple} <: AbstractExplicitContainerLayer{(:layers,)}
layers::T
end
function BroadcastLayer(layers...)
for l in layers
if !iszero(statelength(l))
throw(ArgumentError("Stateful layer `$l` are not supported for `BroadcastLayer`."))
end
end
names = ntuple(i -> Symbol("layer_$i"), length(layers))
return BroadcastLayer(NamedTuple{names}(layers))
end
BroadcastLayer(; kwargs...) = BroadcastLayer(connection, (; kwargs...))
function (m::BroadcastLayer)(x, ps, st::NamedTuple{names}) where {names}
results = (first ∘ Lux.apply).(values(m.layers), x, values(ps), values(st))
return results, st
end
Base.keys(m::BroadcastLayer) = Base.keys(getfield(m, :layers)) Originally posted by @avik-pal in #282 (comment) |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
BroadcastLayer(; kwargs...) = BroadcastLayer(connection, (; kwargs...)) What is the role of |
Beta Was this translation helpful? Give feedback.
-
I still can't understand what BroadcastLayer does when there are multiple layers passed in.
Is my understanding correct and is it necessary to document this? |
Beta Was this translation helpful? Give feedback.
-
@avik-pal Hi, Could you help me confirm that my understanding is correct? If its correct I will open a PR soon |
Beta Was this translation helpful? Give feedback.
-
Honesty with the introduction of @compact(; layer = <your layer>) do x::Union{NTuple{<:AbstractArray}, AbstractVector{<:AbstractArray}
@return map(layer, x)
end |
Beta Was this translation helpful? Give feedback.
-
@avik-pal Thank you very much! This is useful! |
Beta Was this translation helpful? Give feedback.
Honesty with the introduction of
@compact
, it might make sense not to have them inbuilt. Broadcasting is effectively: