Skip to main content

drlab

drlab is a small deep reinforcement learning library for training neural network agents in Gymnasium environments. Its purpose is to provide simple, research-friendly building blocks for reinforcement learning experiments, especially when the environment or training setup is still being explored.

The library is built around PyTorch models, Gymnasium environments, reusable controllers, replay buffers, runners, learners, and lightweight experiment loops. It is intended for research code and prototypes, not as a large production RL framework.

Installation

The intended development setup uses uv. From the repository root:

uv sync --extra experiments --extra dev

Run commands through the managed environment:

uv run python -m pytest
uv run python examples/dqn_cartpole.py

You can also activate the local virtual environment created by uv:

source .venv/bin/activate
python -m pytest

If you prefer a normal venv and pip workflow:

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -e .

For experiment and development dependencies:

python -m pip install -e ".[experiments,dev]"

drlab depends on PyTorch, NumPy, Gymnasium, tqdm, and TensorBoard. If you need a specific CUDA build of PyTorch, install the matching PyTorch wheel before installing the package dependencies.

Library Overview

Most public classes can be imported directly from drlab:

from drlab import DQNConfig, DQNLearner, OffPolicyExperiment

Off-Policy Learning

Off-policy learners train from transition batches that may come from a replay buffer instead of only the most recent rollout. OffPolicyExperiment collects environment transitions with a Runner, stores them in a ReplayBuffer, samples minibatches, and calls the learner update.

Implemented off-policy learners:

  • DQNLearner: value-based learning for discrete action spaces. It supports replay-buffer training, target networks, Double DQN, hard or soft target updates, gradient clipping, and custom regularizers.
  • SACLearner: Soft Actor-Critic style learning for continuous action spaces. It uses an actor, two critics, target critics, entropy tuning, and replay buffer training.

On-Policy Learning

On-policy learners train from the data collected by the current policy. OnPolicyExperiment collects rollouts, computes returns when needed, and trains the learner directly on the fresh batch.

Implemented on-policy learners:

  • ReinforceLearner: policy-gradient learning from discounted returns.
  • ActorCriticLearner: policy and value learning from shared model outputs.
  • PPOLearner: clipped policy optimization over rollout batches.

Supporting Components

Controllers turn model outputs into environment actions:

  • GreedyController, EpsilonGreedyController, and StochasticController support discrete action spaces.
  • GaussianController supports continuous action spaces by sampling bounded actions from model-predicted Gaussian parameters.

Runner interacts with Gymnasium environments and returns transition batches, episode returns, episode lengths, and optionally the last completed episode.

ReplayBuffer stores transitions for off-policy learning. It supports both discrete and continuous action spaces through the configured action shape and action type. Call reset() to remove all stored transitions and reuse the buffer.

TransitionBatch is the tensor container passed between runners, replay buffers, experiments, and learners.

All learners provide reset_optimizers() to clear optimizer state without changing gradients, optimizer settings, or model parameters.

Simple Usage

This example trains a DQN agent on CartPole-v1:

import gymnasium as gym
import torch as th

from drlab import (
    DQNConfig,
    DQNLearner,
    EpsilonGreedyController,
    GreedyController,
    OffPolicyExperiment,
    OffPolicyExperimentConfig,
)

env = gym.make("CartPole-v1")

obs_dim = env.observation_space.shape[0]
num_actions = env.action_space.n

model = th.nn.Sequential(
    th.nn.Linear(obs_dim, 64),
    th.nn.ReLU(),
    th.nn.Linear(64, num_actions),
)
optimizer = th.optim.Adam(model.parameters(), lr=1e-3)

learner = DQNLearner(
    model,
    optimizer,
    DQNConfig(num_actions=num_actions),
)

controller = EpsilonGreedyController(
    GreedyController(model, num_actions=num_actions),
    num_actions=num_actions,
    max_eps=1.0,
    min_eps=0.05,
    anneal_steps=10_000,
)

experiment = OffPolicyExperiment(
    env,
    controller,
    learner,
    OffPolicyExperimentConfig(
        max_steps=10_000,
        run_steps=1,
        batch_size=64,
        log_dir="runs/examples/dqn_cartpole",
    ),
)

experiment.run()
env.close()

More complete scripts are available in the examples/ folder, including DQN, REINFORCE, actor-critic, PPO, SAC, and a comparison with Stable-Baselines3.

Metadata

Release files for drlab 0.2.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drlab 0.2.9
File Size Uploaded Provenance Actions
drlab-0.2.9.tar.gz 28.1 kB Without provenance Details for drlab-0.2.9.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for drlab 0.2.9
File Interpreter ABI Platform Actions
drlab-0.2.9-py3-none-any.whl Python 3 none any Details for drlab-0.2.9-py3-none-any.whl

Total release size: 56.6 kB

Release files / drlab-0.2.9.tar.gz

Download URL drlab-0.2.9.tar.gz
Size 28.1 kB
Tags Source
SHA-256 checksum
How to use checksums
2b93b6dd8caa468d15ae55ff1f9e6ac0e56fbdbff77a600348fb9abea271f451
BLAKE2b-256 checksum
How to use checksums
377e26c281f18a37735ecb7d626e4ab49c3a55cd531a688593e141535c5bc0d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / drlab-0.2.9-py3-none-any.whl

Download URL drlab-0.2.9-py3-none-any.whl
Size 28.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22191fb674224602b94613c7e57cc6209e082cde185ac4481e83ac5297edeec5
BLAKE2b-256 checksum
How to use checksums
f20e01cb9ea0935a156dfeb4fba32cb5b0a57ee4df20fcb4d0e2382f9f5c3851
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for drlab 0.2.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page