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


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?

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

I think the recommended way to save a model is by using BSON (Saving & Loading · Flux) 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:

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.

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.

Thanks for your answer!