Skip to main content

An easy-to-read Reinforcement Learning (RL) framework. Provides standardized interfaces and implementations to various Reinforcement Learning methods and environments. Also this is the main place to start your journey with Reinforcement Learning and learn from tutorials and examples.

Project description

Reinforcement Learning Framework

An easy-to-read Reinforcement Learning (RL) framework. Provides standardized interfaces and implementations to various Reinforcement Learning methods and environments. Also, this is the main place to start your journey with Reinforcement Learning and learn from tutorials and examples.

Main Features

  • Choose from a growing number of Gym environments and MLAgent environments
  • Using various Reinforcement Learning algorithms for learning, which are implemented in Stable-Baselines 3
  • Integrate or implement own custom environments and agents in a standardized interface
  • Upload your models to the HuggingFace Hub

Set-Up

Activate your development environment

If you are on a UNIX-based OS: You are fine. Continue with the next step.

If you are on Windows: Make sure to use a WSL Python interpreter as your development environment, since we require a UNIX-based system underneath Python to run a lot of the environments and algorithms. For users using PyCharm, see https://www.jetbrains.com/help/pycharm/using-wsl-as-a-remote-interpreter.html for more information. For users using Visual Studio Code, see https://code.visualstudio.com/docs/remote/wsl-tutorial and https://code.visualstudio.com/docs/remote/wsl for more information.

Install all dependencies in your development environment

To set up your local development environment, please 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 an Environment class representing your problem. For this you can

Configuring an agent

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

  • you use an existing Reinforcement Learning algorithm implemented in the Stable-Baselines 3 framework with the StableBaselinesAgent class (see the Example section below)
  • create a custom Reinforcement Learning algorithm by inheriting from the base BaseAgent class, which specifies the required interface

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(environments=environments, total_timesteps=100000)

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

Evaluating

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 the HuggingFace Hub

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

Example

In this example script you can see all of the above steps unified.

For a quick impression in this README, find a minimal training and evaluation example here:

# Create environment(s); multiple environments for parallel training
environments = [GymEnvironmentWrapper(ENV_ID) for _ in range(PARALLEL_ENVIRONMENTS)]

# Create new agent
agent = StableBaselinesAgent(
    algorithm=StableBaselinesAlgorithm.PPO,
    algorithm_parameters={
        "policy": "MlpPolicy"
    }
)
# Train agent
agent.train(environments=environments, total_timesteps=100000)

# Evaluate the model
mean_reward, std_reward = agent.evaluate(evaluation_environment=environments[0])

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

and 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

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

reinforcement_learning_framework-0.6.4.tar.gz (29.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file reinforcement_learning_framework-0.6.4.tar.gz.

File metadata

File hashes

Hashes for reinforcement_learning_framework-0.6.4.tar.gz
Algorithm Hash digest
SHA256 40cb5831c696ff0988b24555d09c44853f8e4a5641adc15f53023ad3b7b21762
MD5 950b07df3d45c6587732ac108bc69b7f
BLAKE2b-256 fea1e624c27305c6bbdd42ab9d2b0b488786b31761ae224906ddcce513e5eda1

See more details on using hashes here.

File details

Details for the file reinforcement_learning_framework-0.6.4-py3-none-any.whl.

File metadata

File hashes

Hashes for reinforcement_learning_framework-0.6.4-py3-none-any.whl
Algorithm Hash digest
SHA256 2eef8fb37d360bd43ceef067ed12224d07e308ce0967814ac8c7b3f09bb9d0f6
MD5 dbe15b60061ae3d07c5dc5aa9130e0b7
BLAKE2b-256 26f08f7c19c5fa9354a0b41eb5e8dddc7eda06bd83ef2ea1f96b1c2b7d04b777

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