juliabsonflux.jlflux-machine-learning

How to load a trained model with BSON in Flux.jl


I trained a model earlier in Flux.jl and saved it by doing:

@save "mymodel.bson" model

Now I want to load that model back and use it again. How can I achieve this in Flux?


Solution

  • Similar to the @save macro used above, there is also a built in @load macro which comes from the BSON package. You can access it by doing using BSON: @load and then quite simply do something like:

    julia> using Flux
    
    julia> using BSON: @load
    
    julia> @load "mymodel.bson" model
    
    julia> model
    Chain(Dense(10, 5, NNlib.relu), Dense(5, 2), NNlib.softmax)
    

    You can find out more about saving and loading models in the Flux.jl docs.