Skip to main content

Simple Replay Buffer for RL

Project description

memmap-replay-buffer

An easy-to-use numpy memmap replay buffer for RL and other sequence-based learning tasks.

Install

$ pip install memmap-replay-buffer

Usage

import torch
from memmap_replay_buffer import ReplayBuffer

# initialize buffer

buffer = ReplayBuffer(
    './replay_data',
    max_episodes = 1000,
    max_timesteps = 500,
    fields = dict(
        state = ('float', (8,), 0.5),   # type, shape, and optional default value
        action = ('int', (2,)),
        reward = 'float'                # default shape is ()
    ),
    meta_fields = dict(
        task_id = 'int'
    ),
    circular = True,
    overwrite = True
)

# store 4 episodes

for _ in range(4):
    with buffer.one_episode(task_id = 1):
        for _ in range(100):
            buffer.store(
                state = torch.randn(8).numpy(),
                action = torch.randint(0, 4, (2,)),
                reward = 1.0
            )

# rehydrate from disk

buffer_rehydrated = ReplayBuffer.from_config('./replay_data')
assert buffer_rehydrated.num_episodes == 4

# learn 2 episodes at a time

dataloader = buffer.dataloader(batch_size = 2)

for batch in dataloader:
    state = batch['state']    # (2, 100, 8)
    action = batch['action']  # (2, 100)
    reward = batch['reward']  # (2, 100)
    lens = batch['_lens']     # (2,)

    assert state.shape  == (2, 100, 8)
    assert action.shape == (2, 100, 2)
    assert reward.shape == (2, 100)
    assert lens.shape   == (2,)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

memmap_replay_buffer-0.0.6.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

memmap_replay_buffer-0.0.6-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file memmap_replay_buffer-0.0.6.tar.gz.

File metadata

  • Download URL: memmap_replay_buffer-0.0.6.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for memmap_replay_buffer-0.0.6.tar.gz
Algorithm Hash digest
SHA256 f42358ed0ba985c351cbb92488c5f2f09b0c28520ed079d6d71530c89442ea4b
MD5 efb16149f024e444309d44954f9d5055
BLAKE2b-256 49bac3ec21b32f7bb84d060520922ab42b0374af59b3a85601cacd6adb7e42da

See more details on using hashes here.

File details

Details for the file memmap_replay_buffer-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for memmap_replay_buffer-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 67b4e792096c080e4d9cf82994be18c5223c901f79985bef0e08cc27ca5544dd
MD5 7546d79ee973d1b39ce3f6e308806d69
BLAKE2b-256 bcf95bcf31c983da292326c892cc7fdab8ef92d823782aaccc04d924c7dddc1c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page