Skip to content

Coming from Stable-Baselines3

For readers who know Stable-Baselines3 (SB3), this page puts its calls beside Ajax's and lists the defaults that differ. The SB3 blocks are for comparison; the Ajax blocks run in order.

The main difference

In SB3 a model holds its networks, and learn changes it in place. In Ajax an agent holds only its settings: train returns the trained state (the networks, the optimisers, the replay), and everything that uses a trained agent takes that state. One train call trains several seeds, so a state holds one copy per seed.

Train

# doc: skip (Stable-Baselines3, for comparison)
from stable_baselines3 import PPO

model = PPO("MlpPolicy", "CartPole-v1", seed=0)
model.learn(total_timesteps=4_096)
from ajax import PPO

agent = PPO("CartPole-v1")
state, evaluations = agent.train(seed=[0, 1], n_timesteps=4_096)
print(evaluations.returns)  # one row per seed, one column per evaluation

train evaluates the policy about ten times over a run, after whole rollouts (here twice: two rollouts of 2,048 steps), and prints a line for each, with no callback to set up. logging_config=None turns both off.

There is no policy name to give: an MLP reads the observations. For images, pass cnn_image_shape to read them with a CNN encoder (Networks); Ajax warns when images reach an MLP.

The defaults are the original's

Ajax implements each algorithm as its paper or its authors' code does, and keeps their defaults, which each algorithm page cites. SB3 sets some of its own (here those of its 2.9.0 constructors), so the same call does not train the same thing in both libraries.

Agent Ajax, as the original SB3
PPO value loss clipped around the rollout's value (ppo2) not clipped
PPO observations and rewards normalised (ppo2's MuJoCo run) as the environment gives them, unless wrapped in VecNormalize
PPO vf_coef weighs half the squared error weighs the whole squared error: SB3's 0.5 is 1.0 here
PPO batch_size=64 with n_steps=2048 gives 2048 / 64 = 32 minibatches, whatever n_envs is batch_size is a minibatch's size: 32 minibatches per environment
SAC learning_starts=10_000 random steps before the first update 100
TD3 learning_starts=25_000; learning rates 3e-4; exploration noise 0.1 100; 1e-3; no noise unless given
DQN CleanRL's vector-observation settings: Adam at 2.5e-4, the squared loss, a replay of 10,000, batches of 128, an update every 10 steps, the target copied every 50 updates, learning_starts=10_000, epsilon decaying over half the run Adam at 1e-4, the Huber loss, a replay of 1,000,000, batches of 32, an update every 4 steps, the target copied every 10,000 steps, learning_starts=100, epsilon decaying over a tenth of the run

A short run shows the warm-up most: SAC on 20,000 steps acts at random for the first half unless learning_starts is lowered.

For PPO, PPO.preset("SB3", env_id) sets SB3's defaults in one call, and its page says what stays yours to set (the number of minibatches with several environments) and what no option matches:

ported = PPO.preset("SB3", "CartPole-v1", n_envs=8, n_steps=256, num_minibatches=32)
print(ported.clip_value_loss, ported.normalize_observations)  # False False

The other agents have no such preset: pass the values of the SB3 column. For DQN, SB3's Huber loss is td_loss_fn=compute_huber_1_td_loss (its notes).

SB3 argument names

An SB3 argument Ajax does not have is refused with the closest Ajax names, or with where Ajax takes it (verbose, net_arch, ...). One name differs in meaning: SB3's learn(total_timesteps=N) is train(n_timesteps=N), and Ajax's total_timesteps is the horizon of a run split over several calls.

The calls side by side

In SB3 In Ajax Page
PPO("MlpPolicy", env) PPO(env_id), or a prebuilt JAX environment Training an agent
model.learn(total_timesteps=N) state, evaluations = agent.train(seed=[...], n_timesteps=N) Training an agent
one model per seed seed=[0, 1, 2]: every seed in one compiled program Performance
make_vec_env(env_id, n_envs=8) PPO(env_id, n_envs=8) Training an agent
VecNormalize(venv) normalize_observations=True, normalize_rewards=True (PPO's defaults; off by default elsewhere) Environments
policy_kwargs=dict(net_arch=[64, 64]) actor_architecture=("64", "tanh", "64", "tanh"), and critic_architecture: each width is followed by its activation Networks
learning_rate=3e-4 actor_learning_rate=3e-4, critic_learning_rate=3e-4, one per network, and alpha_learning_rate for SAC's temperature; DQN, PQN and CrossQ take one learning_rate Algorithms
EvalCallback(eval_env, eval_freq=...) nothing: train evaluates and returns the evaluations; LoggingConfig(log_frequency=...) sets how often Logging
model.learn(..., progress_bar=True) nothing: on a terminal or in a notebook, train draws a progress bar with the time left Quickstart
evaluate_policy(model, env, n_eval_episodes=10) agent.evaluate(state, episodes=10), one row of returns per seed Quickstart
tensorboard_log="runs" LoggingConfig(backends=(TensorBoardBackend("runs"),)), in a script under if __name__ == "__main__":; the viewer is pip install tensorboard Logging
model.predict(obs, deterministic=True) agent.predict(state, obs), or agent.policy(state) for a policy with memory Train, evaluate, deploy
model.save(path), PPO.load(path) agent.save(state, path), agent, state = PPO.load(path) Checkpoints
model.learn(..., reset_num_timesteps=False) agent.train(initial_state=state, ...) Training an agent
a Gymnasium environment "gymnasium:<id>", stepped in Python and slower Environments
RecurrentPPO (sb3-contrib) PPO(env_id, memory={"kind": "lstm", "hidden_size": 64}) Memory

Act with the trained agent

# doc: skip (Stable-Baselines3, for comparison)
action, _ = model.predict(obs, deterministic=True)
import gymnax
import jax

env, params = gymnax.make("CartPole-v1")
obs, _ = env.reset(jax.random.PRNGKey(0), params)

action = agent.predict(state, obs)  # the first seed's policy
print(action)

predict returns the action alone, where SB3's returns an (action, state) pair, and as a JAX array. A Gymnasium environment wants a NumPy value, so pass it through np.asarray:

import gymnasium
import numpy as np

gym_env = gymnasium.make("CartPole-v1")
gym_obs, _ = gym_env.reset(seed=0)
action = np.asarray(agent.predict(state, gym_obs))
gym_obs, reward, terminated, truncated, _ = gym_env.step(action)

predict acts as the evaluation does: the most likely action of a discrete policy, the mean action of a continuous one. An agent whose policy carries something between steps (a memory, a world model) refuses predict; agent.policy(state) gives a policy with reset and act instead (Train, evaluate, deploy).

Save and load

# doc: skip (Stable-Baselines3, for comparison)
model.save("ppo_cartpole")
model = PPO.load("ppo_cartpole")
import os
import tempfile

path = os.path.join(tempfile.mkdtemp(), "ppo_cartpole.pkl")
agent.save(state, path)

# Later, in another process.
agent, state = PPO.load(path)
print(agent.predict(state, obs))

save takes the state, and load returns two values, the agent and its state, where SB3's returns the model alone. The file records the agent's plain arguments; objects (a prebuilt environment, extensions) are passed to load again (Checkpoints).

What Ajax does not have

  • Callbacks. A training run is one compiled program, so no Python runs between its steps. What a callback would do goes elsewhere: evaluations come with train, changes to the training loop are extensions, and a run can be cut into several train calls with your own code between them (Continuing a run).
  • Fast training on a Python environment. A Gymnasium environment trains at the speed of Python (Gymnasium environments).
  • A2C, DDPG and HER. Algorithms lists what Ajax has, and Why Ajax says when SB3 fits better.

Next, the user guide starts with Training an agent: building an agent, the budget, seeds, sweeps and resuming.