Trainer#

class Trainer(approx_mpc)[source]#

Bases: object

Trainer class for training the ApproxMPC.

Added in version >v4.6.0.

The Trainer class is used to train the ApproxMPC. The training data is loaded from the data directory and the training is done using the training data.

Configuration and setup:

Configuring and setting up the Trainer controller involves the following steps:

  1. Configure the Trainer controller with TrainerSettings and TrainerSchedulerSettings. The ApproxMPC instance has the attribute settings and scheduler_settings which is an instance of TrainerSettings and TrainerSchedulerSettings respectively.

  2. To finalize the class configuration setup() may be called. This method sets up the Trainer.

Usage:

The Trainer can be used to sample the data by calling the default_training() method. This method starts training the ApproxMPC with the provided configuration.

Example:

# Create an MPC instance
mpc = do_mpc.controller.MPC(...)
mpc.setup()

# trainer
trainer = Trainer(approx_mpc)
trainer.settings.n_samples = 1000
trainer.settings.scheduler_flag = True
trainer.scheduler_settings.cooldown = 0
trainer.scheduler_settings.patience = 10
trainer.setup()
trainer.default_training()
Parameters:

approx_mpc (do_mpc.approximateMPC.ApproxMPC) – The ApproxMPC object to be trained.

Methods#

default_training#

default_training(self)#

This method handles the training of the ApproxMPC class. Data loaded from the data directory is used to train and validate the ApproxMPC class. Training performance is stored and subsequently plotted and saves depending on the settings chosen during the setup.

Returns:

None

Return type:

None

load_data#

load_data(self)#

This function loads the data from the data directory and returns the relevant data loaders and the optimizer.

Returns:

Returns the relevant data loaders and the optimizer.

Return type:

(torch.utils.data.dataloader.DataLoader, torch.utils.data.dataloader.DataLoader, torch.optim.adam.Adam)

log_value#

log_value(self, val, key)#

This method logs the value of the key in the history. The key cold be either of ‘epoch’, ‘train_loss’, ‘val_loss’, ‘lr’.

Parameters:
  • val (int) – Contains the value to be logged.

  • key (str) – Contains the key to which the value belongs.

print_last_entry#

print_last_entry(self, keys=['epoch,train_loss'])#

This method prints the last entry of the history. The keys to be printed can be provided as a list. Possible entries can be ‘epoch’, ‘train_loss’, ‘val_loss’, ‘lr’.

Parameters:

keys (list, optional) – Store the keys. Possible entries are ‘epoch’, ‘train_loss’, ‘val_loss’, ‘lr’.. Defaults to [“epoch,train_loss”].

scale_dataset#

scale_dataset(self, x, u0)#

Scales the dataset.

Parameters:
  • x (torch.tensor) – States for the system.

  • u0 (torch.tensor) – Inputs of the system.

Returns:

_description_

Return type:

(torch.tensor, torch.tensor)

setup#

setup(self)#

Sets up the Trainer.

This method sets up the Trainer. This method must be called before training the ApproxMPC.

Returns:

None

Return type:

None

train_epoch#

train_epoch(self, optim, train_loader)#

This method calculates the training loss averaged over all epoch, with the training data.

Parameters:
  • optim (torch.optim.adam.Adam) – Contains the optimizer chosen for the backward pass.

  • train_loader (torch.utils.data.dataloader.DataLoader) – This DataLoader contains the training data.

Returns:

Scalar value containing the mean summed squared error of the predicted output from the actual output, averaged over all the epochs.

Return type:

float

train_step#

train_step(self, optim, x, y)#

This method computes the forward pass and the backward pass of the class and returns the loss.

Parameters:
  • optim (torch.optim.adam.Adam) – Contains the optimizer chosen for the backward pass.

  • x (torch.Tensor) – This contains the states of the system.

  • y (torch.Tensor) – This contains the evaluated input of the system. When properly trained, this should be same a the output of the mpc class.

Returns:

Scalar value containing the mean summed squared error of the predicted output from the actual output.

Return type:

float

validation_epoch#

validation_epoch(self, val_loader)#

This method calculates the loss with validation data averaged over all epochs.

Parameters:

val_loader (torch.utils.data.dataloader.DataLoader) – This DataLoader contains the validation data.

Returns:

Scalar value containing the mean summed squared error of the predicted output from the actual output, averaged over all the epochs.

Return type:

float

validation_step#

validation_step(self, x, y)#

This method calculates the loss with validation data.

Parameters:
  • x (torch.Tensor) – This contains the states of the system.

  • y (torch.Tensor) – This contains the evaluated input of the system. When properly trained, this should be same a the output of the mpc class.

Returns:

_description_

Return type:

float

visualize_and_store_history#

visualize_and_store_history(self)#

This method visualizes and stores the history of the training.

Returns:

None

Return type:

None

Attributes#

scheduler_settings#

Trainer.scheduler_settings#

The settings attribute.

The settings attribute is an instance of TrainerSchedulerSettings and is used to configure the Scheduler for the Trainer.

Example to change settings:

trainer.scheduler_settings.cooldown = 0

Note

Settings cannot be updated after calling do_mpc.approximateMPC.Trainer.setup().

Returns:

Contains the configurable settings of the scheduler.

Return type:

TrainerSettings

settings#

Trainer.settings#

The settings attribute.

The settings attribute is an instance of TrainerSettings and is used to configure the Trainer.

Example to change settings:

trainer.settings.n_samples = 1000

Note

Settings cannot be updated after calling do_mpc.approximateMPC.Trainer.setup().

Returns:

Contains the configurable settings of the trainer.

Return type:

TrainerSettings