File post-processing

Exported memories

This library provides an implementation for quickly loading exported memory files to inspect their contents in future post-processing steps. See the section Library utilities (skrl.utils module) for a real use case

Basic usage

 1from skrl.utils import postprocessing
 2
 3
 4# assuming there is a directory called "memories" with Torch files in it
 5memory_iterator = postprocessing.MemoryFileIterator("memories/*.pt")
 6for filename, data in memory_iterator:
 7    filename    # str: basename of the current file
 8    data    # dict: keys are the names of the memory tensors in the file.
 9            # Tensor shapes are (memory size, number of envs, specific content size)
10
11    # example of simple usage:
12    # print the filenames of all memories and their tensor shapes
13    print("\nfilename:", filename)
14    print("  |-- states:", data['states'].shape)
15    print("  |-- actions:", data['actions'].shape)
16    print("  |-- rewards:", data['rewards'].shape)
17    print("  |-- next_states:", data['next_states'].shape)
18    print("  |-- dones:", data['dones'].shape)

API

class skrl.utils.postprocessing.MemoryFileIterator(pathname: str)

Bases: object

__init__(pathname: str) None

Python iterator for loading data from exported memories

The iterator will load the next memory file in the list of path names. The output of the iterator is a tuple of the filename and the memory data where the memory data is a dictionary of torch.Tensor (PyTorch), numpy.ndarray (NumPy) or lists (CSV) depending on the format and the keys of the dictionary are the names of the variables

Supported formats:

  • PyTorch (pt)

  • NumPy (npz)

  • Comma-separated values (csv)

Expected output shapes:

  • PyTorch: (memory_size, num_envs, data_size)

  • NumPy: (memory_size, num_envs, data_size)

  • Comma-separated values: (memory_size * num_envs, data_size)

Parameters

pathname (str) – String containing a path specification for the exported memories. Python glob method is used to find all files matching the path specification

__iter__() skrl.utils.postprocessing.MemoryFileIterator

Return self to make iterable

__next__() Tuple[str, dict]

Return next batch

Returns

Tuple of file name and data

Return type

tuple

_format_csv() Tuple[str, dict]

Load CSV file from file

Returns

Tuple of file name and data

Return type

tuple

_format_numpy() Tuple[str, dict]

Load numpy array from file

Returns

Tuple of file name and data

Return type

tuple

_format_torch() Tuple[str, dict]

Load PyTorch tensor from file

Returns

Tuple of file name and data

Return type

tuple

Tensorboard files

This library provides an implementation for quickly loading Tensorboard files to inspect their contents in future post-processing steps. See the section Library utilities (skrl.utils module) for a real use case

Requirements

This utility requires the TensorFlow package to be installed to load and parse Tensorboard files:

pip install tensorflow

Basic usage

 1from skrl.utils import postprocessing
 2
 3
 4# assuming there is a directory called "runs" with experiments and Tensorboard files in it
 5tensorboard_iterator = postprocessing.TensorboardFileIterator("runs/*/events.out.tfevents.*", \
 6    tags=["Reward / Total reward (mean)"])
 7for dirname, data in tensorboard_iterator:
 8    dirname    # str: path of the directory (experiment name) containing the Tensorboard file
 9    data    # dict: keys are the tags, values are lists of [step, value] pairs
10
11    # example of simple usage:
12    # print the directory name and the value length for the "Reward / Total reward (mean)" tag
13    print("\ndirname:", dirname)
14    for tag, values in data.items():
15        print("  |-- tag:", tag)
16        print("  |   |-- value length:", len(values))

API

class skrl.utils.postprocessing.TensorboardFileIterator(pathname: str, tags: Union[str, List[str]])

Bases: object

__init__(pathname: str, tags: Union[str, List[str]]) None

Python iterator for loading data from Tensorboard files

The iterator will load the next Tensorboard file in the list of path names. The iterator’s output is a tuple of the directory name and the Tensorboard variables selected by the tags. The Tensorboard data is returned as a dictionary with the tag as the key and a list of steps and values as the value

Parameters
  • pathname (str) –

    String containing a path specification for the Tensorboard files. Python glob method is used to find all files matching the path specification

  • tags (str or list of str) – String or list of strings containing the tags of the variables to load

__iter__() skrl.utils.postprocessing.TensorboardFileIterator

Return self to make iterable

__next__() Tuple[str, dict]

Return next batch

Returns

Tuple of directory name and data

Return type

tuple