robustML.advertrain.training package

Submodules

robustML.advertrain.training.adversarial_training module

class robustML.advertrain.training.adversarial_training.AdversarialTraining(model: Module, optimizer: Optimizer, loss_func, device: device, epsilon: float)[source]

Bases: ClassicalTraining

A training class that incorporates adversarial training using Projected Gradient Descent (PGD).

This class extends ClassicalTraining by modifying the preprocessing of batches to include the generation of adversarial examples using PGD.

epsilon

The maximum perturbation allowed for adversarial examples.

Type:

float

preprocess_batch(x: Tensor, y: Tensor, epoch: int) Tuple[Tensor, Tensor][source]

Preprocesses a batch of data by generating adversarial examples using PGD.

Parameters:
  • x (Tensor) – The input data batch.

  • y (Tensor) – The ground truth labels batch.

  • epoch (int) – The current training epoch.

Returns:

A tuple of adversarial examples and their corresponding labels.

Return type:

Tuple[Tensor, Tensor]

robustML.advertrain.training.autoattack_training module

class robustML.advertrain.training.autoattack_training.AutoAttackTraining(model: Module, optimizer: Optimizer, loss_func: Callable, device: device, loss: str, epsilon: float)[source]

Bases: ClassicalTraining

Extends ClassicalTraining to include adversarial training using AutoPGD attacks.

epsilon

The maximum perturbation amount allowed for the APGD attack.

Type:

float

apgd_loss

The loss function to be used in the APGD attack.

Type:

str

apgd

Instance of APGDAttack for generating adversarial examples.

Type:

APGDAttack

preprocess_batch(x, y, epoch)[source]

Processes each batch by generating adversarial examples.

preprocess_batch(x: Tensor, y: Tensor, epoch: int) Tuple[Tensor, Tensor][source]

Processes each batch by generating adversarial examples.

Parameters:
  • x (torch.Tensor) – Input data (images).

  • y (torch.Tensor) – Corresponding labels.

  • epoch (int) – The current epoch number.

Returns:

A tuple containing the adversarial examples and their corresponding labels.

Return type:

Tuple[torch.Tensor, torch.Tensor]

robustML.advertrain.training.classical_training module

class robustML.advertrain.training.classical_training.ClassicalTraining(model: Module, optimizer: Optimizer, loss_func, device: device)[source]

Bases: object

A class representing the classical training process for a PyTorch model.

model

The PyTorch model to be trained.

Type:

Module

optimizer

The optimizer used for training.

Type:

Optimizer

loss_func

The loss function used for training.

device

The device on which to train the model.

Type:

torch.device

metrics

An instance of Metrics to track training performance.

Type:

Metrics

fit(epochs: int, train_dataloader: DataLoader, val_dataloader: DataLoader, patience: int, checkpoint: str) Dict[str, Any][source]

Train and validate the model over a given number of epochs, implementing early stopping.

Parameters:
  • epochs (int) – The total number of epochs to train.

  • train_dataloader (DataLoader) – The DataLoader for the training data.

  • val_dataloader (DataLoader) – The DataLoader for the validation data.

  • patience (int) – The number of epochs to wait for improvement before stopping early.

  • checkpoint (str) – Path to save the model checkpoints.

Returns:

A dictionary containing training and validation metrics.

Return type:

Dict[str, Any]

preprocess_batch(x: Tensor, y: Tensor, epoch: int) Tuple[Tensor, Tensor][source]

Preprocess a batch of data and labels before training or validation.

Parameters:
  • x (torch.Tensor) – Input data batch.

  • y (torch.Tensor) – Corresponding labels batch.

  • epoch (int) – The current epoch number.

Returns:

The preprocessed data and labels.

Return type:

Tuple[torch.Tensor, torch.Tensor]

train_batch(x: Tensor, y: Tensor, epoch: int) Tuple[float, int][source]

Process and train a single batch of data.

Parameters:
  • x (torch.Tensor) – Input data batch.

  • y (torch.Tensor) – Corresponding labels batch.

  • epoch (int) – The current epoch number.

Returns:

The training loss for the batch and the batch size.

Return type:

Tuple[float, int]

val_batch(x: Tensor, y: Tensor, epoch: int) Tuple[float, int][source]

Validate a single batch of data.

Parameters:
  • x (torch.Tensor) – Input data batch.

  • y (torch.Tensor) – Corresponding labels batch.

  • epoch (int) – The current epoch number.

Returns:

The validation loss for the batch and the batch size.

Return type:

Tuple[float, int]

robustML.advertrain.training.fire_training module

class robustML.advertrain.training.fire_training.FIRETraining(model: Module, optimizer: Optimizer, device: device, epsilon: float, beta: float, perturb_steps: int = 20)[source]

Bases: ClassicalTraining

train_batch(x: Tensor, y: Tensor, epoch: int) tuple[float, int][source]

Train the model for one batch of data.

Parameters:
  • x (torch.Tensor) – The input data.

  • y (torch.Tensor) – The labels corresponding to the input data.

  • epoch (int) – The current epoch number.

Returns:

A tuple containing the loss value and the batch size.

Return type:

tuple[float, int]

val_batch(x: Tensor, y: Tensor, epoch: int) tuple[float, int][source]

Validate the model for one batch of data.

Parameters:
  • x (torch.Tensor) – The input data.

  • y (torch.Tensor) – The labels corresponding to the input data.

  • epoch (int) – The current epoch number.

Returns:

A tuple containing the loss value and the batch size.

Return type:

tuple[float, int]

robustML.advertrain.training.trades_training module

class robustML.advertrain.training.trades_training.TRADESTraining(model: Module, optimizer: Optimizer, device: device, epsilon: float, beta: float, perturb_steps: int = 20)[source]

Bases: ClassicalTraining

train_batch(x: Tensor, y: Tensor, epoch: int) tuple[float, int][source]

Train the model on a batch of data.

Parameters:
  • x (torch.Tensor) – Input data.

  • y (torch.Tensor) – Target labels.

Returns:

Tuple containing the loss and the number of examples in the batch.

Return type:

tuple[float, int]

val_batch(x: Tensor, y: Tensor, epoch: int) tuple[float, int][source]

Validate the model on a batch of data.

Parameters:
  • x (torch.Tensor) – Input data.

  • y (torch.Tensor) – Target labels.

Returns:

Tuple containing the loss and the number of examples in the batch.

Return type:

tuple[float, int]

Module contents