Saving and Loading NeuralNetwork Models#

USEncrypt.ai makes is simple to save and load NeuralNetwork models.

Saving a Model#

A model can be saved using the save_model() function:

>>> net = usencrypt.ai.nn.NeuralNetwork()
>>> net.add(ue.ai.nn.layers.FCLayer(4, 5))
>>> net.add(ue.ai.nn.layers.ReluLayer())
>>> net.add(ue.ai.nn.layers.FCLayer(5, 3))
>>> net.add(ue.ai.nn.layers.SoftmaxLayer())
>>> net.summary()
Model: "NeuralNetwork"
=============================================================================
 Layer                    Type                Output Shape        Parameters
=============================================================================
 FCLayer                  FCLayer             (5, None)           25
 ReluLayer                ReluLayer           (5, None)           0
 FCLayer_1                FCLayer             (3, None)           18
 SoftmaxLayer             SoftmaxLayer        (3, None)           0
=============================================================================
 Total Parameters: 43
 Trainable Parameters: 43
 Non-Trainable Parameters: 0
=============================================================================
>>> net.save_model('../saved_model.json')
Model saved successfully. Saved in "../saved_model.json".

The save_model() function saves a NeuralNetwork object’s configuration, and allows users to customize what information to store. This includes:

  • Saving the layers.

  • Saving the parameters.

  • Saving the loss function.

  • Saving the optimizer.

Loading a Model#

Once a model is saved, it can be loaded into another model using the load_model() function:

>>> net = usencrypt.ai.nn.NeuralNetwork()
>>> net.load_model('../saved_model.json')
Model loaded successfully.
>>> net.summary()
Model: "NeuralNetwork"
=============================================================================
 Layer                    Type                Output Shape        Parameters
=============================================================================
 FCLayer                  FCLayer             (5, None)           25
 ReluLayer                ReluLayer           (5, None)           0
 FCLayer_1                FCLayer             (3, None)           18
 SoftmaxLayer             SoftmaxLayer        (3, None)           0
=============================================================================
 Total Parameters: 43
 Trainable Parameters: 43
 Non-Trainable Parameters: 0
=============================================================================

Further, the load_model() function allows for the same level of customization as save_model().