# How do you deserialize stateful optimizers in Flux?

**URL:** https://discourse.julialang.org/t/how-do-you-deserialize-stateful-optimizers-in-flux/38644
**Category:** Machine Learning
**Tags:** flux
**Created:** [May 3, 2020, 3:48am UTC](https://discourse.julialang.org/t/how-do-you-deserialize-stateful-optimizers-in-flux/38644 "2020-05-03T03:48:18Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [May 3, 2020, 3:48am UTC](https://discourse.julialang.org/t/how-do-you-deserialize-stateful-optimizers-in-flux/38644/1 "2020-05-03T03:48:18Z")

</div>

For some stateful optimizers, like [ADAM](https://github.com/FluxML/Flux.jl/blob/master/src/optimise/optimisers.jl#L164-L168), it has an `IdDict` field to store `momentums` of parameters. When deserializing the optimizer with BSON, the `id` info is lost in the newly created `IdDict`. This makes it hard to truly recover from some checkpoints.

(I think in TF, each variable has a name so things may be easier?)

---

<div class="post-metadata">

### Author: ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)
#### Post date: [May 3, 2020, 6:20am UTC](https://discourse.julialang.org/t/how-do-you-deserialize-stateful-optimizers-in-flux/38644/2 "2020-05-03T06:20:42Z")

</div>

This should be fixed in latest BSON release

---

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [May 3, 2020, 7:24am UTC](https://discourse.julialang.org/t/how-do-you-deserialize-stateful-optimizers-in-flux/38644/3 "2020-05-03T07:24:42Z")

</div>

Ah, thanks for reminding!

I just find the _trick_ here is to save the model and the optimizer into a single bson file.

---

<div class="post-metadata">

### Author: ![CarloLucibello](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carlolucibello/32/3278_2.png) [@CarloLucibello](https://discourse.julialang.org/u/CarloLucibello)
#### Post date: [May 3, 2020, 7:36am UTC](https://discourse.julialang.org/t/how-do-you-deserialize-stateful-optimizers-in-flux/38644/4 "2020-05-03T07:36:21Z")

</div>

Why? Won’t saving them separately work the same?

---

<div class="post-metadata">

### Author: ![findmyway](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/findmyway/32/4946_2.png) [@findmyway](https://discourse.julialang.org/u/findmyway)
#### Post date: [May 3, 2020, 7:47am UTC](https://discourse.julialang.org/t/how-do-you-deserialize-stateful-optimizers-in-flux/38644/5 "2020-05-03T07:47:39Z")

</div>

Yeah, I’m afraid not.

```julia
julia> using BSON

julia> a = [1]
1-element Array{Int64,1}:
 1

julia> b = IdDict()
IdDict{Any,Any}b with 0 entries

julia> b[a] = 2
2

julia> b
IdDict{Any,Any} with 1 entry:
  [1] => 2

julia> BSON.@save "b.bson" b

julia> BSON.@save "a.bson" a
 
julia> BSON.@load "a.bson" a

julia> BSON.@load "b.bson" b

julia> a
1-element Array{Int64,1}:
 1

julia> b
IdDict{Any,Any} with 1 entry:
  [1] => 2

julia> b[a]
ERROR: KeyError: key [1] not found
...

```
