3

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?

logankilpatrick
  • 13,148
  • 7
  • 44
  • 125
  • Fair question, we can perhaps move this to a meta thread but I think there are a whole host of benefits to doing this. – logankilpatrick Jul 11 '21 at 12:10
  • 2
    Because you answered your own question within one minute of posting it, I assume your question is not actually, "How do I do this," but more along the lines of, "Why doesn't the documentation spell out how to do this?" If the documentation is a problem and you demonstrably know how to fix it, I would recommend [opening an issue](https://github.com/FluxML/Flux.jl/issues) with Flux.jl or making the modifications yourself and [issuing a pull request](https://github.com/FluxML/Flux.jl/pulls) so that everyone who reads the official documentation can benefit. – PaSTE Jul 11 '21 at 13:02
  • The question really is "how do I do this". I asked myself the question, searched, read the docs, and then went and wrote an answer (as is common with a self answered question). Thanks for the feedback nonetheless! – logankilpatrick Jul 11 '21 at 13:05
  • 2
    @logankilpatrick, I think it would be beneficial to discuss this _somewhere_, maybe indeed on Meta. I understand that you're trying to, sort of, "bring Julia to Stack Overflow", and I support this movement in general, but I'm not sure how helpful posts of this particular format are... – ForceBru Jul 11 '21 at 15:16
  • 2
    IMHO it's sort of decently useful, because a lot of these sorts of APIs in Julia-land have _finally_ started stabilizing recently, so anything that helps people find the current correct ways of doing things (as opposed to outdated versions that often pop up when searching) is a good thing. – cbk Jul 12 '21 at 19:18
  • 10
    Here is a snippet from the stack overflow help: https://stackoverflow.com/help/self-answer about "Can I answer my own question?": "Yes! Stack Exchange has always explicitly encouraged users to answer their own questions. If you have a question that you already know the answer to, and you would like to document that knowledge in public so that others (including yourself) can find it later, it's perfectly okay to ask and answer your own question on a Stack Exchange site." – Kristoffer Carlsson Aug 28 '21 at 13:08
  • 3
    Self answered questions are not only permitted but encouraged on stack overflow. Logan has often taken on it themself to take question basked on Julia channels like slack (which is waaay more active than Julia is on stack overflow) and summarize answers here. Idk if this is one of those cases or a case for there own edification (which I know they also often do). But these questions are welcome and encouraged. Contributing FAQs to SO, is a totally different activity to creating dovs. Needs different skills and reaches a different audience. – Frames Catherine White Aug 28 '21 at 18:07

1 Answers1

7

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.

logankilpatrick
  • 13,148
  • 7
  • 44
  • 125