RLDS (Reinforcement Learning Datasets)

repository·main·Indexed 19 days ago

https://github.com/google-research/rlds

An ecosystem for storing, retrieving, and manipulating episodic data for Reinforcement Learning and Sequential Decision Making. RLDS provides standardized formats and tools, including the RLDS Library, EnvLogger for synthetic datasets, and RLDS Creator for human-environment interactions, ensuring compatibility with TensorFlow Datasets (TFDS) for efficient training.

Tokens
14.5K
Snippets
42
Records
64
Agent score
66%

What's inside RLDS

  1. What is RLDS and its ecosystem?

    main

    RLDS (Reinforcement Learning Datasets) is an ecosystem of tools designed to store, retrieve, and manipulate episodic data for Sequential Decision Making tasks, such as Reinforcement Learning (RL), Offline RL, and Imitation Learning.

    Key components of the ecosystem include:

    • RLDS Library: For manipulating RLDS-compliant datasets.
    • EnvLogger: For creating synthetic datasets.
    • RLDS Creator: For creating datasets from human-environment interactions.
    • TFDS (TensorFlow Datasets): For accessing existing RL datasets and loading RLDS datasets.
  2. Why create a TFDS builder for existing RLDS datasets

    main

    Even if your data is already in TFDS format, you may want to implement a formal TFDS builder for two main reasons:

    • Reshuffling: To re-generate the data so that episodes are shuffled on disk. By default, episodes are stored in the order they were generated by Envlogger.
    • Sharing: To either add the dataset to the official TFDS catalog or to share it within your own repository. Users can still load data directly using tfds.builder_from_directory without a formal builder.
  3. Handle multiple readers for random episodes

    main

    When using multiple processes to read the same dataset (e.g., multiple actors in a single-learner scenario), you must ensure processes do not receive the same sequence of episodes. There are two primary strategies:

    1. Using TFDS Splits (Deterministic): Use the split API to assign a unique, disjoint set of episodes to each reader. This is the easiest method, but if a reader process dies, its assigned portion of the dataset will not be processed.
    2. Non-deterministic Reading: Set shuffle_files=True and tune ReadConfig options in tfds.load or builder.as_dataset. This allows the full dataset to be processed even if a reader dies, but some episodes may appear more than once.
  4. Understand the RLDS Dataset Format

    main

    An RLDS dataset is retrieved as a tf.data.Dataset of Episodes. Each episode is a dictionary containing a tf.data.Dataset of Steps and metadata.

    Episode Structure

    An Episode contains:

    • Steps: A tf.data.Dataset of step dictionaries.
    • Metadata: User-defined fields. Recommended optional fields include:
      • episode_id: Unique identifier for the episode.
      • agent_id: Unique identifier for the agent(s).
      • environment_config: Configuration used to generate the episode.
      • experiment_id: Identifier for the experiment.
      • invalid: A flag to signal incomplete or invalid episodes (e.g., due to machine preemption).

    Step Structure

    Each Step is a dictionary containing:

    • Mandatory Fields:
      • is_first: Boolean; true if this is the initial state of an episode.
      • is_last: Boolean; true if this is the last observation. When true, subsequent fields like action and reward are considered invalid.
    • Optional Fields (must be consistent across all steps in a dataset):
      • observation: The current observation.
      • action: The action taken.
      • reward: The return after applying the action.
      • is_terminal: Boolean; true if the step is a terminal state.
      • discount: The discount factor at this step.
      • extra metadata: Custom fields.

    Note on Terminal States: When is_terminal = True, the observation is the final state, making reward, discount, and action meaningless. If an episode ends with is_terminal = False, it has been truncated.

  5. Interleave steps across multiple episodes

    main

    If your algorithm operates on individual steps or n-step transitions rather than full episodes, you can interleave steps from multiple episodes to achieve randomization.

    One method is to use tf.data.Dataset.interleave to create N copies of the dataset. Each copy shuffles input partitions independently, ensuring consecutive steps come from unrelated episodes.

    Note: To avoid loading the same step N times, construct each copy using disjoint TFDS splits.

    def ds_loader():
      episode_dataset = tfds.load(...) 
      step_dataset = episode_dataset.flat_map(lambda x: x[rlds.STEPS])
      return step_dataset
    
    dataset = Dataset.range(1, N).interleave(ds_loader, cycle_length=..., block_length=...)
  6. Load RLDS datasets with TFDS

    main

    RLDS datasets are designed to be loaded via TensorFlow Datasets (TFDS). The method depends on how the dataset was created.

    Loading datasets created with Envlogger (using TFDS backend)

    If you have a local directory containing an Envlogger-generated dataset, use tfds.builder_from_directory:

    # From a single directory
    tfds.builder_from_directory('path').as_dataset(split='all')
    
    # From a list of directories
    tfds.builder_from_directories(paths).as_dataset(split='all')

    Loading datasets from the TFDS catalog

    For datasets already hosted in the TFDS catalog (like D4RL or RL Unplugged), use tfds.load:

    tfds.load('dataset_name').as_dataset()['train']
    # From a single directory
    tfds.builder_from_directory('path').as_dataset(split='all')
    
    # From a list of directories
    tfds.builder_from_directories(paths).as_dataset(split='all')
    
    # From the TFDS catalog
    tfds.load('dataset_name').as_dataset()['train']
  7. Explore RLDS performance best practices

    main

    If you are experiencing bottlenecks or want to optimize your data loading pipeline, refer to the Performance Best Practices Colab. It provides guidance on how to use RLDS efficiently.

    https://colab.research.google.com/github/google-research/rlds/blob/main/rlds/examples/rlds_performance.ipynb
  8. Add a non-RLDS/TFDS dataset to TFDS

    main

    If your data is in a custom format that is neither RLDS nor TFDS compatible, follow these two steps to integrate it into TFDS:

    1. Implement a Python dataset builder class: Define the dataset specifications (e.g., shapes of observations, actions, etc.) and the logic for reading your raw data files.
      • Note: To ensure compatibility with RLDS pipelines, your implementation must provide the same structure and keys as an RLDS dataset.
    2. Run the download_and_prepare pipeline: This converts your raw data into the TFDS intermediate format.

    Alternatively, you can rewrite raw data into an RLDS/TFDS compatible format before adding it to TFDS using Envlogger or EpisodeWriter. For EpisodeWriter, use the ConfigGenerator tool to create your DatasetConfig.