Skip to main content

Reinforcement Learning Framework

An easy-to-read Reinforcement Learning (RL) framework. Provides standardized interfaces and implementations to various Reinforcement Learning and Imitation Learning methods and utilities.

Main Features

  • Using various Reinforcement Learning algorithms to learn from gym environment interaction, which are implemented in Stable-Baselines 3
  • Using various Imitation Learning algorithms to learn from replays, which are implemented in Imitation
  • Integrate or implement own custom agents and algorithms in a standardized interface
  • Upload your models (with logged metrics, checkpoints and video recordings) to HuggingFace Hub or ClearML

Set-Up

Install all dependencies in your development environment

To set up your local development environment, please install poetry (see (tutorial)[https://python-poetry.org/docs/\]) and run:

poetry install

Behind the scenes, this creates a virtual environment and installs rl_framework along with its dependencies into a new virtualenv. Whenever you run poetry run <command>, that <command> is actually run inside the virtualenv managed by poetry.

You can now import functions and classes from the module with import rl_framework.

Optional: Install FFMPEG to enable generation of videos (for upload)

The creation of videos for the functionality of creating video-replays of the agent performance on the environment requires installing the FFMPEG package on your machine. This feature is important if you plan to upload replay videos to an experiment tracking service together with the agent itself. The ffmpeg command needs to be available to invoke from the command line, since it is called from Python through a os.system invoke. Therefore, it is important that you install this package directly on your machine.

Please follow the guide which can be found here to install the FFMPEG library on your respective machine.

Optional: Preparation for pushing your models to the HuggingFace Hub

Optional: Preparation for using a Unity environment (optional)

In order to use environments based on the Unity game framework, make sure to follow the installation procedures detailed in following installation guideline provided by Unity Technologies. In short:

Getting Started

Configuring an environment

To integrate your environment you wish to train on, you need to create a gymnasium.Env object representing your problem. For this you can use any existing environment with the gym interface. See here for further documentation.

Reinforcement Learning agent

Class definition

To integrate the Reinforcement Learning algorithm you wish to train an agent on your environment with, you need to create an RLAgent class representing your training agent. For this you can

Training

After configuring the environment and the agent, you can start training your agent on the environment. This can be done in one line of code:

agent.train(training_environments=environments, total_timesteps=N_TRAINING_TIMESTEPS)

Independent of which environment and which agent you choose, the unified interface allows to always start the training this way.

Imitation Learning agent

Class definition

To integrate the Imitation Learning algorithm you wish to train an agent on your replays with, you need to create an ILAgent class representing your training agent. For this you can

Training

First you need to collect the replays (recorded episode sequences) from an expert policy or a human demonstration. They should be recorded as imitation.TrajectoryWithRew objects and saved with the serialize.save method (see imitation library documentation) and stored as files. You can afterward load them with the following code line:

sequence = EpisodeSequence.from_dataset(TRAJECTORIES_PATH)

Afterward, you can start training your agent on the environment. This can be done in one line of code:

agent.train(episode_sequence=sequence, training_environments=environments, total_timesteps=N_TRAINING_TIMESTEPS)

The training environments are used by the imitation learning algorithms in different ways. Some of them only use it for the observation and action space information, while others use it for iteratively checking and improving the imitation policy.

Evaluation

Once you trained the agent, you can evaluate the agent policy on the environment and get the average accumulated reward (and standard deviation) as evaluation metric. This evaluation method is implemented in the evaluate function of the agent and called with one line of code:

agent.evaluate(evaluation_environment=environment, n_eval_episodes=100, deterministic=False)

Uploading and downloading models from a experiment registry

Once you trained the agent, you can upload the agent model to an experiment registry (HuggingFace Hub or ClearML) in order to share and compare your agent to others. You can also download yours or other agents from the same service and use them for solving environments or re-training. The object which allows for this functionality is HuggingFaceConnector and ClearMLConnector, which can be found in the connection collection package.

Examples

In this RL example script and in this IL example script you can see all of the above steps unified.

Development

Notebooks

You can use your module code (src/) in Jupyter notebooks without running into import errors by running:

poetry run jupyter notebook

or

poetry run jupyter-lab

This starts the jupyter server inside the project's virtualenv.

Assuming you already have Jupyter installed, you can make your virtual environment available as a separate kernel by running:

poetry add ipykernel
poetry run python -m ipykernel install --user --name="reinforcement-learning-framework"

Note that we mainly use notebooks for experiments, visualizations and reports. Every piece of functionality that is meant to be reused should go into module code and be imported into notebooks.

Testing

We use pytest as test framework. To execute the tests, please run

pytest tests

To run the tests with coverage information, please use

pytest tests --cov=src --cov-report=html --cov-report=term

Have a look at the htmlcov folder, after the tests are done.

Distribution Package

To build a distribution package (wheel), please use

python setup.py bdist_wheel

This will clean up the build folder and then run the bdist_wheel command.

Contributions

Before contributing, please set up the pre-commit hooks to reduce errors and ensure consistency

pip install -U pre-commit
pre-commit install

If you run into any issues, you can remove the hooks again with pre-commit uninstall.

License

© Alexander Zap

Metadata

Release files for reinforcement-learning-framework 0.10.3

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

Source distribution (sdist)

Source distribution for reinforcement-learning-framework 0.10.3
File Size Uploaded
reinforcement_learning_framework-0.10.3.tar.gz 51.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for reinforcement-learning-framework 0.10.3
File Interpreter ABI Platform
reinforcement_learning_framework-0.10.3-py3-none-any.whl Python 3 none any Details

Total release size: 119.5 kB

Release files / reinforcement_learning_framework-0.10.3.tar.gz

Download URL reinforcement_learning_framework-0.10.3.tar.gz
Size 51.0 kB
Tags Source
SHA-256 checksum
How to use checksums
837ae13eb98fe2cede902199f177be9d3fd3483ecece71e7d63fdc25ac5d8a4e
BLAKE2b-256 checksum
How to use checksums
2b3fd2152c95ab73a5ea0a3d41a0f31ecd9e90c09ace34e82014431cf2e8514e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.10 Windows/10

Release files / reinforcement_learning_framework-0.10.3-py3-none-any.whl

Download URL reinforcement_learning_framework-0.10.3-py3-none-any.whl
Size 68.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22277bd5e35c5c5ca387e83a66ce2dd802b36f645e976a54047b61847925eaa5
BLAKE2b-256 checksum
How to use checksums
323fac1d889897832145e96e35e2f3dd99fcd52cbcf0b4b3ef8931d986a3645c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.2 CPython/3.8.10 Windows/10

Release history Release notifications | RSS feed

0.11.6

2 release files

0.11.5

2 release files

0.11.4

2 release files

0.11.3

2 release files

0.11.2

2 release files

0.11.1

2 release files

0.11.0

2 release files

This release

0.10.3 This release

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.21

2 release files

0.9.19

2 release files

0.9.18

2 release files

0.9.17

2 release files

0.9.16

2 release files

0.9.15

2 release files

0.9.14

2 release files

0.9.13

2 release files

0.9.12

2 release files

0.9.11

2 release files

0.9.10

2 release files

0.9.9

2 release files

0.9.8

2 release files

0.9.7

2 release files

0.9.6

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.8

2 release files

0.8.6

2 release files

0.8.5

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.9

2 release files

0.6.8

2 release files

0.6.7

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

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