Stable-Baselines3 (SB3) is a Python library of reliable reinforcement-learning algorithm implementations built with PyTorch. It connects agents to OpenAI Gym-compatible or custom environments, reducing the amount of algorithm and training code needed for experimentation. section 12.2 section 12.3
Supported algorithms and example environments include:
- A2C, SAC, DDPG, DQN, HER, TD3, and PPO. section 12.2
- Mobile-network coordination, electric-drivetrain control, autonomous-driving scenarios, and reproducible robotic-reaching tasks. section 12.2
- Install the library with pip install stable-baselines3. section 12.2
Train and evaluate CartPole
Create a Gymnasium CartPole environment, instantiate A2C or PPO with MlpPolicy, train for 10,000 timesteps, evaluate across 10 episodes, and run the trained agent for 1,000 steps. Training logs expose episode length, reward, speed, timesteps, losses, and policy statistics. Rewards may fluctuate because the agent explores before increasingly exploiting promising actions. section 12.3
import gymnasium as gym #1
from stable_baselines3 import A2C #2
from stable_baselines3.common.evaluation import evaluate_policy #3
env = gym.make("CartPole-v1", render_mode="rgb_array") #4
model = A2C("MlpPolicy", env, verbose=1) #5
model.learn(total_timesteps=10000, progress_bar=True) #6
mean_reward, std_reward = evaluate_policy(model, model.get_env(),
n_eval_episodes=10) #7
vec_env = model.get_env() #7
obs = vec_env.reset() #7
for i in range(1000): #7
action, _states = model.predict(obs, deterministic=True) #7
obs, rewards, dones, info = vec_env.step(action) #7
vec_env.render("human") #8
#1 Import the gym module.
#2 Import the A2C model
#3 To evaluate the performance of the trained model
#4 Create the CartPole-v1 environment.
#5 Create an A2C model with the MlpPolicy (multilayer perceptron) and the environment.
#6 Start the learning process for the model.
#7 Test and evaluate the trained model.
#8 Render the environment in a way that a human can visualize it.
Use the complete A2C CartPole listing to inspect environment setup, model creation, training, evaluation, and deterministic prediction. section 12.3
