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 batch size

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

for state, action, reward, task_id, 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,)

# for loading per timestep

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

for state, action in dataloader:
    assert state.shape == (32, 3, 16, 16)
    assert action.shape == (32, 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.8.tar.gz (10.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.8-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: memmap_replay_buffer-0.0.8.tar.gz
  • Upload date:
  • Size: 10.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.8.tar.gz
Algorithm Hash digest
SHA256 60abfd6ee7c02d7ff9f90015940235ae787115c9bc1dc7c25d710cc5ca40bb3d
MD5 0f6f60b669123e7e57cc798094a8d2ba
BLAKE2b-256 329ae55919c7581c5ba3c54b66924c94f9d80df6cb3de4b6b700d3f16dbcbcd7

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for memmap_replay_buffer-0.0.8-py3-none-any.whl
Algorithm Hash digest
SHA256 596752504d77f3839754d380599f7456f615b75846d4ea0717523983fa3dbfde
MD5 73ae3cf5e5545eb99d987f1434e84c78
BLAKE2b-256 0d022eea592181ef836de2a27dd44a05ca497856b2fb13e725e40a86e51bea98

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