chipiron.learningprocesses.nn_trainer package

Submodules

chipiron.learningprocesses.nn_trainer.factory module

Module to create and save neural network trainers and their parameters

class chipiron.learningprocesses.nn_trainer.factory.NNTrainerArgs(neural_network_architecture_args: ~chipiron.players.boardevaluators.neural_networks.neural_net_board_eval_args.NeuralNetArchitectureArgs = <factory>, nn_parameters_file_if_reusing_existing_one: str | ~os.PathLike[str] | None = None, specific_saving_folder: str | ~os.PathLike[str] | None = None, reuse_existing_model: bool = False, reuse_existing_trainer: bool = False, starting_lr: float = 0.1, momentum_op: float = 0.9, scheduler_step_size: int = 1, scheduler_gamma: float = 0.5, saving_intermediate_copy: bool = True, batch_size_train: int = 32, batch_size_test: int = 10, saving_interval: int = 1000, saving_intermediate_copy_interval: int = 10000, min_interval_lr_change: int = 1000000, min_lr: float = 0.001, epochs_number: int = 100)[source]

Bases: object

Arguments for the NNTrainer class.

reuse_existing_trainer

Whether to reuse an existing trainer.

Type:

bool

starting_lr

The starting learning rate.

Type:

float

momentum_op

The momentum value.

Type:

float

scheduler_step_size

The step size for the scheduler.

Type:

int

scheduler_gamma

The gamma value for the scheduler.

Type:

float

saving_intermediate_copy

Whether to save intermediate copies.

Type:

bool

batch_size_test: int = 10
batch_size_train: int = 32
epochs_number: int = 100
min_interval_lr_change: int = 1000000
min_lr: float = 0.001
momentum_op: float = 0.9
neural_network_architecture_args: NeuralNetArchitectureArgs
nn_parameters_file_if_reusing_existing_one: str | PathLike[str] | None = None
reuse_existing_model: bool = False
reuse_existing_trainer: bool = False
saving_intermediate_copy: bool = True
saving_intermediate_copy_interval: int = 10000
saving_interval: int = 1000
scheduler_gamma: float = 0.5
scheduler_step_size: int = 1
specific_saving_folder: str | PathLike[str] | None = None
starting_lr: float = 0.1
chipiron.learningprocesses.nn_trainer.factory.create_nn_trainer(args: NNTrainerArgs, nn: ChiNN, saving_folder: str | PathLike[str]) NNPytorchTrainer[source]

Creates an instance of NNPytorchTrainer based on the provided arguments and neural network.

Parameters:
  • args (NNTrainerArgs) – The arguments for the NNTrainer.

  • nn (ChiNN) – The neural network to be trained.

Returns:

An instance of NNPytorchTrainer.

Return type:

NNPytorchTrainer

chipiron.learningprocesses.nn_trainer.factory.get_folder_training_copies_path_from(folder_path: str | PathLike[str]) str[source]

Returns the path to the ‘training_copies’ folder within the given folder path.

Parameters:

folder_path (str) – The path to the folder.

Returns:

The path to the ‘training_copies’ folder.

Return type:

str

chipiron.learningprocesses.nn_trainer.factory.get_optimizer_file_path_from(folder_path: str | PathLike[str]) str[source]

Returns the file path for the optimizer file in the given folder path.

Parameters:

folder_path (str) – The path to the folder containing the optimizer file.

Returns:

The file path for the optimizer file.

Return type:

str

chipiron.learningprocesses.nn_trainer.factory.get_scheduler_file_path_from(folder_path: str | PathLike[str]) str[source]

Get the file path for the scheduler file in the given folder path.

Parameters:

folder_path (str) – The path of the folder containing the scheduler file.

Returns:

The file path of the scheduler file.

Return type:

str

chipiron.learningprocesses.nn_trainer.factory.safe_nn_architecture_save(nn_architecture_args: NeuralNetArchitectureArgs, nn_param_folder_name: str | PathLike[str]) None[source]
chipiron.learningprocesses.nn_trainer.factory.safe_nn_param_save(nn: ChiNN, nn_param_folder_name: str | PathLike[str], file_name: str | None = None, training_copy: bool = False) None[source]

Save the parameters of a neural network to a file.

Parameters:
  • nn (ChiNN) – The neural network to save.

  • training_copy (bool, optional) – Whether to save a training copy of the parameters. Defaults to False.

chipiron.learningprocesses.nn_trainer.factory.safe_nn_trainer_save(nn_trainer: NNPytorchTrainer, nn_folder_path: str | PathLike[str]) None[source]

Safely saves the optimizer and scheduler of the given NNPytorchTrainer object to files.

Parameters:

nn_trainer (NNPytorchTrainer) – The NNPytorchTrainer object containing the optimizer and scheduler to be saved.

Returns:

None

chipiron.learningprocesses.nn_trainer.factory.serialize_for_yaml(obj: Any) Any[source]

Recursively converts Enums and other non-serializable objects into basic types for safe YAML dumping.

chipiron.learningprocesses.nn_trainer.nn_trainer module

This module contains the definition of the NNPytorchTrainer class, which is responsible for training and testing a neural network model using PyTorch.

class chipiron.learningprocesses.nn_trainer.nn_trainer.NNPytorchTrainer(net: ChiNN, optimizer: Optimizer, scheduler: LRScheduler)[source]

Bases: object

A class that trains a neural network model using PyTorch.

Parameters:
  • net (ChiNN) – The neural network model to be trained.

  • optimizer (torch.optim.Optimizer) – The optimizer used for updating the model’s parameters.

  • scheduler (torch.optim.lr_scheduler.LRScheduler) – The learning rate scheduler.

net

The neural network model to be trained.

Type:

ChiNN

criterion

The loss function used for training.

Type:

torch.nn.L1Loss

optimizer

The optimizer used for updating the model’s parameters.

Type:

torch.optim.Optimizer

scheduler

The learning rate scheduler.

Type:

torch.optim.lr_scheduler.LRScheduler

train(input_layer, target_value)[source]

Trains the neural network model using the provided input and target values.

test(input_layer, target_value)[source]

Tests the neural network model using the provided input and target values.

train_next_boards(input_layer, next_input_layer)[source]

Trains the neural network model using the provided input and next input layers.

compute_test_error_on_dataset(data_test: DataLoader[tuple[torch.Tensor, torch.Tensor]]) float[source]

Computes the test error of the neural network model.

This method iterates over a test dataset and calculates the average loss for a given number of iterations. The test error is then computed as the average loss divided by the number of iterations.

Returns:

None

test(input_layer: Tensor, target_value: Tensor) Tensor[source]

Tests the neural network model using the provided input and target values.

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

  • target_value (torch.Tensor) – The target values.

Returns:

The loss value.

Return type:

torch.Tensor

train(input_layer: Tensor, target_value: Tensor) Tensor[source]

Trains the neural network model using the provided input and target values.

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

  • target_value (torch.Tensor) – The target values.

Returns:

The loss value.

Return type:

torch.Tensor

train_next_boards(input_layer: Tensor, next_input_layer: Tensor) None[source]

Trains the neural network model using the provided input and next input layers.

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

  • next_input_layer (torch.Tensor) – The next input data.

Returns:

None

chipiron.learningprocesses.nn_trainer.nn_trainer.check_model_device(model: ChiNN) str | device | int[source]
chipiron.learningprocesses.nn_trainer.nn_trainer.compute_loss(net: ChiNN, criterion: Callable[[Tensor, Tensor], Tensor], input_layer: Tensor, target_value: Tensor) Tensor[source]
chipiron.learningprocesses.nn_trainer.nn_trainer.compute_test_error_on_dataset(net: ChiNN, criterion: Callable[[Tensor, Tensor], Tensor], data_test: DataLoader[tuple[torch.Tensor, torch.Tensor]], number_of_tests: int = 100) float[source]

Module contents