# Can I reconstruct a network using Flux.params in Flux.jl?

**URL:** <https://discourse.julialang.org/t/can-i-reconstruct-a-network-using-flux-params-in-flux-jl/90531>\
**Category:** Machine Learning\
**Tags:** flux, machine-learning, neural-network\
**Created:** [November 20, 2022, 8:47am UTC](https://discourse.julialang.org/t/can-i-reconstruct-a-network-using-flux-params-in-flux-jl/90531 "2022-11-20T08:47:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [November 20, 2022, 8:47am UTC](https://discourse.julialang.org/t/can-i-reconstruct-a-network-using-flux-params-in-flux-jl/90531/1 "2022-11-20T08:47:50Z")

</div>

Hi,

I have a custom network `nn`. I can get the parameters `p` of `nn` by `p = Flux.params(nn)`.

Now, I’d like to reconstruct a neural network from the parameters.

Is there any some ways to do the following?

```julia
nn = NN() # construction
nn2 = NN() # as well
p = Flux.params(nn) # parameters
load!(nn, p) # is it possible?

```

---

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [November 20, 2022, 9:15am UTC](https://discourse.julialang.org/t/can-i-reconstruct-a-network-using-flux-params-in-flux-jl/90531/2 "2022-11-20T09:15:25Z")

</div>

I think the recommended way to save a model is by using `BSON` ([Saving & Loading · Flux](https://fluxml.ai/Flux.jl/stable/saving/)) which one line of code and works well. It saves the entire neural network. The parameter object doesn’t usually have all the information needed to restore a neural network, like the activation functions.

If you really want to do it via parameters, you can use `Flux.destructure`:

```julia
parameters, re = Flux.destructure(model)

model_1 = re(parameters)

```

Where `parameters` is a flat vector of all parameter values and `re` is a function that rebuilds the model from the parameter vector.

Overall, I’d recommend `BSON` to reload a model.

---

<div class="post-metadata">

**Author:** ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)\
**Post date:** [November 20, 2022, 9:31am UTC](https://discourse.julialang.org/t/can-i-reconstruct-a-network-using-flux-params-in-flux-jl/90531/3 "2022-11-20T09:31:04Z")

</div>

Thank you so much.  
I know using `BSON` (or using `JLD2`) one can save and load models effortlessly.

But still, I think I need to use destruct to do [this](https://github.com/gdalle/ImplicitDifferentiation.jl/issues/23).

Thanks for your answer!
