Saving and Loading Models
Saving and loading of neural networks.
MultiLayerNetwork and ComputationGraph both have save and load methods.
You can save/load a MultiLayerNetwork using:
1
MultiLayerNetwork net = ...
2
net.save(new File("...");
3
4
MultiLayerNetwork net2 = MultiLayerNetwork.load(new File("..."), true);
Copied!
Similarly, you can save/load a ComputationGraph using:
1
ComputationGraph net = ...
2
net.save(new File("..."));
3
4
ComputationGraph net2 = ComputationGraph.load(new File("..."), true);
Copied!
Internally, these methods use the ModelSerializer class, which handles loading and saving models. There are two methods for saving models shown in the examples through the link. The first example saves a normal multi layer network, the second one saves a computation graph.
Here is a basic example with code to save a computation graph using the ModelSerializer class, as well as an example of using ModelSerializer to save a neural net built using MultiLayer configuration.

RNG Seed

If your model uses probabilities (i.e. DropOut/DropConnect), it may make sense to save it separately, and apply it after model is restored; i.e:
1
Nd4j.getRandom().setSeed(12345);
2
ModelSerializer.restoreMultiLayerNetwork(modelFile);
Copied!
This will guarantee equal results between sessions/JVMs.

ModelSerializer

[source]
Utility class suited to save/restore neural net models
writeModel
1
public static void writeModel(@NonNull Model model, @NonNull File file, boolean saveUpdater) throws IOException
Copied!
Write a model to a file
    param model the model to write
    param file the file to write to
    param saveUpdater whether to save the updater or not
    throws IOException
writeModel
1
public static void writeModel(@NonNull Model model, @NonNull File file, boolean saveUpdater,DataNormalization dataNormalization) throws IOException
Copied!
Write a model to a file
    param model the model to write
    param file the file to write to
    param saveUpdater whether to save the updater or not
    param dataNormalization the normalizer to save (optional)
    throws IOException
writeModel
1
public static void writeModel(@NonNull Model model, @NonNull String path, boolean saveUpdater) throws IOException
Copied!
Write a model to a file path
    param model the model to write
    param path the path to write to
    param saveUpdater whether to save the updater or not
    throws IOException
writeModel
1
public static void writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater)
2
throws IOException
Copied!
Write a model to an output stream
    param model the model to save
    param stream the output stream to write to
    param saveUpdater whether to save the updater for the model or not
    throws IOException
writeModel
1
public static void writeModel(@NonNull Model model, @NonNull OutputStream stream, boolean saveUpdater,DataNormalization dataNormalization)
2
throws IOException
Copied!
Write a model to an output stream
    param model the model to save
    param stream the output stream to write to
    param saveUpdater whether to save the updater for the model or not
    param dataNormalization the normalizer ot save (may be null)
    throws IOException
restoreMultiLayerNetwork
1
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull File file) throws IOException
Copied!
Load a multi layer network from a file
    param file the file to load from
    return the loaded multi layer network
    throws IOException
restoreMultiLayerNetwork
1
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull File file, boolean loadUpdater)
2
throws IOException
Copied!
Load a multi layer network from a file
    param file the file to load from
    return the loaded multi layer network
    throws IOException
restoreMultiLayerNetwork
1
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull InputStream is, boolean loadUpdater)
2
throws IOException
Copied!
Load a MultiLayerNetwork from InputStream from an input stream Note: the input stream is read fully and closed by this method. Consequently, the input stream cannot be re-used.
    param is the inputstream to load from
    return the loaded multi layer network
    throws IOException
    see #restoreMultiLayerNetworkAndNormalizer(InputStream, boolean)
restoreMultiLayerNetwork
1
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull InputStream is) throws IOException
Copied!
Restore a multi layer network from an input stream Note: the input stream is read fully and closed by this method. Consequently, the input stream cannot be re-used.
    param is the input stream to restore from
    return the loaded multi layer network
    throws IOException
    see #restoreMultiLayerNetworkAndNormalizer(InputStream, boolean)
restoreMultiLayerNetwork
1
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull String path) throws IOException
Copied!
Load a MultilayerNetwork model from a file
    param path path to the model file, to get the computation graph from
    return the loaded computation graph
    throws IOException
restoreMultiLayerNetwork
1
public static MultiLayerNetwork restoreMultiLayerNetwork(@NonNull String path, boolean loadUpdater)
2
throws IOException
Copied!
Load a MultilayerNetwork model from a file
    param path path to the model file, to get the computation graph from
    return the loaded computation graph
    throws IOException
restoreComputationGraph
1
public static ComputationGraph restoreComputationGraph(@NonNull String path) throws IOException
Copied!
Restore a MultiLayerNetwork and Normalizer (if present - null if not) from the InputStream. Note: the input stream is read fully and closed by this method. Consequently, the input stream cannot be re-used.
    param is Input stream to read from
    param loadUpdater Whether to load the updater from the model or not
    return Model and normalizer, if present
    throws IOException If an error occurs when reading from the stream
restoreComputationGraph
1
public static ComputationGraph restoreComputationGraph(@NonNull String path, boolean loadUpdater)
2
throws IOException
Copied!
Load a computation graph from a file
    param path path to the model file, to get the computation graph from
    return the loaded computation graph
    throws IOException
restoreComputationGraph
1
public static ComputationGraph restoreComputationGraph(@NonNull InputStream is, boolean loadUpdater)
2
throws IOException
Copied!
Load a computation graph from a InputStream
    param is the inputstream to get the computation graph from
    return the loaded computation graph
    throws IOException
restoreComputationGraph
1
public static ComputationGraph restoreComputationGraph(@NonNull InputStream is) throws IOException
Copied!
Load a computation graph from a InputStream
    param is the inputstream to get the computation graph from
    return the loaded computation graph
    throws IOException
restoreComputationGraph
1
public static ComputationGraph restoreComputationGraph(@NonNull File file) throws IOException
Copied!
Load a computation graph from a file
    param file the file to get the computation graph from
    return the loaded computation graph
    throws IOException
restoreComputationGraph
1
public static ComputationGraph restoreComputationGraph(@NonNull File file, boolean loadUpdater) throws IOException
Copied!
Restore a ComputationGraph and Normalizer (if present - null if not) from the InputStream. Note: the input stream is read fully and closed by this method. Consequently, the input stream cannot be re-used.
    param is Input stream to read from
    param loadUpdater Whether to load the updater from the model or not
    return Model and normalizer, if present
    throws IOException If an error occurs when reading from the stream
taskByModel
1
public static Task taskByModel(Model model)
Copied!
    param model
    return
addNormalizerToModel
1
public static void addNormalizerToModel(File f, Normalizer<?> normalizer)
Copied!
This method appends normalizer to a given persisted model.
PLEASE NOTE: File should be model file saved earlier with ModelSerializer
    param f
    param normalizer
addObjectToFile
1
public static void addObjectToFile(@NonNull File f, @NonNull String key, @NonNull Object o)
Copied!
Add an object to the (already existing) model file using Java Object Serialization. Objects can be restored using {- link #getObjectFromFile(File, String)}
    param f File to add the object to
    param key Key to store the object under
    param o Object to store using Java object serialization
Export as PDF
Copy link