Skip to content
Skillpedia
Agents51k · #1983

stable-baselines3

Enables your agent to train and evaluate reinforcement learning models. It's used to teach agents how to make decisions in complex environments, like games or robotics.

What it does

This tool helps train artificial intelligence 'agents' to learn how to make decisions in simulated environments. It uses reinforcement learning algorithms so the agents improve with experience.

When to use it

  • ✓When you want to teach a virtual robot to walk.
  • ✓When creating a game where a character learns to play by itself.
  • ✓To test strategies in complex simulations.

Example of what to ask your AI

“Train an agent to play Pac-Man using the PPO algorithm.”

Want more? Open the sections below.

How to use

The easiest way: copy the message below and paste it into Claude, ChatGPT, Cursor or any AI. It reads the skill and follows the instructions.

Skill link

https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/stable-baselines3/SKILL.md

Ready-to-paste message

Use this skill to help me: https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/stable-baselines3/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
How it works

You define the environment where the agent will learn and choose an algorithm. The tool then runs the training, allowing the agent to adjust its actions to get rewards.

Installation

Easiest way: copy the skill link and paste it into Claude, ChatGPT or Cursor asking it to follow the instructions.

In the terminal (Claude Code, Codex, Cursor), in your project folder:

npx skills add K-Dense-AI/scientific-agent-skills --skill stable-baselines3

Or download just the file:

curl -L https://raw.githubusercontent.com/K-Dense-AI/scientific-agent-skills/main/skills/stable-baselines3/SKILL.md -o SKILL.md
Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

Stable Baselines3

Overview

Stable Baselines3 (SB3) is a PyTorch-based library providing reliable implementations of reinforcement learning algorithms. This skill provides comprehensive guidance for training RL agents, creating custom environments, implementing callbacks, and optimizing training workflows using SB3's unified API.

Current upstream: SB3 2.9.0 (June 15, 2026). Docs: stable-baselines3.readthedocs.io.

Installation

Tested against stable-baselines3 2.9.0. Requires Python 3.10+ (3.9 dropped in 2.8.0) and PyTorch >= 2.8.

# Basic installation
uv pip install "stable-baselines3==2.9.0"

# With extra dependencies (TensorBoard, ale-py for Atari, etc.)
uv pip install "stable-baselines3[extra]==2.9.0"

The 2.9.0 release supports Gymnasium >=0.29.1,<2.0; this review exercised Gymnasium 1.3.0, PyTorch 2.14.1 and Python 3.13 on CPU. pandas/matplotlib are now optional extras. Installation requires network unless packages are cached; local RL training needs no credentials or service endpoints.

On zsh, quote brackets: uv pip install 'stable-baselines3[extra]==2.9.0'.

For MuJoCo continuous-control benchmarks:

uv pip install "gymnasium[mujoco]"

Check your version:

import stable_baselines3
print(stable_baselines3.__version__)

Related Projects

  • SB3-Contrib: experimental algorithms (MaskablePPO, CrossQ, QR-DQN, RecurrentPPO) — separate sb3-contrib package
  • RL Baselines3 Zoo: pre-trained agents, hyperparameters, training scripts
  • SBX: SB3 + JAX implementations for users who prefer JAX over PyTorch

Core Capabilities

1. Training RL Agents

Basic Training Pattern:

import gymnasium as gym
from stable_baselines3 import PPO

# Create environment
env = gym.make("CartPole-v1")

# Initialize agent (device="cpu" is often faster for MlpPolicy on small envs)
model = PPO("MlpPolicy", env, verbose=1, device="cpu", seed=0)

# Train the agent
model.learn(total_timesteps=10000)

# Save the model
model.save("ppo_cartpole")

# Load the model (without prior instantiation)
model = PPO.load("ppo_cartpole", env=env, device="cpu")
env.close()

Important Notes:

  • total_timesteps is a lower bound; actual training may exceed this due to batch collection
  • Call the class method PPO.load(...) and keep the returned new model
  • The replay buffer is NOT saved with the model to save space

Algorithm Selection: Use references/algorithms.md for detailed algorithm characteristics and selection guidance. Quick reference:

  • PPO/A2C: General-purpose, supports Box, Discrete, flat MultiDiscrete and MultiBinary actions, good for multiprocessing
  • SAC/TD3: Continuous control, off-policy, sample-efficient
  • DQN: Discrete actions, off-policy
  • HER: Replay-buffer strategy for goal-conditioned off-policy tasks

See scripts/train_rl_agent.py for a complete training template with best practices.

2. Custom Environments

Requirements: Custom environments must inherit from gymnasium.Env and implement:

  • __init__(): Define action_space and observation_space
  • reset(seed, options): Return initial observation and info dict
  • step(action): Return observation, reward, terminated, truncated, info
  • render(): Visualization (optional)
  • close(): Cleanup resources

Key Constraints:

  • Default CNN image preprocessing expects np.uint8 in range [0, 255]
  • Use channel-first format when possible (channels, height, width)
  • SB3 normalizes images automatically by dividing by 255
  • For pre-normalized float images, use channel-first layout and policy_kwargs={"normalize_images": False}
  • SB3 does NOT support Discrete or MultiDiscrete spaces with start!=0

Validation:

from stable_baselines3.common.env_checker import check_env

check_env(env, warn=True)

See the template and environment guide. The template now observes both agent and random goal coordinates, shape (4,); old shape (2,) checkpoints require retraining. gym.make("CustomEnv-v0") adds the 100-step time limit; direct CustomEnv() does not. check_env checks API consistency, not Markov sufficiency, reward correctness or learnability.

…

FAQ

What is 'reinforcement learning'?

It's a type of machine learning where an agent learns to make decisions by receiving rewards or punishments for its actions.

What are 'custom environments'?

These are simulations you create yourself to train the agent in specific situations that aren't readily available.

Why use 'vectorized environments'?

They allow running multiple simulations at the same time, which greatly speeds up agent training.

Details

Original description by the author

Trains and evaluates single-agent reinforcement learning with Stable Baselines3 (PPO, SAC, DQN, TD3, DDPG, A2C), Gymnasium custom environments, vectorized rollouts, callbacks, and checkpoint normalization. Applies to reproducible RL experiments, continuous control, discrete actions, and SB3-Contrib recurrent or masked policies.

repo: K-Dense-AI/scientific-agent-skills

Popularity

GitHub stars
51k
Ranking position
#1983
License
MIT
Updated
10/15/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills