Export module

nnodely.operators.exporter.Exporter.saveTorchModel(self, name: str = 'net', model_folder: str | None = None, *, models: str | None = None) None

Saves the neural network model in PyTorch format.

Parameters:
  • name (str, optional) – The name of the saved model file. Default is ‘net’.

  • model_folder (str or None, optional) – The folder to save the model file in. Default is None.

  • models (list or None, optional) – A list of model names to save. If None, the entire model is saved. Default is None.

Raises:

RuntimeError – If the model is not neuralized.

Examples

model = Modely()
model.neuralizeModel()
model.saveTorchModel(name='example_model', model_folder='path/to/save')
nnodely.operators.exporter.Exporter.loadTorchModel(self, name: str = 'net', model_folder: str | None = None) None

Loads a neural network model from a PyTorch format file.

Parameters:
  • name (str, optional) – The name of the model file to load. Default is ‘net’.

  • model_folder (str or None, optional) – The folder to load the model file from. Default is None.

Raises:

RuntimeError – If the model is not neuralized.

Examples

model = Modely()
model.neuralizeModel()
model.loadTorchModel(name='example_model', model_folder='path/to/load')
nnodely.operators.exporter.Exporter.saveModel(self, name: str = 'net', model_folder: str | None = None, *, models: str | list | None = None) None

Saves the neural network model definition in a json file.

Parameters:
  • name (str, optional) – The name of the saved model file. Default is ‘net’.

  • model_folder (str or None, optional) – The path to save the model file. Default is None.

  • models (list or None, optional) – A list of model names to save. If None, the entire model is saved. Default is None.

Raises:

RuntimeError – If the network has not been defined.

Examples

model = Modely()
model.neuralizeModel()
model.saveModel(name='example_model', model_folder='folder/')
nnodely.operators.exporter.Exporter.loadModel(self, name: str = 'net', model_folder: str | None = None) None

Loads a neural network model from a json file containing the model definition.

Parameters:
  • name (str or None, optional) – The name of the model file to load. Default is ‘net’.

  • model_folder (str or None, optional) – The folder to load the model file from. Default is None.

Raises:

RuntimeError – If there is an error loading the network.

Examples

model = Modely()
model.loadModel(name='example_model', model_folder='path/to/load')
nnodely.operators.exporter.Exporter.exportPythonModel(self, name: str = 'net', model_folder: str | None = None, *, models: str | None = None) None

Exports the neural network model as a standalone PyTorch Module class.

Parameters:
  • name (str, optional) – The name of the exported model file. Default is ‘net’.

  • model_folder (str or None, optional) – The path to save the exported model file. Default is None.

  • models (list or None, optional) – A list of model names to export. If None, the entire model is exported. Default is None.

Raises:

RuntimeError – If the network has not been defined. If the model is traced and cannot be exported to Python. If the model is not neuralized.

Examples

model = Modely(name='example_model')
model.neuralizeModel()
model.exportPythonModel(name='example_model', model_folder='folder/')
nnodely.operators.exporter.Exporter.importPythonModel(self, name: str = 'net', model_folder: str | None = None) None

Imports a neural network model from a standalone PyTorch Module class.

Parameters:
  • name (str or None, optional) – The name of the model file to import. Default is ‘net’.

  • model_folder (str or None, optional) – The folder to import the model file from. Default is None.

Raises:

RuntimeError – If there is an error loading the network.

Examples

model = Modely()
model.importPythonModel(name='example_model', model_folder='path/to/import')
nnodely.operators.exporter.Exporter.exportONNX(self, inputs_order: list | None = None, outputs_order: list | None = None, name: str = 'net', model_folder: str | None = None, *, models: str | list | None = None) None

Exports the neural network model to an ONNX file.

Note

The input_order may contain all the inputs of the model in the order that you want to export them.

Parameters:
  • inputs_order (list) – The order of the input variables.

  • outputs_order (list) – The order of the output variables.

  • models (list or None, optional) – A list of model names to export. If None, the entire model is exported. Default is None.

  • name (str, optional) – The name of the exported ONNX file. Default is ‘net’.

  • model_folder (str or None, optional) – The folder to save the exported ONNX file. Default is None.

Raises:

RuntimeError – If the network has not been defined. If the model is traced and cannot be exported to ONNX. If the model is not neuralized. If the model is loaded and not created.

Examples

input1 = Input('input1').last()
input2 = Input('input2').last()
out = Output('output1', input1+input2)

model = Modely()
model.neuralizeModel()
model.exportONNX(inputs_order=['input1', 'input2'], outputs_order=['output1'], name='example_model', model_folder='path/to/export')
nnodely.operators.exporter.Exporter.onnxInference(self, inputs: dict, name: str = 'net', model_folder: str | None = None) dict

Run an inference session using an onnx model previously exported using the nnodely framework.

Note

Feed-Forward ONNX model For feed-forward models, the onnx model expect all the recurrent inputs to have 3 dimensions. The first dimension is the batch size, the second is the time window and the third is the feature dimension.

Note

Recurrent ONNX model For recurrent models, the onnx model expect all the inputs to have 4 dimensions. The first dimension is the prediction horizon, the second is the batch size, the third is the time window and the fourth is the feature dimension. For recurrent models, the onnx model expect all the recurrent inputs to have 3 dimensions. The first dimension is the batch size, the second is the time window, the third is the feature dimension

Parameters:
  • inputs (dict) – A dictionary containing the input and recurrent variables to be used to make the inference. Recurrent variables are mandatory and are used to initialize the state memory of the model.

  • path (str) – The path to the ONNX file to use.

Raises:

RuntimeError – If the shape of the inputs are not equals to the ones defined in the onnx model. If the batch size is not equal for all the inputs.

Examples

import numpy as np
from nnodely.basic.model import Modely
from nnodely.layers.input import Input

x = Input('x')

model_folder = "folder/"

dummy_input = {
    'x': np.ones(shape=(3, 1, 1)).astype(np.float32)
}

predictions = Modely().onnxInference(dummy_input, model_folder)

Example - Recurrent:

import numpy as np
from nnodely.basic.model import Modely
from nnodely.layers.input import Input

x = Input('x')
y = Input('y')

model_folder = "folder/"

dummy_input = {
    'x': np.ones(shape=(3, 1, 1, 1)).astype(np.float32),
    'y': np.ones(shape=(1, 1, 1)).astype(np.float32)
}

predictions = Modely().onnxInference(dummy_input, model_folder)
nnodely.operators.exporter.Exporter.exportReport(self, name: str = 'net', model_folder: str | None = None) None

Generates a PDF report with plots containing the results of the training and validation of the neural network.

Parameters:
  • name (str, optional) – The name of the exported report file. Default is ‘net’.

  • model_folder (str or None, optional) – The folder to save the exported report file. Default is None.

Examples

model = Modely()
model.neuralizeModel()
model.trainModel(train_dataset='train_dataset', validation_dataset='val_dataset', num_of_epochs=10)
model.exportReport(name='example_model', model_folder='path/to/export')

For more examples of how to use the export module, please refer to the export tutorial.