rofunc.learning.RofuncRL.utils.memory#
1. Module Contents#
1.1. Classes#
1.2. API#
- class rofunc.learning.RofuncRL.utils.memory.Memory(memory_size: int, num_envs: int = 1, device: Optional[Union[str, torch.device]] = None, export: bool = False, export_format: str = 'pt', export_directory: str = '')[source]#
Initialization
Base class representing a memory with circular buffers
Buffers are torch tensors with shape (memory size, number of environments, data size). Circular buffers are implemented with two integers: a memory index and an environment index
- Parameters:
memory_size (int) – Maximum number of elements in the first dimension of each internal storage
num_envs (int, optional) – Number of parallel environments (default: 1)
device (str or torch.device, optional) – Device on which a torch tensor is or will be allocated (default:
None). If None, the device will be either"cuda:0"if available or"cpu"export (bool, optional) – Export the memory to a file (default: False). If True, the memory will be exported when the memory is filled
export_format (str, optional) – Export format (default: “pt”). Supported formats: torch (pt), numpy (np), comma separated values (csv)
export_directory (str, optional) – Directory where the memory will be exported (default: “”). If empty, the agent’s experiment directory will be used
- Raises:
ValueError – The export format is not supported
Share the tensors between processes
- get_tensor_names() Tuple[str][source]#
Get the name of the internal tensors in alphabetical order
- Returns:
Tensor names without internal prefix (_tensor_)
- Return type:
tuple of strings
- get_tensor_by_name(name: str, keepdim: bool = True) torch.Tensor[source]#
Get a tensor by its name
- Parameters:
name (str) – Name of the tensor to retrieve
keepdim (bool, optional) – Keep the tensor’s shape (memory size, number of environments, size) (default: True) If False, the returned tensor will have a shape of (memory size * number of environments, size)
- Raises:
KeyError – The tensor does not exist
- Returns:
Tensor
- Return type:
torch.Tensor
- set_tensor_by_name(name: str, tensor: Union[torch.Tensor, rofunc.learning.RofuncRL.processors.standard_scaler.RunningStandardScaler]) None[source]#
Set a tensor by its name
- Parameters:
name (str) – Name of the tensor to set
tensor (torch.Tensor) – Tensor to set
- Raises:
KeyError – The tensor does not exist
- create_tensor(name: str, size: Union[int, Tuple[int], gym.Space, gymnasium.Space], dtype: Optional[torch.dtype] = None, keep_dimensions: bool = False) bool[source]#
Create a new internal tensor in memory The tensor will have a 3-components shape (memory size, number of environments, size). The internal representation will use _tensor_<name> as the name of the class property :param name: Tensor name (the name has to follow the python PEP 8 style) :param size: Number of elements in the last dimension (effective data size).
The product of the elements will be computed for sequences or gym/gymnasium spaces
- Parameters:
dtype – Data type (torch.dtype). If None, the global default torch data type will be used (default)
keep_dimensions – Whether or not to keep the dimensions defined through the size parameter (default: False)
- reset() None[source]#
Reset the memory by cleaning internal indexes and flags
Old data will be retained until overwritten, but access through the available methods will not be guaranteed
Default values of the internal indexes and flags
filled: False
env_index: 0
memory_index: 0
- add_samples(**tensors: torch.Tensor) None[source]#
Record samples in memory
Samples should be a tensor with 2-components shape (number of environments, data size). All tensors must be of the same shape
According to the number of environments, the following classification is made:
one environment: Store a single sample (tensors with one dimension) and increment the environment index (second index) by one
number of environments less than num_envs: Store the samples and increment the environment index (second index) by the number of the environments
number of environments equals num_envs: Store the samples and increment the memory index (first index) by one
- Parameters:
tensors (dict) – Sampled data as key-value arguments where the keys are the names of the tensors to be modified. Non-existing tensors will be skipped
- Raises:
ValueError – No tensors were provided or the tensors have incompatible shapes
- abstract sample(names: Union[Tuple[str], List[str]], batch_size: int, mini_batches: int = 1, sequence_length: int = 1) List[List[torch.Tensor]][source]#
Data sampling method to be implemented by the inheriting classes
- Parameters:
names (tuple or list of strings) – Tensors names from which to obtain the samples
batch_size (int) – Number of element to sample
mini_batches (int, optional) – Number of mini-batches to sample (default: 1)
sequence_length (int, optional) – Length of each sequence (default: 1)
- Raises:
NotImplementedError – The method has not been implemented
- Returns:
Sampled data from tensors sorted according to their position in the list of names. The sampled tensors will have the following shape: (batch size, data size)
- Return type:
list of torch.Tensor list
- sample_by_index(names: Tuple[str], indexes: Union[tuple, numpy.ndarray, torch.Tensor], mini_batches: int = 1) List[List[torch.Tensor]][source]#
Sample data from memory according to their indexes
- Parameters:
names (tuple or list of strings) – Tensors names from which to obtain the samples
indexes (tuple or list, numpy.ndarray or torch.Tensor) – Indexes used for sampling
mini_batches (int, optional) – Number of mini-batches to sample (default: 1)
- Returns:
Sampled data from tensors sorted according to their position in the list of names. The sampled tensors will have the following shape: (number of indexes, data size)
- Return type:
list of torch.Tensor list
- sample_all(names: List[str], mini_batches: int = 1, sequence_length: int = 1) List[List[torch.Tensor]][source]#
Sample all data from memory
- Parameters:
names (tuple or list of strings) – Tensors names from which to obtain the samples
mini_batches (int, optional) – Number of mini-batches to sample (default: 1)
sequence_length (int, optional) – Length of each sequence (default: 1)
- Returns:
Sampled data from memory. The sampled tensors will have the following shape: (memory size * number of environments, data size)
- Return type:
list of torch.Tensor list
- get_sampling_indexes() Union[tuple, numpy.ndarray, torch.Tensor][source]#
Get the last indexes used for sampling
- Returns:
Last sampling indexes
- Return type:
tuple or list, numpy.ndarray or torch.Tensor
- save(directory: str = '', format: str = 'pt') None[source]#
Save the memory to a file
Supported formats:
PyTorch (pt)
NumPy (npz)
Comma-separated values (csv)
- Parameters:
directory (str) – Path to the folder where the memory will be saved. If not provided, the directory defined in the constructor will be used
format (str, optional) – Format of the file where the memory will be saved (default: “pt”)
- Raises:
ValueError – If the format is not supported
- class rofunc.learning.RofuncRL.utils.memory.RandomMemory(memory_size: int, num_envs: int = 1, device: Optional[Union[str, torch.device]] = None, export: bool = False, export_format: str = 'pt', export_directory: str = '', replacement=True)[source]#
Bases:
rofunc.learning.RofuncRL.utils.memory.MemoryInitialization
Random sampling memory
Sample a batch from memory randomly
- Parameters:
memory_size (int) – Maximum number of elements in the first dimension of each internal storage
num_envs (int, optional) – Number of parallel environments (default: 1)
device (str or torch.device, optional) – Device on which a torch tensor is or will be allocated (default:
None). If None, the device will be either"cuda:0"if available or"cpu"export (bool, optional) – Export the memory to a file (default: False). If True, the memory will be exported when the memory is filled
export_format (str, optional) – Export format (default: “pt”). Supported formats: torch (pt), numpy (np), comma separated values (csv)
export_directory (str, optional) – Directory where the memory will be exported (default: “”). If empty, the agent’s experiment directory will be used
replacement (bool, optional) – Flag to indicate whether the sample is with or without replacement (default: True). Replacement implies that a value can be selected multiple times (the batch size is always guaranteed). Sampling without replacement will return a batch of maximum memory size if the memory size is less than the requested batch size
- Raises:
ValueError – The export format is not supported
- sample(names: Tuple[str], batch_size: int, mini_batches: int = 1, sequence_length: int = 1) List[List[torch.Tensor]][source]#
Sample a batch from memory randomly
- Parameters:
names (tuple or list of strings) – Tensors names from which to obtain the samples
batch_size (int) – Number of element to sample
mini_batches (int, optional) – Number of mini-batches to sample (default: 1)
sequence_length (int, optional) – Length of each sequence (default: 1)
- Returns:
Sampled data from tensors sorted according to their position in the list of names. The sampled tensors will have the following shape: (batch size, data size)
- Return type:
list of torch.Tensor list