# Save neural network after training

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?

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

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.