# Saving and Loading Error BSON.jl

**URL:** https://discourse.julialang.org/t/saving-and-loading-error-bson-jl/83555
**Category:** Machine Learning
**Created:** [June 30, 2022, 8:18am UTC](https://discourse.julialang.org/t/saving-and-loading-error-bson-jl/83555 "2022-06-30T08:18:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ccccc](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@ccccc](https://discourse.julialang.org/u/ccccc)
#### Post date: [June 30, 2022, 8:18am UTC](https://discourse.julialang.org/t/saving-and-loading-error-bson-jl/83555/1 "2022-06-30T08:18:37Z")

</div>

I’m getting an error while saving / loading in BSON:

```julia
res1_node = DiffEqFlux.sciml_train(loss, p, ADAM(0.01), cb=callback, maxiters = 500)

using BSON: @save
@save "res1_node" res1_node

```

when I try to load:

```julia
using BSON: @load

@load "res1_node" res1_node

```

I get the error:

```julia
ERROR: UndefVarError: Optimization not defined
Stacktrace:
  [1] (::BSON.var"#31#32")(m::Module, f::String)
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\extensions.jl:21
  [2] BottomRF
    @ .\reduce.jl:81 [inlined]
  [3] _foldl_impl(op::Base.BottomRF{BSON.var"#31#32"}, init::Module, itr::Vector{Any})
    @ Base .\reduce.jl:58
  [4] foldl_impl
    @ .\reduce.jl:48 [inlined]
  [5] mapfoldl_impl
    @ .\reduce.jl:44 [inlined]
  [6] _mapreduce_dim
    @ .\reducedim.jl:327 [inlined]
  [7] #mapreduce#731
    @ .\reducedim.jl:322 [inlined]
  [8] #reduce#733
    @ .\reducedim.jl:371 [inlined]
  [9] resolve(fs::Vector{Any}, init::Module)
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\extensions.jl:21
 [10] (::BSON.var"#35#36")(d::Dict{Symbol, Any}, init::Module)
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\extensions.jl:64
 [11] _raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\read.jl:80
 [12] raise_recursive(d::Dict{Symbol, Any}, cache::IdDict{Any, Any}, init::Module)
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\read.jl:93
 [13] (::BSON.var"#23#24"{IdDict{Any, Any}, Module})(x::Dict{Symbol, Any})
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\read.jl:98
 [14] applychildren!(f::BSON.var"#23#24"{IdDict{Any, Any}, Module}, x::Vector{Any})
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\BSON.jl:26
 [15] raise_recursive
    @ C:\Users\44745\.julia\packages\BSON\rOaki\src\read.jl:98 [inlined]
 [16] (::BSON.var"#17#20"{IdDict{Any, Any}, Module})(x::Vector{Any})
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\read.jl:80
 [17] applychildren!(f::BSON.var"#17#20"{IdDict{Any, Any}, Module}, x::Dict{Symbol, Any})
    @ BSON C:\Users\44745\.julia\packages\BSON\rOaki\src\BSON.jl:19

```

I tried explicitly defining the optimizer:

```julia
opt1 = ADAM(0.01)
@save "res1_nodeopt" res1_node opt1
@load "res1_nodeopt" res1_node opt1

```

but get the same error…  
Does anyone know how to fix this?  
Thanks

---

<div class="post-metadata">

### Author: ![josuagrw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/josuagrw/32/1015_2.png) [@josuagrw](https://discourse.julialang.org/u/josuagrw)
#### Post date: [June 30, 2022, 9:00am UTC](https://discourse.julialang.org/t/saving-and-loading-error-bson-jl/83555/2 "2022-06-30T09:00:34Z")

</div>

Please share the complete error message (including the part above the stack trace)

---

<div class="post-metadata">

### Author: ![ccccc](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@ccccc](https://discourse.julialang.org/u/ccccc)
#### Post date: [June 30, 2022, 10:05am UTC](https://discourse.julialang.org/t/saving-and-loading-error-bson-jl/83555/3 "2022-06-30T10:05:02Z")

</div>

Edited, put the ``` in the wrong place…

---

<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: [July 1, 2022, 5:16pm UTC](https://discourse.julialang.org/t/saving-and-loading-error-bson-jl/83555/4 "2022-07-01T17:16:32Z")

</div>

Try adding [GitHub - SciML/Optimization.jl: Local, global, and beyond optimization for scientific machine learning (SciML)](https://github.com/SciML/Optimization.jl/) to your environment and importing it before calling `@load`. It’s already being loaded by other packages so you’re not getting any extra dependencies, but you need to explicitly add it to use `using`/`import`.

---

<div class="post-metadata">

### Author: ![ccccc](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@ccccc](https://discourse.julialang.org/u/ccccc)
#### Post date: [July 2, 2022, 12:07pm UTC](https://discourse.julialang.org/t/saving-and-loading-error-bson-jl/83555/5 "2022-07-02T12:07:58Z")

</div>

Thanks! That works
