# How to create a Flux model from parameter vector?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-flux-model-from-parameter-vector/52928>\
**Category:** New to Julia\
**Tags:** flux\
**Created:** [January 6, 2021, 11:50am UTC](https://discourse.julialang.org/t/how-to-create-a-flux-model-from-parameter-vector/52928 "2021-01-06T11:50:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [January 6, 2021, 11:50am UTC](https://discourse.julialang.org/t/how-to-create-a-flux-model-from-parameter-vector/52928/1 "2021-01-06T11:50:42Z")

</div>

Hi,  
I have a parameter vector and shape array as below:

```julia
shape =[((2,5),2),((2,2),2)] #shape of layers : weights and bias

```

`w = rand(18) #weights and bias together.`

In this case elements of `w` are divided as below:

- First 10 elements of `w` are weights of layer 1
- Next 2 elements of `w` are bias of layer 1
- First 4elements of `w` are weights of layer 2
- Next 2 elements of `w` are bias of layer 2

What will be the best way to create a Flux model using like below using elements of `w`

`Chain(Dense(5,2), Dense(2,2))`

Thanks and Regards  
Manu

---

<div class="post-metadata">

**Author:** ![Tomas\_Pevny](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomas_pevny/32/25466_2.png) [@Tomas\_Pevny](https://discourse.julialang.org/u/Tomas_Pevny)\
**Post date:** [January 6, 2021, 12:52pm UTC](https://discourse.julialang.org/t/how-to-create-a-flux-model-from-parameter-vector/52928/2 "2021-01-06T12:52:00Z")

</div>

I would first create the model using something like

```julia
model = Chain(map(x -> Dense(x[1]...), shape)...)

```

and then use loadparams!(model, theta),  
where I would create `theta` as

```julia
theta = mapreduce(x -> [view(parameter_vector, x[1]...),view(parameter_vector, x[2])], vcat, shape)

```

Also, there is `destructure` and `restructure` commands, converting model to a vector of parameters and back, so I would take a look on those.

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [January 6, 2021, 1:01pm UTC](https://discourse.julialang.org/t/how-to-create-a-flux-model-from-parameter-vector/52928/3 "2021-01-06T13:01:11Z")

</div>

@Tomas_Pevny: I really appreciate your reply. Your solution saved my time… Amazing…  
I looked on destructure

```julia
θ, re = destructure(model);

```

Then I can update model with new weight as

```julia
re(w)

```

---

<div class="post-metadata">

**Author:** ![ToucheSir](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/touchesir/32/14411_2.png) [@ToucheSir](https://discourse.julialang.org/u/ToucheSir)\
**Post date:** [January 6, 2021, 5:18pm UTC](https://discourse.julialang.org/t/how-to-create-a-flux-model-from-parameter-vector/52928/4 "2021-01-06T17:18:41Z")

</div>

Yeah, destructure was written for exactly this use-case.
