stable_learning_control.algos.pytorch.common.helpers

Contains several Pytorch helper functions.

Functions

retrieve_device([device_type])

Retrieves the available computational device given a device type.

mlp(sizes, activation[, output_activation])

Create a multi-layered perceptron using pytorch.

count_vars(module)

Returns the total number of parameters of a pytorch module.

compare_models(model_1, model_2)

Compares two models to see if the weights are equal.

rescale(data, min_bound, max_bound)

Rescale normalized data (i.e. between -1 and 1) to a desired range.

np_to_torch(input_object[, dtype, device])

Converts all numpy arrays in a python object to Torch Tensors.

Module Contents

stable_learning_control.algos.pytorch.common.helpers.retrieve_device(device_type='cpu')[source]

Retrieves the available computational device given a device type.

Parameters:

device_type (str) – The device type (options: cpu, gpu, gpu:0, gpu:1, etc.). Defaults to cpu.

Returns:

The Pytorch device object.

Return type:

torch.device

stable_learning_control.algos.pytorch.common.helpers.mlp(sizes, activation, output_activation=nn.Identity)[source]

Create a multi-layered perceptron using pytorch.

Parameters:
Returns:

The multi-layered perceptron.

Return type:

torch.nn.Sequential

stable_learning_control.algos.pytorch.common.helpers.count_vars(module)[source]

Returns the total number of parameters of a pytorch module.

Parameters:

module (torch.nn.Module) – The module.

Returns:

The total number of parameters inside the module.

Return type:

numpy.int64

stable_learning_control.algos.pytorch.common.helpers.compare_models(model_1, model_2)[source]

Compares two models to see if the weights are equal.

Parameters:
Raises:

Exception – Raises Key error if the graph of the two models is different.

Returns:

Bool specifying whether the weights of two models are equal.

Return type:

bool

stable_learning_control.algos.pytorch.common.helpers.rescale(data, min_bound, max_bound)[source]

Rescale normalized data (i.e. between -1 and 1) to a desired range.

Parameters:
Returns:

Array which has it values scaled between

the min and max boundaries.

Return type:

Union[Torch.Tensor, numpy.ndarray]

stable_learning_control.algos.pytorch.common.helpers.np_to_torch(input_object, dtype=None, device=None)[source]

Converts all numpy arrays in a python object to Torch Tensors.

Parameters:
  • input_item (obj) – The python object.

  • dtype (type, optional) – The type you want to use for storing the data in the tensor. Defaults to None (i.e. torch default will be used).

  • device (str, optional) – The computational device on which the tensors should be stored. (options: cpu, gpu, gpu:0, gpu:1, etc.). Defaults to None (i.e. torch default device will be used).

Returns:

The output python object in which numpy arrays have been converted to

torch tensors.

Return type:

object