6
$\begingroup$

I've written quite a few projects that involve training a neural network through the Classify and Predict functions, but every time I want to do something new or restart Mathematica I have to train the network again. Is there a way to save the trained network so it can be used at other times without having to re-train the network and without having to restart Mathematica?

$\endgroup$
3
  • 3
    $\begingroup$ You can save the trained functions a few different ways, for example Export["testclassifier.wmlf", myClassify] and then myClassify = Import["testclassifier.wmlf"] $\endgroup$
    – Carl Lange
    Commented Jun 24, 2020 at 10:21
  • $\begingroup$ @CarlLange That seems like a reasonable answer; could you write it up as such? $\endgroup$
    – MarcoB
    Commented Jun 24, 2020 at 14:42
  • $\begingroup$ @MarcoB Sure, I have done that. Char, feel free to elaborate if you need more information. $\endgroup$
    – Carl Lange
    Commented Jun 24, 2020 at 18:00

1 Answer 1

9
$\begingroup$

There are plenty of different ways to save Classify and Predict functions - and also trained Neural networks and so on. Most of the ways involve using Export and Import.

For Classify and Predict, the WMLF format is probably the easiest:

c = Classify[{1, 2, 3, 4} -> {"A", "A", "B", "B"}]

Export["testclassifier.wmlf", c]

And then to use the classifier elsewhere:

c = Import["testclassifier.wmlf"]

The same goes for Predict functions.

For Neural Networks (trained or untrained), you may want to use the WLNet format:

net = NetInitialize@
  NetGraph[{LinearLayer[10], LinearLayer[3]}, {1 -> 2}, 
   "Input" -> {2}]

Export["testnet.wlnet", net] 

Then to import:

net2 = Import["testnet.wlnet"]

You can also use the MXNet format here, but its usage is better suited to porting networks from Wolfram Language to MXNet implementations elsewhere.

Another way to do this is to use the Iconize function, which saves the data directly in the notebook. This is not always recommended for machine learning data, since that tends to be rather large, but it can be done.

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.