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?
1 Answer
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.
Export["testclassifier.wmlf", myClassify]
and thenmyClassify = Import["testclassifier.wmlf"]
$\endgroup$