Foundations to Frontier
Tests

Stable-Baselines3 and CleanRL

Stable-Baselines3 (SB3) and CleanRL are the two most used libraries of reference RL implementations: SB3 as a tested, modular package with a three-line API, CleanRL as single-file scripts in which every implementation detail is visible. This page wraps the PPO of topic L4-4 in an SB3-style interface (learn, predict, evaluate_policy, save and load), then uses it for the lesson both libraries' authors stress most: results vary across random seeds, so a comparison needs many runs and an honest interval.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Don’t build the engine to drive the car

Writing a reinforcement learning algorithm from scratch is like building a car engine: instructive, but full of small details that are easy to get wrong. Libraries such as Stable-Baselines3 give you a tested engine: you choose the settings, call learn, and get a trained agent. CleanRL instead hands you the whole engine on one page, so you can read and modify every part. Either way, one lesson remains: a single test drive proves little, because the same car on the same road behaves differently each time the dice of randomness are rolled.

RL libraries lab
clip 0.2clip 0.1thousands of steps to solve (left is faster; 51.2 = not solved)
Each dot is one PPO run on cart-pole from Exercise 2 of this topic (48 per setting). Over all runs, clip range 0.2 needed a median of about 12.8 thousand steps and 0.1 about 19.9 thousand. This experiment drew 3 runs per setting (large dots; bars are their means): 14.5 against 22.1, which gets the order right. Over 4,000 such experiments with 3 seeds, 5.3% ranked clip 0.1 at least as fast.
Why engineers care

SB3 is maintained by the German Aerospace Center (DLR) robotics team and collaborators; CleanRL is maintained by Costa Huang and contributors and is widely used for research baselines.

📝 Quick check (Level 0)
0/3 answered
  1. 1. SB3 provides…
  2. 2. CleanRL's implementations are…
  3. 3. A single training run proves little because…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • SB3 API: model = PPO(“MlpPolicy”, env, **hyperparameters); model.learn(total_timesteps); action, _ = model.predict(obs, deterministic=True); model.save(path); PPO.load(path).
  • evaluate_policy(model, env, n_eval_episodes, deterministic): mean and standard deviation of evaluation returns.
  • Callbacks: functions called during training for logging, evaluation or early stopping.
  • Schedules: hyperparameters such as the learning rate given as functions of progress_remaining (1 at the start, 0 at the end).
  • CleanRL: one file per algorithm and setting (ppo.py, ppo_atari.py, sac_continuous_action.py), with experiment tracking.
  • Implementation details: advantage normalisation, observation and reward normalisation, orthogonal initialisation, gradient clipping and learning-rate annealing change results substantially.
  • Seeds and statistics: report many seeds, interquartile means or medians with bootstrap confidence intervals, and performance profiles (the rliable library).
📝 Quick check (Level 1)
0/3 answered
  1. 1. predict(obs, deterministic=True) returns…
  2. 2. A schedule in SB3 is a function of…
  3. 3. rliable recommends reporting…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

An SB3-style interface around PPO

Exercise 1.

  • The vectorised PPO of topic L4-4 for cart-pole (linear sigmoid policy, quadratic critic, GAE, clipped surrogate, Adam), extended to continue from given weights.
  • A Gymnasium-style CartPoleEnv (topic L8-1), and a PPO class with SB3’s interface: constructor hyperparameters (n_envs, n_steps, n_epochs, clip_range, learning_rate, gamma, gae_lambda, seed), learn(total_timesteps, callback) that continues training, predict(obs, deterministic), save() and load().
  • evaluate_policy returning the mean and standard deviation of returns over seeded evaluation episodes.
  • Checks: training improves evaluation, callbacks see the step count, and a saved and reloaded model predicts identically.

Practice.

  • Start from a library implementation (SB3, CleanRL) and its tuned hyperparameters (RL Zoo) before writing your own.
  • Run at least 5 to 10 seeds, more for close comparisons, and report interquartile means or medians with bootstrap intervals.
  • Evaluate separately from training with deterministic actions and a fixed set of evaluation seeds.
  • Log everything (TensorBoard, Weights & Biases) and record library versions and hyperparameters.
  • When results differ from a paper, check implementation details first: normalisation, initialisation and time-limit handling (topic L8-1).
model = PPO(n_envs=16, n_steps=32, n_epochs=10, clip_range=0.2, seed=0)
model.learn(total_timesteps=20_480, callback=log.append)
mean, std = evaluate_policy(model, CartPoleEnv(), n_eval_episodes=10)
blob = model.save(); clone = PPO.load(blob)
action, _ = clone.predict(obs, deterministic=True)

# The same in Stable-Baselines3:
#   from stable_baselines3 import PPO
#   from stable_baselines3.common.evaluation import evaluate_policy
#   model = PPO("MlpPolicy", "CartPole-v1", n_steps=32, n_epochs=10, seed=0)
#   model.learn(20_480); evaluate_policy(model, model.get_env(), 10)
🐍 Exercise 1: an SB3-style API
Wrap the PPO of topic L4-4 in an SB3-style class with learn, predict, save and load, add evaluate_policy, and check that training, evaluation and saving behave as in the library.
📝 Quick check (Level 2)
0/3 answered
  1. 1. Tuned hyperparameters for SB3 are collected in…
  2. 2. Evaluation should use…
  3. 3. When results differ from a paper, first check…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

Why seeds matter so much in RL. A training run’s outcome depends on the network initialisation, the exploration noise, the environment’s randomness and the order of updates, and RL feeds its own decisions back into its data. Small early differences compound: one run finds the balancing behaviour early and improves quickly, another wanders for longer. Henderson and colleagues (2018) showed that different sets of 5 seeds of the same algorithm could give statistically different learning curves.

Sampling error in small experiments. With n runs, the standard error of a mean is σ/√n. For three runs it is 58% of the spread of a single run, so two groups of three runs from identical settings routinely differ by 20% or more, as Exercise 2 measures. A comparison is only meaningful if the difference is large relative to this error.

Robust summaries. The median and the interquartile mean (IQM) are less sensitive to a few failed runs than the mean. Agarwal and colleagues (2021) recommended IQM with stratified bootstrap confidence intervals and performance profiles, implemented in rliable.

The bootstrap with few runs. Resampling three values cannot reveal variation that the three values do not contain, so bootstrap intervals from very few runs are too narrow; with few runs, Student’s t intervals or simply more runs are needed.

📝 Quick check (Level 3)
0/3 answered
  1. 1. The standard error of a mean of n runs is…
  2. 2. Henderson and colleagues showed that different seed sets…
  3. 3. Bootstrap intervals from 3 runs are…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

How variable is PPO across seeds, how often would a 3-seed experiment rank two settings wrongly, and how different can identical settings look?

History.

  • A. Hill and colleagues (2018) released Stable Baselines, a fork of OpenAI Baselines; A. Raffin and colleagues (2021) published Stable-Baselines3 for PyTorch.
  • P. Henderson and colleagues (2018) showed how seeds, hyperparameters and implementations change deep RL results.
  • C. Colas, O. Sigaud and P.-Y. Oudeyer (2018) analysed how many seeds a comparison needs; R. Agarwal and colleagues (2021) introduced rliable.
  • S. Huang and colleagues (2022) published CleanRL and catalogued 37 implementation details of PPO.

Exercise 2.

  • 16 PPO runs with clip range 0.2 and 16 with clip range 0.1 (10 epochs per batch, otherwise as in Exercise 1), 100 batches of 16 × 32 steps each, all trained together.
  • Score: thousands of environment steps until the last 20 episodes average 195; runs that never got there are scored at the budget of 51.2.
  • A bootstrap 95% interval for the difference of medians; 4,000 random 3-seed experiments per comparison; and the gap between two disjoint groups of 3 runs with identical settings.
seed median, clip 0.2 median, clip 0.1 unsolved (0.2, 0.1) 95% interval for the difference wrong 3-seed rankings identical settings: median gap, 1 in 10 gap
0 12.27 23.51 0, 1 3.0 to 16.8 2.1% 20%, 52%
1 12.67 18.05 0, 0 1.2 to 8.5 7.6% 21%, 57%
2 14.01 25.76 0, 2 2.5 to 23.1 4.2% 19%, 51%

Findings.

  • Clip range 0.1 was slower, with medians 1.4 to 1.9 times those of 0.2, and failed to solve within the budget in 3 of 48 runs against none.
  • Single runs varied widely: with clip 0.2 from 6.6 to 23.3 thousand steps, a factor of 3.5 between the fastest and slowest of identical settings.
  • 3-seed experiments got the order wrong 2 to 8% of the time, even though the medians differed by 40 to 90%.
  • Identical settings looked different: two groups of 3 runs differed by a median of about 20% and by more than 50% one time in ten, an ‘improvement’ that a careless comparison would report.
🐍 Exercise 2 (research): how variable is PPO across seeds, how often would a 3-seed experiment rank two settings wrongly, and how different can identical settings look?
Run 16 seeds each of PPO with clip ranges 0.2 and 0.1, and measure how reliable 3-seed comparisons would have been.

RL libraries in practice

  • Stable-Baselines3: PPO, A2C, DQN, SAC, TD3 and more, with SB3-Contrib for recurrent PPO and others, and RL Baselines3 Zoo for tuned hyperparameters.
  • CleanRL: single-file implementations with Weights & Biases tracking and JAX versions for speed.
  • Other libraries: RLlib for distributed training, TorchRL and Tianshou for modular research code, Sample Factory and PureJaxRL for throughput.
  • Reporting: follow rliable’s recommendations and publish seeds, configurations and code.

References

  1. A. Raffin and colleagues (2021). “Stable-Baselines3: reliable reinforcement learning implementations.” Journal of Machine Learning Research.
  2. S. Huang and colleagues (2022). “CleanRL: high-quality single-file implementations of deep reinforcement learning algorithms.” Journal of Machine Learning Research.
  3. P. Henderson and colleagues (2018). “Deep reinforcement learning that matters.” AAAI.
  4. R. Agarwal and colleagues (2021). “Deep reinforcement learning at the edge of the statistical precipice.” NeurIPS.
  5. C. Colas, O. Sigaud and P.-Y. Oudeyer (2018). “How many random seeds? Statistical power analysis in deep reinforcement learning experiments.” arXiv:1806.08295.