Skip to main content

An RL agent training assistant

Project description

Crete

               _       
  ___ _ __ ___| |_ ___ 
 / __| '__/ _ \ __/ _ \
| (__| | |  __/ ||  __/
 \___|_|  \___|\__\___|
Reinforcement Learning CLI assistant.

Installation

Crete is not released for public use yet, and is not available of PyPI.

Introduction

Crete is a CLI for RL training. Named after the island that Talos, the first AI in human mythology, walked upon.

It can be used as easily as:

python -m crete --help

Registering Agents, Environments and Wrappers

To start using Crete, you must register (at least) one agent. Crete already comes preloaded with all the OpenAI Gym(nasium) environments.

Simply create a module containing an agent that inherits crete.Agent, for example;

 cool_rl_project
 +- crete_profile.yaml
 +- module_a 
 |  +- agents
 |  |  +- __init__.py
 |  |  +- agent_1.py
 |  +- etc...
# module_a/agent_1.py

from crete import Agent, ExtraState, ProfileConfig

class DQNAgent(Agent):
    def __init__(self, obs, n_actions, device='cpu') -> None:
        super().__init__("DQN")
        ...

    def get_action(self, obs, extra_state: ExtraState = None) -> Tuple[int, ExtraState]:
        """Request an action."""
        ...

    def save(self) -> Dict:
        """Extract all policy data."""
        ...

    def load(self, agent_data: Dict):
        """Load policy data."""
        ...

def dqn_training_wrapper(
        env_factory: Callable[[int], gym.Env],  # Function that creates the environment.
        agent: DQNAgent,  # The agent itself.
        config: ProfileConfig,  # Object with config values.
        artifacts: Dict,  # Empty map for storing training information.
        save_callback  # Function for saving agents, for instance at peak rewards.
):
    """The DQN training procedure."""
    ...

And then register that agent in the modules initialisation file. There is also register_env and register_wrapper which work the same way. Environments and wrappers should use the OpenAI Gym(nasium) API.

# module_a/__init__.py

from crete import register_agent
from .dqn_agent import DQNAgent, dqn_training_wrapper

register_agent(
    agent_id="DQN",
    agent_factory=lambda obs, n_actions, device: DQNAgent(obs, n_actions, device=device),
    training_wrapper=dqn_training_wrapper
)

Then, register that module with crete (this will create a .crete.yaml file);

python -m crete module add module_a.agents

Verify the module has been loaded correctly by running the following, which should list DQN.

python -m crete list agents

Training Agents

Agents can be trained using the train or batch command, which trains one or several 'profiles' respectively. A 'profile' is contained within a .yaml file, and may look like the following;

---
# crete_profile.yaml

defaults: # Contains default `config` values that can be overridden. 
  batch_size: 32
  init_epsilon: 1
  final_epsilon: 0.1
  eval_freq: 1000
  gather_freq: 50
  
final_dqn_map_0: # Arbitrary profile ID, can be anything.
  agent_id: DQN # The ID of the registered agent to use.
  env_id: CartPole-v1 # The ID of the environment to use.
  env_args: # Arbitrary environment arguments that will be passed to the constructor
    render_mode: human
  config: # Config values that can be accessed through the Profile object via the training wrapper.
    total_steps :  40000
    decay_steps :  38000
    replay_buffer_size: 10000
    batch_size: 128
    refresh_target_network_freq :  1000
    hidden_layers:
      - 64
      - 64

Training is engaged by;

python -m crete train crete_profile.yaml final_dqn_map_0 ; to train a specific profile or,
python -m crete batch crete_profile.yaml                 ; to train all profiles within a file.

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

crete-0.0.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

crete-0.0.1-py3-none-any.whl (20.6 kB view details)

Uploaded Python 3

File details

Details for the file crete-0.0.1.tar.gz.

File metadata

  • Download URL: crete-0.0.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.6rc1

File hashes

Hashes for crete-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d3d740bde498e3c232fbedb74ce65a2061902114cfc866c6e95ad9cbdbc2a4c8
MD5 c0f703128b77e0153b87cda53404a877
BLAKE2b-256 62d786473f40eed8f3352ef0f16e7d9b457bfec9a13ec90b1196454726c78bd4

See more details on using hashes here.

File details

Details for the file crete-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: crete-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 20.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.6rc1

File hashes

Hashes for crete-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 da7d714d93e58e32595f6026557637484143616a8903f6390bd8fe3cbc3f15e0
MD5 c739a5998904673bb88b25359868ed5f
BLAKE2b-256 82849949675eef2318dcd389cbf38ae62e27b3d50e8734a9a9f1340eddf161f1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page