File post-processing

Utilities for processing files generated during training/evaluation.



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 example usage.


Usage

from skrl.utils import postprocessing


# assuming there is a directory called "memories" with Torch files in it
memory_iterator = postprocessing.MemoryFileIterator("memories/*.pt")
for filename, data in memory_iterator:
    filename: str  # basename of the current file
    data: dict  # keys: tensor names, values: tensors with shape (memory_size, num_envs, data_size)

    # example of simple usage:
    # print the filenames of all memories and their tensor shapes
    print("\nfilename:", filename)
    print("  |-- observations:", data["observations"].shape)
    print("  |-- actions:", data["actions"].shape)
    print("  |-- rewards:", data["rewards"].shape)
    print("  |-- next_observations:", data["next_observations"].shape)
    print("  |-- terminated:", data["terminated"].shape)
    print("  |-- truncated:", data["truncated"].shape)

API

class skrl.utils.postprocessing.MemoryFileIterator(pathname: str)[source]

Bases: object

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 – String containing a path specification for the exported memories. Python glob function is used to find all files matching the path specification.

__iter__() MemoryFileIterator[source]

Return self to make iterable.

__next__() tuple[str, dict[str, Any]][source]

Return next batch.

Returns:

Tuple of file name and data.

Methods:

_format_csv()

Load CSV file from file.

_format_numpy()

Load NumPy array from file.

_format_torch()

Load PyTorch tensor from file.

_format_csv() tuple[str, dict[str, Any]][source]

Load CSV file from file.

Returns:

Tuple of file name and data.

_format_numpy() tuple[str, dict[str, Any]][source]

Load NumPy array from file.

Returns:

Tuple of file name and data.

_format_torch() tuple[str, dict[str, Any]][source]

Load PyTorch tensor from file.

Returns:

Tuple of file name and data.


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 example usage.


Requirements

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

pip install tensorflow

Usage

from skrl.utils import postprocessing


# assuming there is a directory called "runs" with experiments and TensorBoard files in it
tensorboard_iterator = postprocessing.TensorboardFileIterator(
    "runs/*/events.out.tfevents.*", tags=["Reward / Total reward (mean)"]
)
for dirname, data in tensorboard_iterator:
    dirname: str  # path of the directory (experiment name) containing the TensorBoard file
    data: dict  # keys: tags, values: lists of [step, value] pairs

    # example of simple usage:
    # print the directory name and the value length for the "Reward / Total reward (mean)" tag
    print("\ndirname:", dirname)
    for tag, values in data.items():
        print("  |-- tag:", tag)
        print("  |   |-- value length:", len(values))

API

class skrl.utils.postprocessing.TensorboardFileIterator(pathname: str, tags: str | list[str])[source]

Bases: object

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 – String containing a path specification for the TensorBoard files. Python glob function is used to find all files matching the path specification.

  • tags – String or list of strings containing the tags of the variables to load.

__iter__() TensorboardFileIterator[source]

Return self to make iterable.

__next__() tuple[str, dict[str, Any]][source]

Return next batch.

Returns:

Tuple of directory name and data.