ReplayBuffer for Reinforcement Learning written by C++ and Cython
Project description
Overview
cpprb is a python (CPython) module providing replay buffer classes for reinforcement learning.
Major target users are researchers and library developers.
You can build your own reinforcement learning algorithms together with your favorite deep learning library (e.g. TensorFlow, PyTorch).
cpprb forcuses speed, flexibility, and memory efficiency.
By utilizing Cython, complicated calculations (e.g. segment tree for prioritized experience replay) are offloaded onto C++. (The name cpprb comes from "C++ Replay Buffer".)
In terms of API, initially cpprb referred to OpenAI Baselines' implementation. In the current version, cpprb has much more flexibility. Any NumPy compatible types of any numbers of values can be stored (as long as memory capacity is sufficient). For example, you can store the next action and the next next observation, too.
Installation
cpprb requires following softwares before installation.
- C++17 compiler (for installation from source)
- GCC (maybe 7.2 and newer)
- Visual Studio (2017 Enterprise is fine)
- Python 3
- pip
Cuurently, clang, which is a default Xcode C/C++ compiler at Apple macOS, cannot compile cpprb.
If you are macOS user, you need to install GCC and set environment values
of CC
and CXX
to g++
, or just use virtual environment (e.g. Docker).
Step by step installation is described here.
Additionally, here are user's good feedbacks for installation at macOS and Ubuntu. (Thanks!)
Install from PyPI (Recommended)
The following command installs cpprb together with other dependancies.
pip install cpprb
Depending on your environment, you might need sudo
or --user
flag
for installation.
On supported platflorms (Linux x86-64 and Windows amd64), binary packages are hosted on PyPI can be used, so that you don't need C++ compiler.
If you have trouble to install from binary, you can fall back to
source installation to passk --no-binary
option to the above pip command.
Currently, no other platforms, such as macOS, and 32bit or arm-architectured Linux and Windows, cannot install from binary, and need to compile by yourself. Please be patient, we will plan to support wider platforms in future.
Install from source code
First, download source code manually or clone the repository;
git clone https://gitlab.com/ymd_h/cpprb.git
Then you can install same way;
cd cpprb
pip install .
For this installation, you need to convert extended Python (.pyx) to C++ (.cpp) during installation, it takes longer time than installation from PyPI.
Usage
Here is a simple example for storing standard environment (aka. "obs", "act", "rew", "nextobs", and "done").
from cpprb import ReplayBuffer
buffer_size = 256
obs_shape = 3
act_dim = 1
rb = ReplayBuffer(buffer_size,
env_dict ={"obs": {"shape": obs_shape},
"act": {"shape": act_dim},
"rew": {},
"next_obs": {"shape": obs_shape},
"done": {}})
obs = np.ones(shape=(obs_shape))
act = np.ones(shape=(act_dim))
rew = 0
next_obs = np.ones(shape=(obs_shape))
done = 0
for i in range(500):
rb.add(obs=obs,act=act,rew=rew,next_obs=next_obs,done=done)
if done:
# Together with resetting environment, call ReplayBuffer.on_episode_end()
rb.on_episode_end()
batch_size = 32
sample = rb.sample(batch_size)
# sample is a dictionary whose keys are 'obs', 'act', 'rew', 'next_obs', and 'done'
Flexible environment values are defined by env_dict
when buffer creation.
Since stored values have flexible name, you have to pass to
ReplayBuffer.add
member by keyword.
Features
cpprb provides buffer classes for building following algorithms.
Algorithms | cpprb class | Paper |
---|---|---|
Experience Replay | `ReplayBuffer` | [L. J. Lin](https://link.springer.com/article/10.1007/BF00992699) |
Prioritized Experience Replay | `PrioritizedReplayBuffer` | [T. Schaul et. al.](https://arxiv.org/abs/1511.05952) |
Multi-step Learning | `ReplayBuffer`, `PrioritizedReplayBuffer` |
cpprb features and its usage are described at following pages:
- Flexible Environment
- Multi-step add
- Prioritized Experience Replay
- Nstep Experience Replay
- Memory Compression
Contributing to cpprb
Any contribution are very welcome!
Making Community Larger
Bigger commumity makes development more active and improve cpprb.
- Star this repository (and/or GitHub Mirror)
- Publish your code using cpprb
- Share this repository to your friend and/or followers.
Report Issue
When you have any problems or requests, you can check issues on GitLab.com. If you still cannot find any information, you can open your own issue.
Merge Request (Pull Request)
cpprb follows local rules:
- Branch Name
- "HotFix***" for bug fix
- "Feature***" for new feature implementation
- docstring
- Must for external API
- Numpy Style
- Unit Test
- Put test code under "test/" directory
- Can test by
python -m unittest <Your Test Code>
command - Continuous Integration on GitLab CI configured by
.gitlab-ci.yaml
- Open an issue and associate it to Merge Request
Step by step instruction for beginners is described at here.
Links
cpprb sites
cpprb users' repositories
- keiohta/TF2RL: TensorFlow2.0 Reinforcement Learning
Lisence
cpprb is available under MIT lisence.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for cpprb-9.0.3-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b63cb61d821366185351ec9eb43ff3ca507e0d9a81b60eef8e0dce1bfb110e6 |
|
MD5 | 66a6673a3e59591129d690733ce118cc |
|
BLAKE2b-256 | 2f797fed5c94adc0b04d020926d9d3f377e5150b8dd22bc19f8e618fb702d8f3 |
Hashes for cpprb-9.0.3-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8337e8d5754f15de6c798f864ae88d22e6e76e0bbf1ebd105c696c9bddb49c88 |
|
MD5 | bddd2c596927e5570089c05e334370af |
|
BLAKE2b-256 | d428e3420ab91157eb2b7abfa675b0cfb15674c5f918975560ad94b6435f8a84 |
Hashes for cpprb-9.0.3-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0830a37091b440f2f173172ee120d44432f5f53258f5323dda1c1ec6537eef06 |
|
MD5 | 705619ac086bc30d1df7d5addf5b9469 |
|
BLAKE2b-256 | 50b3761ad9a3900c7ee5b07626902cbff197d8ba4a6d82908d3d04bdf9a467e8 |
Hashes for cpprb-9.0.3-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | be22fc00ccefd62f4339fe9375e56f88887bb66e69e5001359248eece754f26a |
|
MD5 | 7ce3a0fcd383bc783d3194b9b77f8ca0 |
|
BLAKE2b-256 | ed226047fe5d4baa866408063358020d627a9f8e351c91710b0021da1eaf8e6a |
Hashes for cpprb-9.0.3-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d266e5296c7f448a7ce78957a13ac5427fe5c17d5f1513d3914a159063bee531 |
|
MD5 | 6fdd20adf0a302ad40a59f9cff07e0ff |
|
BLAKE2b-256 | 9cf0becbc52b5f8fea3f0b79fad0d53022ee743e42cfe4df1a98653e20d8aebb |
Hashes for cpprb-9.0.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3ad7287b886ddcaa60e12ff3e7dbab13e98a25e71affbf88d3b418285fe2fba |
|
MD5 | 9596cbabc52c7b71f4db380517385b66 |
|
BLAKE2b-256 | 816985c534da3771ef394c1a971ec85d410120ff1798e1f1258c9db7890f49ee |
Hashes for cpprb-9.0.3-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a474e9bc50a83622c04e36b1a392f8b2c26bb4d633c38dffb24961253038765f |
|
MD5 | f3d0b88cda540e1002275877b797ba3e |
|
BLAKE2b-256 | 5a5570a5af41801aa1e26424f33d94e1af7a6ee5b7b6c7e987935145c50dafc1 |
Hashes for cpprb-9.0.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc099cbbdd97b425b9d202e994fc89d3748308401c6f6eecb4ded087d26e03c4 |
|
MD5 | 639eeae588dd6f9e7e4970146de07afd |
|
BLAKE2b-256 | f25adf3b57b2932c988875a74d3679c64103f23ba68839ec6877b0453ec34ff5 |