REDQ¶
Randomized Ensembled Double Q-learning is SAC with many critics and many critic updates per environment step. Each critic target takes the minimum over a random subset of the target critics, which keeps over-estimation in check even at a high update-to-data ratio, and the actor maximises the ensemble's mean value.
Paper: Chen, Wang, Zhou and Ross, Randomized Ensembled Double
Q-Learning: Learning Fast Without a Model, ICLR 2021
(arXiv:2101.05982). Ajax follows its
Algorithm 1 and, where the authors' code is more specific, the code:
watchernyu/REDQ at 7b5d1bf.
When to use it¶
REDQ suits tasks where environment steps are scarce and compute is not: by default it makes 20 critic steps on an ensemble of 10 critics per environment step (with one environment), so it learns more from each sample than SAC, at far more compute per step.
| Feature | REDQ |
|---|---|
| Discrete actions | no |
| Continuous actions | yes, a tanh-squashed Gaussian, mapped onto the environment's bounds |
Memory (memory=) |
yes, on replayed sequences, without expert_policy or most extensions (Memory) |
| Environments | gymnax, Brax, MuJoCo Playground |
| Image observations | no |
| PID policy | yes, actor_architecture=PIDPolicy(...) (Networks) |
| Several seeds in one call | yes, train(seed=[...]) |
| Offline training | yes, train_offline (Training) |
| Extension phases | pretrain, post_update, eval_metrics, observe, on_target, critic_loss, actor_loss, action, eval_action, transform_action, actuate, choose_warmup_action, choose_replay_action, track_memory (which extensions run) |
| Resume and checkpoints | yes; a run split by a checkpoint equals the run left whole |
Example¶
# doc: skip (20 critic steps of 10 critics per update: minutes on a CPU)
from ajax import REDQ
from ajax.logging import LoggingConfig
# 10 critics, targets from the min over 2 of them, 20 critic steps per update.
agent = REDQ("Pendulum-v1", n_envs=4, learning_starts=1_000)
logging = LoggingConfig(log_frequency=2_000, backends=())
state, evaluations = agent.train(seed=[0, 1], n_timesteps=4_000, logging_config=logging)
print(evaluations["Eval/episodic mean reward"]) # one row per seed
An SVGD-style variant, which pushes the critics apart in function space,
is the KernelRepulsion extension, an Ajax research feature:
from ajax import REDQ
from ajax.extensions.ensemble import KernelRepulsion
agent = REDQ("Pendulum-v1", extensions=(KernelRepulsion(coef=0.1),))
Key hyperparameters¶
The defaults are the authors' code's, but for the target entropy (see Differences from the reference). The API section lists every argument.
| Argument | Default | What it does |
|---|---|---|
n_envs |
1 |
parallel environments |
actor_learning_rate, critic_learning_rate, alpha_learning_rate |
3e-4 |
Adam step sizes (epsilon 1e-8, torch's) |
actor_architecture, critic_architecture |
("256", "relu", "256", "relu") |
hidden layers |
gamma |
0.99 |
discount |
tau |
0.005 |
Polyak rate of the target critics |
buffer_size |
1_000_000 |
replay capacity |
batch_size |
None |
rows per update: 256, or 64 sequences with memory= |
learning_starts |
5_000 |
steps of uniform random actions before the first update |
num_critics |
10 |
critics in the ensemble |
subset_size |
2 |
target critics the target takes the minimum over |
num_critic_updates |
20 |
critic steps per update, each on a fresh batch |
alpha_init |
1.0 |
initial entropy weight |
target_entropy_per_dim |
-1.0 |
target entropy per action dimension; "mbpo" for MBPO's per-task value |
reward_scale |
1.0 |
multiplies the rewards in the target |
max_grad_norm |
None |
gradient clipping |
normalize_observations, normalize_rewards |
False |
running normalisation (Environments) |
memory, burn_in, sequence_length, stored_state |
None, 0, 64, False |
recurrent memory and its sequence replay (Memory) |
episode_length, action_repeat |
None, 1 |
simulator steps per episode and per agent step, on Brax and Playground; None is 1,000 steps, or a mujoco: task's own time limit (Environments) |
expert_policy |
None |
an expert the run tracks; each evaluation logs its return next to the agent's (Eval/expert bias) |
extensions |
() |
research features (Extensions) |
Cloning an expert into the actor before training is the CloningPretrain
extension; a cloning term during training is ImitationLoss
(Expert guidance).
Presets¶
REDQ.preset(task, env_id) sets MBPO's target entropy for the task, the
authors' code's default (redq/algos/core.py:11-13;
Presets), as target_entropy_per_dim: the
whole action's value over the task's action dimensions.
| Preset | Whole action | Action dimensions | target_entropy_per_dim |
|---|---|---|---|
Hopper |
-1 | 3 | -1/3 |
HalfCheetah |
-3 | 6 | -0.5 |
Walker2d |
-3 | 6 | -0.5 |
Ant |
-4 | 8 | -0.5 |
Humanoid |
-2 | 17 | -2/17 |
Differences from the reference¶
- Target entropy. The default is
-1per action dimension, the authors' code's"auto"option (redq_sac.py:55-56), since its own default, MBPO's, is defined for five tasks only.target_entropy_per_dim="mbpo"takes MBPO's value instead for the Brax idshopper,halfcheetah,walker2d,antandhumanoid, and raises on any other environment; the presets give the same values by task. - Where the code departs from the paper, Ajax follows the code, in four
choices. The actor step reads the critics as they were
before the last critic step, since the code computes the actor loss
before that step's optimiser update. The critic loss is the sum over the
critics of each one's mean squared error.
alpha_init=1.0matches the code'slog_alphastarting at 0. And the code then readsalpha=0.2until its first temperature step (its constructor sets the temperature twice), so the first update's critic targets and actor loss read 0.2 in Ajax too.
Notes¶
- One update.
num_critic_updatescritic steps, each on a fresh replay batch and each followed by a target update; then one actor step and one temperature step on the last batch. - Update-to-data ratio. Each iteration steps every environment once,
then makes one update, so the ratio of critic steps to environment steps
is
num_critic_updates / n_envs: 20 with the defaults. - Initialisation. As in the authors' code, every layer of the actor
and the critics, the mean and log-std layers included, starts
Xavier-uniform with a zero bias, each critic of the ensemble drawn on its
own. With
memory=, REDQ builds Ni et al. (2022)'s recurrent networks and their initialisation, by analogy with their SAC. - Actions and evaluation. As SAC; evaluation uses the mean action. The
log-probability subtracts
log(1 - tanh(u)^2 + 1e-6), as the authors' code does (core.py:232). The critics read actions in the environment's units, as the authors' critics do (act_limit * tanh,core.py:238). - Time limits. A transition cut by a time limit bootstraps on its final observation; only a termination stops the bootstrap.
- Diagnostics. REDQ logs how spread out its ensemble is
(
ensemble_q_std,mean_pairwise_q_dist), computed without gradient.
Results¶
The repository records no learning curves for REDQ yet. A conformance
check (tests/conformance/test_redq_original.py) runs one training step
of Ajax's REDQ and of watchernyu/REDQ from the same state, with the same
batches, subsets and noises, and compares every critic and target, the
actor, the temperature and their optimisers, the first step (temperature
0.2) included. The probes (tests/probing) train it briefly on tiny
environments with known answers: its targets, discounting, time limits,
memory, extension phases, and a run split by a checkpoint matching the run
left whole. Performance compares its speed with
the other agents'.
API¶
ajax.agents.REDQ.REDQ.REDQ
dataclass
¶
REDQ(
env_id,
*,
n_envs=1,
env_params=None,
weight_decay=0.0,
episode_length=None,
action_repeat=1,
extensions=(),
buffer_size=int(1000000.0),
batch_size=None,
learning_starts=5000,
train_frequency=1,
memory=None,
burn_in=0,
sequence_length=NI_WINDOW_STEPS,
stored_state=False,
share_encoder=None,
expert_policy=None,
eval_expert_policy=None,
normalize_rewards=False,
reward_normalization_gamma=None,
normalize_observations=False,
observation_clip=None,
max_grad_norm=None,
policy_update_start=0,
alpha_learning_rate=0.0003,
alpha_init=1.0,
target_entropy_per_dim=-1.0,
alpha_update_start=0,
actor_learning_rate=0.0003,
critic_learning_rate=0.0003,
actor_architecture=("256", "relu", "256", "relu"),
critic_architecture=("256", "relu", "256", "relu"),
gamma=0.99,
tau=0.005,
reward_scale=1.0,
num_critic_updates=20,
num_critics=10,
subset_size=2,
)
Bases: EntropyTemperature, PolicyUpdateStart, GradientClipping, ObservationNormalization, RewardNormalization, ExpertGuided, EncoderSharing, RecurrentReplayAgent
REDQ: an ensemble of num_critics SAC critics, each target the minimum over subset_size of them drawn at random, num_critic_updates critic steps per env step (the module's update).
Defaults are REDQ's official code's (watchernyu/REDQ@7b5d1bf): ten critics, a subset of two, 20 critic steps per env step (utd_ratio), Adam at 3e-4 with torch's eps 1e-8 (redq_sac.py:48, :51), xavier-uniform layers, a log-probability subtracting log(1 - tanh^2 + 1e-6) (core.py:232). One default is not its default: the target entropy is its "auto" option, -1 per action dimension (redq_sac.py:55-56). Its default, "mbpo" (redq_sac.py:33, :57-58), is a table of five tasks (core.py:11-13) that raises on any other env, so it is the option "mbpo" and the presets here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha_init
|
float
|
The temperature's initial value: 1.0 as in REDQ's official code, which learns log_alpha from 0 (redq_sac.py:59-61). Its self.alpha = alpha at :80 then sets the temperature to the unused alpha=0.2 default until its first temperature step (:214-236), on the last of the 20 critic updates (utd_ratio) of the first env step that trains: those updates' targets and the first actor loss read 0.2, and so do Ajax's (FIRST_ALPHA). |
1.0
|
target_entropy_per_dim
|
Union[Schedule, Literal['mbpo']]
|
The target entropy per action dimension (-1, the official code's "auto"), a schedule of the timestep, or "mbpo" for MBPO's per-task target entropy (MBPO_TARGET_ENTROPY), the default of REDQ's official code; an env id outside that table raises. |
-1.0
|
num_critic_updates
|
int
|
The critic steps of each update, each on a fresh replay batch (SAC's same-named steps share one). |
20
|
subset_size
|
int
|
The random target critics whose minimum each target reads. |
2
|
target_entropy
property
¶
MBPO's target entropy for the env (target_entropy_per_dim "mbpo"), else the shared rule's: per dimension times the policy's action width.
init ¶
A squashed Gaussian actor, num_critics Q critics with their targets, and the temperature.
update ¶
num_critic_updates critic and target steps, each on a fresh replay batch, then one actor step and one temperature step on the last batch (Chen et al., 2021, Algorithm 1), each kept from its start.
The actor step reads the critics from before the last critic step, as REDQ's official code, which computes the actor loss (redq_sac.py:220) before the critics' optimizers step (:242); Algorithm 1 reads them after. The critic and actor steps read read_temperature's temperature; the temperature step updates the learned one.
update_critic ¶
One step of the ensemble on the random-subset target.
update_actor ¶
The actor step on alpha log pi(a|s) - mean_i Q_i(s, a), the critics scoring a residual policy's executed actions; also returns its sample's log-probs, which the temperature step reads.