# Adding more inputs after convolution in Flux?

**URL:** <https://discourse.julialang.org/t/adding-more-inputs-after-convolution-in-flux/42093>\
**Category:** Machine Learning\
**Created:** [June 26, 2020, 9:46am UTC](https://discourse.julialang.org/t/adding-more-inputs-after-convolution-in-flux/42093 "2020-06-26T09:46:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [June 26, 2020, 9:46am UTC](https://discourse.julialang.org/t/adding-more-inputs-after-convolution-in-flux/42093/1 "2020-06-26T09:46:05Z")

</div>

So I have a large spatial vector as input from which I extract some features using convolution, but I also have some others non-spatial inputs that I’d like to concatenate to my features, is there a way to do that with Flux ? Maybe with a skip connection, but I’m not sure how to handle the different parts of the input.

```julia
x = (rand(10,1,1,1), rand(3))

mconv = Chain(
    Conv((10,1), 1=>5),
    Flux.flatten,
)
    
vcat( mconv(x[1]), x[2] ) #?

```

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [June 26, 2020, 9:59am UTC](https://discourse.julialang.org/t/adding-more-inputs-after-convolution-in-flux/42093/2 "2020-06-26T09:59:57Z")

</div>

This seems to work (even with named tuples, nice).

```julia
x = (spatial = rand(10,1,1,1), nonspatial = rand(5))

mconv = Chain(
    Conv((10,1), 1=>5),
    Flux.flatten,
)
    
m = Chain(
    x -> vcat(mconv(x.spatial), x.nonspatial),
    Dense(10,1)
)

```

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [June 26, 2020, 1:35pm UTC](https://discourse.julialang.org/t/adding-more-inputs-after-convolution-in-flux/42093/3 "2020-06-26T13:35:01Z")

</div>

Nevermind that doesn’t work (seems to “freeze” the first layer).

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [June 26, 2020, 3:35pm UTC](https://discourse.julialang.org/t/adding-more-inputs-after-convolution-in-flux/42093/4 "2020-06-26T15:35:42Z")

</div>

For inputs having multiple elements you can define a struct and apply Flux.@functor on it to get the parameters. The issue in your 2nd solution is that Flux doesn’t know how to reach the mconv parameters.

To solve this

```julia
x = (spatial = rand(10,1,1,1), nonspatial = rand(5))

struct MConv
    model
end

Flux.@functor MConv

(mconv::MConv)(x) = vcat(mconv.model(x.spatial), x.nonspatial)

model = MConv(Chain(
    Conv((10,1), 1=>5),
    Flux.flatten,
))

m = Chain(model, Dense(10, 1))

```
