stable_learning_control.algos.pytorch.common.helpers
Contains several Pytorch helper functions.
Functions
|
Retrieves the available computational device given a device type. |
|
Create a multi-layered perceptron using pytorch. |
|
Returns the total number of parameters of a pytorch module. |
|
Compares two models to see if the weights are equal. |
|
Rescale normalized data (i.e. between |
|
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 tocpu
.- Returns:
The Pytorch device object.
- Return type:
- stable_learning_control.algos.pytorch.common.helpers.mlp(sizes, activation, output_activation=nn.Identity)[source]
Create a multi-layered perceptron using pytorch.
- Parameters:
sizes (list) – The size of each of the layers.
activation (union[
torch.nn.modules.activation
,str
]) – The activation function used for the hidden layers.output_activation (union[
torch.nn.modules.activation
,str
], optional) – The activation function used for the output layers. Defaults totorch.nn.Identity
.
- Returns:
The multi-layered perceptron.
- Return type:
- 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:
- 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:
model_1 (torch.nn.Module) – The first Pytorch model.
model_2 (torch.nn.Module) – The second Pytorch model.
- 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:
- stable_learning_control.algos.pytorch.common.helpers.rescale(data, min_bound, max_bound)[source]
Rescale normalized data (i.e. between
-1
and1
) to a desired range.- Parameters:
data (Union[torch.Tensor, numpy.ndarray, list]) – Normalized input data.
min_bound (Union[numpy.ndarray, list]) – Array containing the minimum value of the desired range.
max_bound (Union[numpy.ndarray, list]) – Array containing the maximum value of the desired range.
- 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 toNone
(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: