Training module

The framework supports two complementary training modalities, depending on the model architecture.

Feed-forward Training

Follows the conventional mini-batch paradigm. Samples are shuffled and propagated through an acyclic computation graph, and model parameters are updated based on the instantaneous loss using trainModel with optimizers from the Optimizer module. This modality is suitable for architectures without internal feedback, where predictions depend only on the provided inputs.

Recurrent Training

Targets architectures with closed-loop dependencies, such as feedback connections or coupled multi-model structures. In this modality, training is performed over a finite prediction horizon. The model is rolled out forward in time, and parameter updates are applied only after completing the full rollout using trainModel.

Early stopping strategies from the Early Stopping module can be applied to control convergence in long-horizon training.

nnodely.operators.trainer.Trainer.addMinimize(self, name: str, streamA: str | Stream | Output, streamB: str | Stream | Output, loss_function: str = 'mse') None

Adds a minimize loss function to the model.

Parameters:
  • name (str) – The name of the cost function.

  • streamA (Stream) – The first relation stream for the minimize operation.

  • streamB (Stream) – The second relation stream for the minimize operation.

  • loss_function (str, optional) – The loss function to use from the ones provided. Default is ‘mse’.

Example

model.addMinimize('minimize_op', streamA, streamB, loss_function='mse')
nnodely.operators.trainer.Trainer.removeMinimize(self, name_list: list | str) None

Removes minimize loss functions using the given list of names.

Parameters:

name_list (list of str) – The list of minimize operation names to remove.

Example

model.removeMinimize(['minimize_op1', 'minimize_op2'])
nnodely.operators.trainer.Trainer.getTrainingInfo(self)

Returns a dictionary with the training parameters and information. :param **kwargs: Additional parameters to include in the training information. :type **kwargs: dict

Returns:

A dictionary containing the training parameters and information.

Return type:

dict

nnodely.operators.trainer.Trainer.trainModel(self, *, name: str | None = None, models: str | list | None = None, train_dataset: str | list | dict | None = None, validation_dataset: str | list | dict | None = None, dataset: str | list | None = None, splits: list | None = None, closed_loop: dict | None = None, connect: dict | None = None, step: int | None = None, prediction_samples: int | None = None, shuffle_data: bool | None = None, early_stopping: Callable | None = None, early_stopping_params: dict | None = None, select_model: Callable | None = None, select_model_params: dict | None = None, minimize_gain: dict | None = None, num_of_epochs: int = None, train_batch_size: int = None, val_batch_size: int = None, optimizer: str | Optimizer | None = None, lr: int | float | None = None, lr_param: dict | None = None, optimizer_params: list | None = None, optimizer_defaults: dict | None = None, add_optimizer_params: list | None = None, add_optimizer_defaults: dict | None = None, training_params: dict | None = {}) None

Trains the model using the provided datasets and parameters.

Notes

Note

If no datasets are provided, the model will use all the datasets loaded inside nnodely.

Parameters:
  • name (str or None, optional) – A name used to identify the training operation.

  • models (str or list or None, optional) – A list or name of models to train. Default is all the models loaded.

  • train_dataset (str or None, optional) – The name of datasets to use for training.

  • validation_dataset (str or None, optional) – The name of datasets to use for validation.

  • dataset (str or None, optional) – The name of the datasets to use for training, validation and test. If dataset is None and train_dataset is None, the model will use all the datasets loaded inside nnodely.

  • splits (list or None, optional) – A list of 3 elements specifying the percentage of splits for training, validation, and testing. The three elements must sum up to 100! default is [100, 0, 0] The parameter splits is only used when ‘dataset’ is not None.

  • closed_loop (dict or None, optional) – A dictionary specifying closed loop connections. The keys are input names and the values are output names. Default is None.

  • connect (dict or None, optional) – A dictionary specifying connections. The keys are input names and the values are output names. Default is None.

  • step (int or None, optional) – The step size for training. A big value will result in less data used for each epochs and a faster train. Default is zero.

  • prediction_samples (int or None, optional) – The size of the prediction horizon. Number of samples at each recurrent window Default is zero.

  • shuffle_data (bool or None, optional) – Whether to shuffle the data during training. Default is True.

  • early_stopping (Callable or None, optional) – A callable for early stopping. Default is None.

  • early_stopping_params (dict or None, optional) – A dictionary of parameters for early stopping. Default is None.

  • select_model (Callable or None, optional) – A callable for selecting the best model. Default is None.

  • select_model_params (dict or None, optional) – A dictionary of parameters for selecting the best model. Default is None.

  • minimize_gain (dict or None, optional) – A dictionary specifying the gain for each minimization loss function. Default is None.

  • num_of_epochs (int or None, optional) – The number of epochs to train the model. Default is 100.

  • train_batch_size (int or None, optional) – The batch size for training. Default is 128.

  • val_batch_size (int or None, optional) – The batch size for validation. Default is 128.

  • optimizer (Optimizer or None, optional) – The optimizer to use for training. Default is ‘Adam’.

  • lr (float or None, optional) – The learning rate. Default is 0.001

  • lr_param (dict or None, optional) – A dictionary of learning rate parameters. Default is None.

  • optimizer_params (list or dict or None, optional) – A dictionary of optimizer parameters. Default is None.

  • optimizer_defaults (dict or None, optional) – A dictionary of default optimizer settings. Default is None.

  • training_params (dict or None, optional) – A dictionary of training parameters. Default is None.

  • add_optimizer_params (list or None, optional) – Additional optimizer parameters. Default is None.

  • add_optimizer_defaults (dict or None, optional) – Additional default optimizer settings. Default is None.

Examples

x = Input('x')
F = Input('F')

xk1 = Output('x[k+1]', Fir()(x.tw(0.2)) + Fir()(F.last()))

mass_spring_damper = Modely(seed=0)
mass_spring_damper.addModel('xk1', xk1)
mass_spring_damper.neuralizeModel(sample_time=0.05)

data_struct = ['time', 'x', 'dx', 'F']
data_folder = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'dataset', 'data')
mass_spring_damper.loadData(name='mass_spring_dataset', source=data_folder, format=data_struct, delimiter=';')

params = {'num_of_epochs': 100, 'train_batch_size': 128, 'lr': 0.001}
mass_spring_damper.trainModel(splits=[70, 20, 10], training_params=params)

Example - recurrent training:

x = Input('x')
F = Input('F')

xk1 = Output('x[k+1]', Fir()(x.tw(0.2)) + Fir()(F.last()))

mass_spring_damper = Modely(seed=0)
mass_spring_damper.addModel('xk1', xk1)
mass_spring_damper.addClosedLoop(xk1, x)
mass_spring_damper.neuralizeModel(sample_time=0.05)

data_struct = ['time', 'x', 'dx', 'F']
data_folder = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'dataset', 'data')
mass_spring_damper.loadData(name='mass_spring_dataset', source=data_folder, format=data_struct, delimiter=';')

params = {'num_of_epochs': 100, 'train_batch_size': 128, 'lr': 0.001}
mass_spring_damper.trainModel(splits=[70, 20, 10], prediction_samples=10, training_params=params)

For more example please refer to the training tutorial.