# Chaining Custom Layers using Custom Signature

**URL:** https://discourse.julialang.org/t/chaining-custom-layers-using-custom-signature/98357
**Category:** Machine Learning
**Created:** [May 5, 2023, 1:38am UTC](https://discourse.julialang.org/t/chaining-custom-layers-using-custom-signature/98357 "2023-05-05T01:38:04Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Ian\_L](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ian_l/32/49509_2.png) [@Ian\_L](https://discourse.julialang.org/u/Ian_L)
#### Post date: [May 5, 2023, 1:38am UTC](https://discourse.julialang.org/t/chaining-custom-layers-using-custom-signature/98357/1 "2023-05-05T01:38:04Z")

</div>

Hi. Is there an idiomatic way of chaining layers in Flux when not all of the outputs of the previous layer pipe into the next layer. Unlike Flux.Chain which directly pipes all of the data, how can one create a custom layer that emulates something like:

```julia
# Pass auxilliary data/matrix A
function (model::CustomLayer)(x_0, A) 
  x_1 = model.submodel(x_0, A)
  x_2 = model.submodel(x_1, A)
  # ... 
  x_n = model.submodel(x_n_1,A)
end

```

Specifically, I’m looking for advice on two design choices. 1) How should I go about implementing this custom chain when all layers of the chain require the same auxilliary data. Are there Flux constructs that can express this? 2) If not and the solution just requires writing some type of loop (over n blocks), how should I properly go about this to avoid performance/type-stability issues?

Thanks.

---

<div class="post-metadata">

### Author: ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)
#### Post date: [May 5, 2023, 6:01am UTC](https://discourse.julialang.org/t/chaining-custom-layers-using-custom-signature/98357/2 "2023-05-05T06:01:37Z")

</div>

Maybe the [Join](http://fluxml.ai/Flux.jl/stable/models/advanced/#Multiple-inputs:-a-custom-Join-layer), Split and Parallel layers can serve your purpose. Otherwise, just define a custom layer and its forward pass;

```julia
struct CustomLayer
  ...
end

Flux.@functor CustomLayer

function (layer::CustomLayer)(x_0, A)
  ...
  return x_n
end

```

Using for loops is fine.
