You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
julia> model = MyModel(); # MyModel definition must be available
32
41
33
-
julia> model
34
-
Chain(
35
-
Dense(10 => 5, relu), # 55 parameters
36
-
Dense(5 => 2), # 12 parameters
37
-
NNlib.softmax,
38
-
) # Total: 4 arrays, 67 parameters, 524 bytes.
42
+
julia> Flux.loadmodel!(model, model_state);
39
43
```
40
44
41
-
Models are just normal Julia structs, so it's fine to use any Julia storage
42
-
format for this purpose. BSON.jl is particularly well supported and most likely
43
-
to be forwards compatible (that is, models saved now will load in future
44
-
versions of Flux).
45
-
46
45
!!! note
47
46
48
47
If a saved model's parameters are stored on the GPU, the model will not load
49
48
later on if there is no GPU support available. It's best to [move your model
50
49
to the CPU](gpu.md) with `cpu(model)` before saving it.
51
50
52
-
!!! warning
53
-
54
-
Previous versions of Flux suggested saving only the model weights using
55
-
`@save "mymodel.bson" params(model)`.
56
-
This is no longer recommended and even strongly discouraged.
57
-
Saving models this way will only store the trainable parameters which
58
-
will result in incorrect behavior for layers like `BatchNorm`.
59
-
60
-
```julia
61
-
julia>using Flux
62
-
63
-
julia> model =Chain(Dense(10=>5,relu),Dense(5=>2),softmax)
64
-
Chain(
65
-
Dense(10=>5, relu), # 55 parameters
66
-
Dense(5=>2), # 12 parameters
67
-
NNlib.softmax,
68
-
) # Total: 4 arrays, 67 parameters, 524 bytes.
69
-
70
-
julia> weights = Flux.params(model);
71
-
```
72
-
73
-
Loading the model as shown above will return a new model with the stored parameters.
74
-
But sometimes you already have a model, and you want to load stored parameters into it.
75
-
This can be done as
76
-
77
-
```julia
78
-
using Flux: loadmodel!
79
-
using BSON
80
-
81
-
# some predefined model
82
-
model =Chain(Dense(10=>5, relu), Dense(5=>2), softmax)
83
-
84
-
# load one model into another
85
-
model =loadmodel!(model, BSON.load("mymodel.bson")[:model])
86
-
```
87
-
88
-
This ensures that the model loaded from `"mymodel.bson"` matches the structure of `model`. [`Flux.loadmodel!`](@ref) is also convenient for copying parameters between models in memory.
89
-
90
-
```@docs
91
-
Flux.loadmodel!
92
-
```
93
51
94
52
## Checkpointing
95
53
@@ -98,50 +56,91 @@ In longer training runs it's a good idea to periodically save your model, so tha
will produce a series of models like `"model-2018-03-06T02:57:10.41.bson"`. You
80
+
will produce a series of models like `"model-2018-03-06T02:57:10.41.jld2"`. You
125
81
could also store the current test set loss, so that it's easy to (for example)
126
82
revert to an older copy of the model if it starts to overfit.
127
83
128
84
```julia
129
-
@save"model-$(now()).bson" model loss =testloss()
85
+
jldsave("model-$(now()).jld2", model_state = Flux.state(m), loss =testloss())
130
86
```
131
87
132
-
Note that to resume a model's training, you might need to restore other stateful parts of your training loop. Possible examples are stateful optimisers (which usually utilize an `IdDict` to store their state), and the randomness used to partition the original data into the training and validation sets.
88
+
Note that to resume a model's training, you might need to restore other stateful parts of your training loop. Possible examples are the optimiser state and the randomness used to partition the original data into the training and validation sets.
133
89
134
90
You can store the optimiser state alongside the model, to resume training
135
-
exactly where you left off. BSON is smart enough to [cache values](https://github.com/JuliaIO/BSON.jl/blob/v0.3.4/src/write.jl#L71) and insert links when saving, but only if it knows everything to be saved up front. Thus models and optimisers must be saved together to have the latter work after restoring.
0 commit comments