stable-baselines3 – skill Agent & Tự động hóa cho agent AI
Huấn luyện và đánh giá học tăng cường đơn tác nhân với Stable Baselines3 (PPO, SAC, DQN, TD3, DDPG, A2C), môi trường Gymnasium tùy chỉnh, rollout vectorized, callback và chuẩn hóa checkpoint. Áp dụng cho thí nghiệm RL có thể tái lập, điều khiển liên tục, hành động rời rạc và các biến thể recurrent hoặc masked của SB3-Contrib
Cài skill stable-baselines3 bằng một lệnh
Chọn công cụ bạn đang dùng, copy lệnh và dán vào terminal. Skill sẽ nằm trong thư mục skills của công cụ đó và sẵn sàng dùng ở phiên làm việc tiếp theo.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/stable-baselines3-d56ae.tgz | tar xz -C ~/.claude/skills
Gọi trong Claude Code: /stable-baselines3 – rồi mô tả công việc bạn muốn làm.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/stable-baselines3-d56ae.tgz | tar xz -C ~/.codex/skills
Gọi trong Codex: $stable-baselines3 – rồi mô tả công việc bạn muốn làm.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/stable-baselines3-d56ae.tgz | tar xz -C ~/.hermes/skills
Gọi trong Hermes Agent: /stable-baselines3 – rồi mô tả công việc bạn muốn làm.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/stable-baselines3-d56ae.tgz | tar xz -C ~/.deepcode/skills
Gọi trong DeepSeek: /stable-baselines3 – rồi mô tả công việc bạn muốn làm.
Nội dung SKILL.md
Nội dung SKILL.md gốc bằng tiếng Anh.
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](https://stable-baselines3.readthedocs.io/en/v2.9.0/).
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](https://github.com/Stable-Baselines-Team/stable-baselines3-contrib): experimental algorithms (MaskablePPO, CrossQ, QR-DQN, RecurrentPPO) — separate
sb3-contribpackage - [RL Baselines3 Zoo](https://github.com/DLR-RM/rl-baselines3-zoo): pre-trained agents, hyperparameters, training scripts
- [SBX](https://github.com/araffin/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_timestepsis 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_spacereset(seed, options): Return initial observation and info dictstep(action): Return observation, reward, terminated, truncated, inforender(): Visualization (optional)close(): Cleanup resources
Key Constraints:
- Default CNN image preprocessing expects
np.uint8in 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
DiscreteorMultiDiscretespaces withstart!=0
Validation:
from stable_baselines3.common.env_checker import check_env
check_env(env, warn=True)See [the template](scripts/custom_env_template.py) and [environment guide](references/custom_environments.md).
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.
3. Vectorized Environments
Purpose:
Vectorized environments run multiple environment instances in parallel, accelerating training and enabling certain wrappers (frame-stacking, normalization).
Types:
- DummyVecEnv: Sequential execution on current process (for lightweight environments)
- SubprocVecEnv: Parallel execution across processes (for compute-heavy environments)
Quick Setup:
from stable_baselines3 import PPO
from stable_baselines3.common.env_util import make_vec_env
# DummyVecEnv batches 4 lightweight environments sequentially.
env = make_vec_env("CartPole-v1", n_envs=4, seed=0)
try:
model = PPO("MlpPolicy", env, verbose=1, device="cpu")
model.learn(total_timesteps=25000)
finally:
env.close()Off-Policy Optimization:
With step-based train_freq, gradient_steps=-1 matches gradient updates to
collected transitions (train_freq * n_envs) after warmup. This changes compute
and reuse of data; benchmark it rather than assuming it is always faster.
SubprocVecEnv creation belongs under a main guard in a Python file.
API Differences:
reset()returns only observations (info available invec_env.reset_infos)step()returns 4-tuple:(obs, rewards, dones, infos)not 5-tuple- Environments auto-reset after episodes
- Terminal observations available via
infos[env_idx]["terminal_observation"]
See references/vectorized_envs.md for detailed information on wrappers and advanced usage.
4. Callbacks for Monitoring and Control
Purpose:
Callbacks enable monitoring metrics, saving checkpoints, implementing early stopping, and custom training logic without modifying core algorithms.
Common Callbacks:
- EvalCallback: Evaluate periodically and save best model
- CheckpointCallback: Save model checkpoints at intervals
- StopTrainingOnRewardThreshold: Stop when target reward reached
- ProgressBarCallback: Display training progress with timing
Custom Callback Structure:
from stable_baselines3.common.callbacks import BaseCallback
class CustomCallback(BaseCallback):
def _on_training_start(self):
# Called before first rollout
pass
def _on_step(self):
# Called after each environment step
# Return False to stop training
return True
def _on_rollout_end(self):
# Called at end of rollout
passAvailable Attributes:
self.model: The RL algorithm instanceself.num_timesteps: Total environment stepsself.training_env: The training environment
Chaining Callbacks:
from stable_baselines3.common.callbacks import CallbackList
callback = CallbackList([eval_callback, checkpoint_callback, custom_callback])
model.learn(total_timesteps=10000, callback=callback)See references/callbacks.md for comprehensive callback documentation.
5. Model Persistence and Inspection
Saving and Loading:
from stable_baselines3.common.vec_env import VecNormalize
# After training with VecNormalize, save a matching pair:
model.save("model_name")
model.get_vec_normalize_env().save("vec_normalize.pkl")
# Build the same underlying environment and wrappers before loading:
vec_env = make_vec_env("Pendulum-v1", n_envs=1, seed=20000)
vec_env = VecNormalize.load("vec_normalize.pkl", vec_env)
vec_env.training = False
vec_env.norm_reward = False
model = PPO.load("model_name", env=vec_env, device="cpu")This is a continuation fragment for a PPO/Pendulum run with normalization.
Load only trusted model/statistics files. For off-policy training continuation,
save_replay_buffer() / load_replay_buffer() are separate from save() / load().
Resume with a live environment and learn(..., reset_num_timesteps=False).
Parameter Access:
# Get parameters
params = model.get_parameters()
# Set parameters
model.set_parameters(params)
# Access PyTorch state dict
state_dict = model.policy.state_dict()6. Evaluation and Recording
Evaluation:
When training uses VecNormalize, load its saved training statistics into a separate evaluation environment with the same observation wrappers. Set training=False to freeze those statistics and norm_reward=False to report rewards in the original units; do not fit normalization on evaluation episodes. Save the normalization state alongside the model checkpoint.
from stable_baselines3.common.evaluation import evaluate_policy
mean_reward, std_reward = evaluate_policy(
mode… (nội dung đầy đủ có trong gói tải về)
Skill liên quan
- wp-plugin-automation – [Không dịch: mô tả công cụ kiểm thử CVE plugin WordPress hàng loạt trên nhiều domain]
- discord-automation – Tự động hóa tác vụ Discord qua Rube MCP (Composio): tin nhắn, kênh, vai trò, webhook…
- dispatching-parallel-agents – Dùng khi có từ 2 tác vụ trở lên độc lập, có thể làm song song mà không chia sẻ trạng…
- superself – Dùng khi dự án lưu trạng thái trong Superself (khối `<!-- superself:begin` trong…
- distribute-skill-to-all-agents – Phân phối một skill tới các thư mục skill của agent đã cấu hình, đồng thời tôn trọng bố…
- task-coordination-strategies – Phân rã tác vụ phức tạp, thiết kế đồ thị phụ thuộc và điều phối công việc đa agent với…
- documentation-and-adrs – Ghi lại quyết định và tài liệu. Dùng khi đưa ra quyết định kiến trúc, thay đổi API công…
- team-communication-protocols – Giao thức nhắn tin có cấu trúc cho giao tiếp nhóm agent, gồm chọn loại tin nhắn, phê…