PyTorch Helper Functions

../../../../../../../_images/MLPro-SL-pool-afct-pytorch_class_diagram.drawio.png

Ver. 3.0.7 (2023-07-14)

This a helper module for supervised learning models using PyTorch.

class mlpro.sl.pool.afct.pytorch.PyTorchIOElement(p_input: Tensor, p_output: Tensor)

Bases: BufferElement

This class provides a buffer element for PyTorch based SLNetwork.

Parameters:
  • p_input (Element) – Abscissa/input element object (type Element)

  • p_output (Element) – Setpoint ordinate/output element (type Element)

class mlpro.sl.pool.afct.pytorch.PyTorchBuffer(p_size: int = 1, p_test_data: float = 0.3, p_batch_size: int = 100, p_seed: int = 1)

Bases: Buffer, Dataset

This class provides buffer functionalities for PyTorch based SLNetwork and also using several built-in PyTorch functionalities.

Parameters:
  • p_size (int) – the buffer size. Default = 1.

  • p_test_data (float) – the proportion of testing data within the sampled data. Default = 0.3.

  • p_batch_size (int) – the batch size for a sample. Default = 100.

  • p_seed (int) – the seeding for randomizer in the buffer, optional. Default = 1.

add_element(p_elem: BufferElement)

This method has a functionality to add an element to the buffer.

Parameters:

p_elem (BufferElement) – an element of the buffer

get_internal_counter() int

This method has a functionality to get the number of elements being added to the buffer.

sampling()

This method has a functionality to sample from the buffer using built-in PyTorch functionalities.

Returns:

  • trainer (dict) – a dictionary that consists of sampled data for training, which are splitted to 2 keys such as input and output. The value of each key is a torch’s DataLoader of the sampled data.

  • tester (dict) – a dictionary that consists of sampled data for testing, which are splitted to 2 keys such as input and output. The value of each key is a torch’s DataLoader of the sampled data.

class mlpro.sl.pool.afct.pytorch.PyTorchHelperFunctions

Bases: object

PyTorch Helper Functions in MLPro-SL.

input_preproc(p_input: Element) Tensor

This method has a functionality to transform input data in the form of Element to torch.Tensor for pre-processing.

Parameters:

p_input (Element) – Input data in the form of Element.

Returns:

input – Input data in the form of torch.Tensor.

Return type:

torch.Tensor

output_preproc(p_output: Element) Tensor

This method has a functionality to transform output data in the form of Element to torch.Tensor for pre-processing.

Parameters:

p_output (Element) – Output data in the form of Element.

Returns:

output – Output data in the form of torch.Tensor.

Return type:

torch.Tensor

output_postproc(p_output: Tensor) list

This method has a functionality to transform output data in the form of torch.Tensor to a list for post-processing.

Parameters:

p_output (torch.Tensor) – Output data in the form of torch.Tensor.

Returns:

output – Output data in the form of list.

Return type:

list

_input_preproc(p_input: Tensor) Tensor

Additional process of input_preproc. This is optional. Please redefine if you need it.

Parameters:

p_input (torch.Tensor) – Input data in the form of torch.Tensor.

Returns:

input – Processed input data in the form of torch.Tensor.

Return type:

torch.Tensor

_output_preproc(p_output: Tensor) Tensor

Additional process of output_preproc. This is optional. Please redefine if you need it.

Parameters:

p_output (torch.Tensor) – Output data in the form of torch.Tensor.

Returns:

output – Processed output data in the form of torch.Tensor.

Return type:

torch.Tensor

_output_postproc(p_output: Tensor) Tensor

Additional process of output_postproc. This is optional. Please redefine if you need it.

Parameters:

p_output (torch.Tensor) – Output data in the form of torch.Tensor.

Returns:

output – Processed output data in the form of torch.Tensor.

Return type:

torch.Tensor

_default_weight_bias_init(module, weight_init, bias_init, gain=1)

Weight and bias initialization method.