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', (3, 16, 16), 0.),    # 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(3, 16, 16),
                action = torch.randint(0, 4, (2,)).numpy(),
                reward = 1.0
            )

# rehydrate from disk

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

# train 2 episodes at a time

dataloader = buffer.dataloader(
    batch_size = 2,
    return_mask = True,
    to_named_tuple = ('state', 'action', 'reward', 'task_id', '_mask', '_lens')
)

for state, action, reward, task_id, mask, lens in dataloader:
    assert state.shape   == (2, 100, 3, 16, 16)
    assert action.shape  == (2, 100, 2)
    assert reward.shape  == (2, 100)
    assert task_id.shape == (2,)

    assert lens.shape    == (2,)
    assert mask.shape    == (2, 100)

# for loading per timestep

dataloader = buffer.dataloader(
    batch_size = 8,
    filter_meta = dict(
        task_id = 1
    ),
    to_named_tuple = ('state', 'action', 'task_id'),
    timestep_level = True,
    drop_last = True
)

for state, action, task_id in dataloader:
    assert state.shape   == (8, 3, 16, 16)
    assert action.shape  == (8, 2)
    assert task_id.shape == (8,)

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.29.tar.gz (19.8 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.29-py3-none-any.whl (27.0 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for memmap_replay_buffer-0.0.29.tar.gz
Algorithm Hash digest
SHA256 d5847af216a512a7dfbce0e47fc54fdc180daaf3c0ef55945b8f263179ce0394
MD5 22bc40f4eec3d3ea920c482f48614930
BLAKE2b-256 c26f54e69379f668cb6ec63c8e2b84616cb38c30200df48b9ad42d967a4fd6cd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for memmap_replay_buffer-0.0.29-py3-none-any.whl
Algorithm Hash digest
SHA256 28f139df4fc5e1928d9d129000e9907cdca7bcf8df7115b1b4a0678ac53babbe
MD5 ace0abc071118ba13c2a92fe546fdd39
BLAKE2b-256 66b92896d584d4e369b79bbcf2eee469de01a25ebfa4797ce4645975a706e3ab

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