# Saving/loading Flux models with Julia 1.8.x?

**URL:** <https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275>\
**Category:** Specific Domains\
**Tags:** flux\
**Created:** [October 26, 2022, 7:08am UTC](https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275 "2022-10-26T07:08:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [October 26, 2022, 7:08am UTC](https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275/1 "2022-10-26T07:08:44Z")

</div>

I have been using BSON.jl for saving and loading Flux models, but with Julia 1.8, there is an issue with BSON that appears to be a sticking point ([`setfield!: const field .names of type TypeName cannot be changed` on Julia 1.8 · Issue #109 · JuliaIO/BSON.jl · GitHub](https://github.com/JuliaIO/BSON.jl/issues/109)) Any new recommendations for saving and loading?

---

<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:** [October 26, 2022, 8:32am UTC](https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275/2 "2022-10-26T08:32:52Z")

</div>

There was a discussion about this on slack. Some people suggested  
[GitHub - beacon-biosignals/LegolasFlux.jl: Save Flux model weights in Legolas-powered Arrow tables](https://github.com/beacon-biosignals/LegolasFlux.jl)  
[GitHub - ancapdev/LightBSON.jl: High performance encoding and decoding of BSON data in Julia](https://github.com/ancapdev/LightBSON.jl)

Kyle Daruwala suggested this  
_I would also recommend serializing on the state via `fmapstructure` instead of the model itself. The types need to be resolvable in the current namespace for BSON to work anyways._

and of course serialization, which now should be backward compatible.

These are copy pasted from slack answers. I have not tried them, I usually serialize, since my models are short lived.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [October 26, 2022, 10:14am UTC](https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275/3 "2022-10-26T10:14:39Z")

</div>

Thanks for the links. serialize/deserialize doesn’t seem to work for me, I can use it to save the model, but I get problems when trying to load it back:

```julia
julia> using BSON, Flux,CUDA, Serialization

julia> BSON.@load "models/MA2/mcx2_(n-100).bson" best_model

julia> serialize("junk", best_model)

julia> 
❯ julia --proj --quiet
julia> using BSON, Flux,CUDA, Serialization

julia> deserialize("junk")
ERROR: UndefVarError: #233#234 not defined
Stacktrace:

```

The problem seems to be in the part of the model at the end of this excerpt:

```julia
Chain(
  Chain(
    Chain(
      Parallel(
        +,
        Chain(
          Conv((1, 32), 1 => 32, pad=(0, 0, 16, 15)), # 1_056 parameters
          BatchNorm(32, relu), # 64 parameters, plus 64
          Conv((1, 32), 32 => 32, pad=(0, 0, 16, 15)), # 32_800 parameters
          BatchNorm(32, relu), # 64 parameters, plus 64
        ),
        Conv((1, 1), 1 => 32), # 64 parameters
      ),
      BSON. __deserialized_types__.var"#233#234"(),

```

---

<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:** [October 26, 2022, 11:32am UTC](https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275/4 "2022-10-26T11:32:45Z")

</div>

Do you have some anonymous function on the end of the model? In that slack channel they have written than BSON is broken with 1.8.

What is `BSON. __deserialized_types__.var"#233#234"()` in model definition?

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [October 26, 2022, 11:35am UTC](https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275/5 "2022-10-26T11:35:42Z")

</div>

I think that must come from originally saving the model using BSON. For the future, I will try eliminating BSON entirely, for the original save, and try out serialization again. Thanks for the mention of the slack discussion, I found it.

---

<div class="post-metadata">

**Author:** ![Euhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euhan/32/36548_2.png) [@Euhan](https://discourse.julialang.org/u/Euhan)\
**Post date:** [April 24, 2023, 6:28pm UTC](https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275/6 "2023-04-24T18:28:42Z")

</div>

So if one has Terabytes of these models saved and need to load and evaluate them, this will be smooth sailing using Julia 1.7?

---

<div class="post-metadata">

**Author:** ![Euhan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/euhan/32/36548_2.png) [@Euhan](https://discourse.julialang.org/u/Euhan)\
**Post date:** [April 24, 2023, 8:20pm UTC](https://discourse.julialang.org/t/saving-loading-flux-models-with-julia-1-8-x/89275/7 "2023-04-24T20:20:16Z")

</div>

Also considering that they contain closures…
