Flux: concatenate layers


#1

I’m looking for a way to concatenate layers in Flux like this:

Would this work for that? The input is an array of inputs to the layers and the output is the concatenation of the layers’ outputs. Creating it would be something like Chain(Concat(Chain(A1, B1), Chain(A2, B2, C2)), D, E)

using Flux
using Flux: @treelike

struct Concat
    layers::Array
end

@treelike Concat

function (c::Concat)(inputs::Array)
    output = []
    for i in 1:length(c.layers)
        append!(output, c.layers[i](inputs[i]))
    end
    output
end

#2

You can simply do

Chain(x -> cat(Chain(A1, B1)(x), Chain(A2, B2, C2)(x), dims=3), # Concatenating along channel dimension
          D, E)

#3

Thanks!