# Efficient way for executing multiple models

**URL:** <https://discourse.julialang.org/t/efficient-way-for-executing-multiple-models/110804>\
**Category:** Performance\
**Tags:** flux\
**Created:** [February 26, 2024, 7:51pm UTC](https://discourse.julialang.org/t/efficient-way-for-executing-multiple-models/110804 "2024-02-26T19:51:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![cirobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cirobr/32/219994_2.png) [@cirobr](https://discourse.julialang.org/u/cirobr)\
**Post date:** [February 26, 2024, 7:51pm UTC](https://discourse.julialang.org/t/efficient-way-for-executing-multiple-models/110804/1 "2024-02-26T19:51:11Z")

</div>

Cheers,

Let `chain` be a `Flux.Chain` of FCNs. I want to execute each of the chained models independently with the same input array, then concatenate all outputs as the model result. I did the following:

> using Flux: Chain, @functor, Conv, relu
> 
> struct Mymodel  
> chain  
> N  
> end
> 
> function Mymodel(ch\_in, ch\_out, N)  
> models = [Conv((1,1), ch\_in =\> ch\_out, relu) for i in 1:N]  
> chain = Chain(models…)  
> return Mymodel(chain, N)  
> end
> 
> function (m::Mymodel)(x)  
> res = map(i → m.chain[i] (x), 1:m.N)  
> return cat(res…, dims=3)  
> end
> 
> @functor Mymodel

The model trains and executes as I expected, except for the unusually long training time apparently added by the `map` instruction line. Would appreciate if someone could shed a light on the root cause and perhaps suggest a more efficient way to build the logic.

Thanks in advance.
