DQN Extensions
local graph
Chapter 8 — DQN Extensions
Section titled “Chapter 8 — DQN Extensions”Since DeepMind published its paper on the deep Q-network (DQN) model in 2015, many improvements have been proposed, along with tweaks to the basic architecture, which, significantly, have improved the convergence, stability, and sample efficiency of DeepMind’s basic DQN. In this chapter, we will take a deeper look at some of those ideas.
In October 2017, Hessel et al. from DeepMind published a paper called Rainbow: Combining improvements in deep reinforcement learning [Hes+18], which presented the six most important improvements to DQN; some were invented in 2015, but others are relatively recent. In this paper, state-of-the-art results on the Atari games suite were reached, just by combining those six methods.
Since 2017, more papers have been published and state-of-the-art results have been pushed further, but all the methods presented in the paper are still relevant and widely used in practice. For example, in 2023, Marc Bellemare published the book Distributional reinforcement learning [BDR23] about one of the paper’s methods. In addition, the improvements described are relatively simple to implement and understand, so I have not made any major modifications to this chapter in this edition.
The DQN extensions that we will become familiar with are the following:
- N-step DQN: How to improve convergence speed and stability with a simple unrolling of the Bellman equation, and why it’s not an ultimate solution
- Double DQN: How to deal with DQN overestimation of the values of the actions
- Noisy networks: How to make exploration more efficient by adding noise to the network weights
- Prioritized replay buffer: Why uniform sampling of our experience is not the best way to train
- Dueling DQN: How to improve convergence speed by making our network’s architecture more closely represent the problem that we are solving
- Categorical DQN: How to go beyond the single expected value of the action and work with full distributions
This chapter will go through all these methods. We will analyze the ideas behind them, alongside how they can be implemented and compared to the classic DQN performance. Finally, we will analyze how the combined system with all the methods performs.
Basic DQN
Section titled “Basic DQN”To get started, we will implement the same DQN method as in Chapter 6, but leveraging the high-level primitives described in Chapter 7. This will make our code much more compact, which is good, as non-relevant details won’t distract us from the method’s logic. At the same time, the purpose of this book is not to teach you how to use the existing libraries but rather how to develop intuition about RL methods and, if necessary, implement everything from scratch. From my perspective, this is a much more valuable skill, as libraries come and go, but true understanding of the domain will allow you to quickly make sense of other people’s code and apply it consciously.
In the basic DQN implementation, we have three modules in the Chapter08 folder of the GitHub repository for this book:
- Chapter08/lib/dqn_model.py: The DQN neural network (NN), which is the same as in Chapter 6, so I won’t repeat it
- Chapter08/lib/common.py: Common functions and declarations shared by the code in this chapter
- Chapter08/01_dqn_basic.py: 77 lines of code leveraging the PTAN and Ignite libraries, implementing the basic DQN method
Common library
Section titled “Common library”Let’s start with the contents of lib/common.py. First of all, we have hyperparameters for our Pong environment from the previous chapter. The hyperparameters are stored in the dataclass object, which is a standard way to store a bunch of data fields with their type annotations. This makes it easy to add another configuration set for different, more complicated Atari games and allows us to experiment with hyperparameters:
@dataclasses.dataclassclass Hyperparams: env_name: str stop_reward: float run_name: str replay_size: int replay_initial: int target_net_sync: int epsilon_frames: int
learning_rate: float = 0.0001 batch_size: int = 32 gamma: float = 0.99 epsilon_start: float = 1.0 epsilon_final: float = 0.1
tuner_mode: bool = False episodes_to_solve: int = 500
GAME_PARAMS = { ’pong’: Hyperparams( env_name="PongNoFrameskip-v4", stop_reward=18.0, run_name="pong", replay_size=100_000, replay_initial=10_000, target_net_sync=1000, epsilon_frames=100_000, epsilon_final=0.02, ),The next function from lib/common.py has the name unpack_batch, and it takes the batch, of transitions and converts it into the set of NumPy arrays suitable for training. Every transition from ExperienceSourceFirstLast has a type of ExperienceFirstLast, which is a dataclass with the following fields:
- state: Observation from the environment.
- action: Integer action taken by the agent.
- reward: If we have created ExperienceSourceFirstLast with the attribute steps_count=1, it’s just the immediate reward. For larger step counts, it contains the discounted sum of rewards for this number of steps.
- last_state: If the transition corresponds to the final step in the environment, then this field is None; otherwise, it contains the last observation in the experience chain.
The code of unpack_batch is as follows:
def unpack_batch(batch: tt.List[ExperienceFirstLast]): states, actions, rewards, dones, last_states = [],[],[],[],[] for exp in batch: states.append(exp.state) actions.append(exp.action) rewards.append(exp.reward) dones.append(exp.last_state is None) if exp.last_state is None: lstate = exp.state # the result will be masked anyway else: lstate = exp.last_state last_states.append(lstate) return np.asarray(states), np.array(actions), np.array(rewards, dtype=np.float32), \ np.array(dones, dtype=bool), np.asarray(last_states)Note how we handle the final transitions in the batch. To avoid the special handling of such cases, for terminal transitions, we store the initial state in the last_states array. To make our calculations of the Bellman update correct, we have to mask such batch entries during the loss calculation using the dones array. Another solution would be to calculate the value of the last states only for non-terminal transitions, but it would make our loss function logic a bit more complicated.
Calculation of the DQN loss function is provided by the calc_loss_dqn function, and the code is almost the same as in Chapter 6. One small addition is torch.no_grad(), which stops the PyTorch calculation graph from being recorded for the target net:
def calc_loss_dqn( batch: tt.List[ExperienceFirstLast], net: nn.Module, tgt_net: nn.Module, gamma: float, device: torch.device) -> torch.Tensor: states, actions, rewards, dones, next_states = unpack_batch(batch)
states_v = torch.as_tensor(states).to(device) next_states_v = torch.as_tensor(next_states).to(device) actions_v = torch.tensor(actions).to(device) rewards_v = torch.tensor(rewards).to(device) done_mask = torch.BoolTensor(dones).to(device)
actions_v = actions_v.unsqueeze(-1) state_action_vals = net(states_v).gather(1, actions_v) state_action_vals = state_action_vals.squeeze(-1) with torch.no_grad(): next_state_vals = tgt_net(next_states_v).max(1)[0] next_state_vals[done_mask] = 0.0
bellman_vals = next_state_vals.detach() * gamma + rewards_v return nn.MSELoss()(state_action_vals, bellman_vals)Besides those core DQN functions, common.py provides several utilities related to our training loop, data generation, and TensorBoard tracking. The first such utility is a small class that implements epsilon decay during the training. Epsilon defines the probability of taking the random action by the agent. It should be decayed from 1.0 in the beginning (fully random agent) to some small number, like 0.02 or 0.01. The code is trivial but is needed in almost any DQN, so it is provided by the following little class:
class EpsilonTracker: def __init__(self, selector: EpsilonGreedyActionSelector, params: Hyperparams): self.selector = selector self.params = params self.frame(0)
def frame(self, frame_idx: int): eps = self.params.epsilon_start - frame_idx / self.params.epsilon_frames self.selector.epsilon = max(self.params.epsilon_final, eps)Another small function is batch_generator, which takes ExperienceReplayBuffer (the PTAN class described in Chapter 7) and infinitely generates training batches sampled from the buffer. In the beginning, the function ensures that the buffer contains the required amount of samples:
def batch_generator(buffer: ExperienceReplayBuffer, initial: int, batch_size: int) -> \ tt.Generator[tt.List[ExperienceFirstLast], None, None]: buffer.populate(initial) while True: buffer.populate(1) yield buffer.sample(batch_size)Finally, a lengthy, but nevertheless very useful, function called setup_ignite attaches the needed Ignite handlers, showing the training progress and writing metrics to TensorBoard. Let’s look at this function piece by piece:
def setup_ignite( engine: Engine, params: Hyperparams, exp_source: ExperienceSourceFirstLast, run_name: str, extra_metrics: tt.Iterable[str] = (), tuner_reward_episode: int = 100, tuner_reward_min: float = -19,): handler = ptan_ignite.EndOfEpisodeHandler( exp_source, bound_avg_reward=params.stop_reward) handler.attach(engine) ptan_ignite.EpisodeFPSHandler().attach(engine)Initially, setup_ignite attaches two Ignite handlers provided by PTAN:
- EndOfEpisodeHandler, which emits the Ignite event every time a game episode ends. It can also fire an event when the averaged reward for episodes crosses some boundary. We use this to detect when the game is finally solved.
- EpisodeFPSHandler, a small class that tracks the time the episode has taken and the amount of interactions that we have had with the environment. From this, we calculate frames per second (FPS), which is an important performance metric to track.
Then, we install two event handlers:
@engine.on(ptan_ignite.EpisodeEvents.EPISODE_COMPLETED)def episode_completed(trainer: Engine): passed = trainer.state.metrics.get(’time_passed’, 0) print("Episode %d: reward=%.0f, steps=%s, speed=%.1f f/s, elapsed=%s" % ( trainer.state.episode, trainer.state.episode_reward, trainer.state.episode_steps, trainer.state.metrics.get(’avg_fps’, 0), timedelta(seconds=int(passed))))
@engine.on(ptan_ignite.EpisodeEvents.BOUND_REWARD_REACHED)def game_solved(trainer: Engine): passed = trainer.state.metrics[’time_passed’] print("Game solved in %s, after %d episodes and %d iterations!" % ( timedelta(seconds=int(passed)), trainer.state.episode, trainer.state.iteration)) trainer.should_terminate = True trainer.state.solved = TrueOne of the event handlers is called at the end of an episode. It will show information about the completed episode on the console. Another function will be called when the average reward grows above the boundary defined in the hyperparameters (18.0 in the case of Pong). This function shows a message about the solved game and stops the training.
The rest of the function is related to the TensorBoard data that we want to track. First, we create a TensorboardLogger:
now = datetime.now().isoformat(timespec=’minutes’).replace(’:’, ’’)logdir = f"runs/{now}-{params.run_name}-{run_name}"tb = tb_logger.TensorboardLogger(log_dir=logdir)run_avg = RunningAverage(output_transform=lambda v: v[’loss’])run_avg.attach(engine, "avg_loss")This is a special class provided by Ignite to write into TensorBoard. Our processing function will return the loss value, so we attach the RunningAverage transformation (also provided by Ignite) to get a smoothed version of the loss over time.
Next, we attach the metrics we want to track to the Ignite events:
metrics = [’reward’, ’steps’, ’avg_reward’]handler = tb_logger.OutputHandler(tag="episodes", metric_names=metrics)event = ptan_ignite.EpisodeEvents.EPISODE_COMPLETEDtb.attach(engine, log_handler=handler, event_name=event)TensorboardLogger can track two groups of values from Ignite: outputs (values returned by the transformation function) and metrics (calculated during the training and kept in the engine state). EndOfEpisodeHandler and EpisodeFPSHandler provide metrics, which are updated at the end of every game episode. So, we attach OutputHandler, which will write into TensorBoard information about the episode every time it is completed.
Next, we track another group of values, metrics from the training process: loss, FPS, and, possibly, some custom metrics relevant to the specific extension’s logic:
ptan_ignite.PeriodicEvents().attach(engine)metrics = [’avg_loss’, ’avg_fps’]metrics.extend(extra_metrics)handler = tb_logger.OutputHandler(tag="train", metric_names=metrics, output_transform=lambda a: a)event = ptan_ignite.PeriodEvents.ITERS_100_COMPLETEDtb.attach(engine, log_handler=handler, event_name=event)Those values are updated every training iteration, but we are going to do millions of iterations, so we will store values in TensorBoard every 100 training iterations; otherwise, the data files will be huge. All this functionality might look too complicated, but it provides us with the unified set of metrics gathered from the training process. In fact, Ignite is not very tricky, given the flexibility it provides. That’s it for common.py.
Implementation
Section titled “Implementation”Now, let’s take a look at 01_dqn_basic.py, which creates the needed classes and starts the training. I’m going to omit non-relevant code and focus only on important pieces (the full version is available in the GitHub repo). First, we create the environment:
env = gym.make(params.env_name)env = ptan.common.wrappers.wrap_dqn(env)
net = dqn_model.DQN(env.observation_space.shape, env.action_space.n).to(device)tgt_net = ptan.agent.TargetNet(net)Here, we apply a set of standard wrappers. We discussed them in Chapter 6 and will also touch upon them in the next chapter, when we optimize the performance of the Pong solver. Then, we create the DQN model and the target network.
Next, we create the agent, passing it an epsilon-greedy action selector:
selector = ptan.actions.EpsilonGreedyActionSelector(epsilon=params.epsilon_start)epsilon_tracker = common.EpsilonTracker(selector, params)agent = ptan.agent.DQNAgent(net, selector, device=device)During the training, epsilon will be decreased by the EpsilonTracker class that we have already discussed. This will decrease the amount of randomly selected actions and give more control to our NN.
The next two very important objects are ExperienceSourceFirstLast and ExperienceReplayBuffer:
exp_source = ptan.experience.ExperienceSourceFirstLast( env, agent, gamma=params.gamma, env_seed=common.SEED)buffer = ptan.experience.ExperienceReplayBuffer( exp_source, buffer_size=params.replay_size)ExperienceSourceFirstLast takes the agent and environment and provides transitions over game episodes. Those transitions will be kept in the experience replay buffer.
Then we create an optimizer and define the processing function:
optimizer = optim.Adam(net.parameters(), lr=params.learning_rate)
def process_batch(engine, batch): optimizer.zero_grad() loss_v = common.calc_loss_dqn(batch, net, tgt_net.target_model, gamma=params.gamma, device=device) loss_v.backward() optimizer.step() epsilon_tracker.frame(engine.state.iteration) if engine.state.iteration % params.target_net_sync == 0: tgt_net.sync() return { "loss": loss_v.item(), "epsilon": selector.epsilon, }The processing function will be called for every batch of transitions to train the model. To do this, we call the common.calc_loss_dqn function and then backpropagate on the result. This function also asks EpsilonTracker to decrease the epsilon and does periodical target network synchronization.
And, finally, we create the Ignite Engine object:
engine = Engine(process_batch)common.setup_ignite(engine, params, exp_source, NAME)engine.run(common.batch_generator(buffer, params.replay_initial, params.batch_size))We configure it using a function from common.py, and run our training process.
Hyperparameter tuning
Section titled “Hyperparameter tuning”To make our comparison of DQN extensions fair, we also need to tune hyperparameters. This is essential because even for the same game (Pong), using the fixed set of training parameters might give less optimal results when we change the details of the method.
In principle, every explicit or implicit constant in our code could be tuned, such as:
- Network configuration: Amount and size of layers, activation function, dropout, etc.
- Optimization parameters: Method (vanilla SGD, Adam, AdaGrad, etc.), learning rate, and other optimizer parameters
- Exploration parameters: Decay rate of 𝜖, final 𝜖 value
- Discount factor γ in Bellman equation
But every new parameter we tune has a multiplicative effect on the amount of trial training we need to perform, so having too many hyperparameters might require hundreds or even thousands of trainings. Large companies like Google and Meta have access to a much larger amount of GPUs than individual researchers like us, so we need to keep the balance there.
In my case, I’m going to demonstrate how hyperparameter tuning is done in general, but we’ll do the search only on a few values:
- Learning rate
- Discount factor γ
- Parameters specific to the DQN extension we’re considering
There are several libraries that might be helpful with hyperparameter tuning. Here, I’m using Ray Tune (https://docs.ray.io/en/latest/tune/index.html), which is a part of the Ray project — a distributed computing framework for ML and DL. At a high level, you need to define:
- The hyperparameter space you want to explore (boundaries for values to sample from or an explicit list of values to try)
- The function that performs the training with specific values of hyperparameters and returns the metric you want to optimize with the tuning
This might look very similar to ML problems, and in fact it is — this is also an optimization problem. But there are substantial differences: the function we’re optimizing is not differentiable (so you cannot perform the gradient descent to push your hyperparameters towards the desired direction of the metric) and the optimization space might be discrete (you cannot train the network with the number of layers equal to 2.435, for example, since we cannot take the derivative of a non-smooth function).
In later chapters, we’ll touch on this problem slightly in the context of black-box optimization methods (Chapter 17) and RL in discrete optimizations (Chapter 21), but for now, we’ll use the simplest approach — a random search of hyperparameters. In this case, the ray.tune library randomly samples concrete parameters several times and calls the function to obtain the metric. The smallest (or highest) metric corresponds to the best hyperparameter combination found in this run.
In this chapter, our metric (optimization objective) will be the number of games the agent needs to play before solving the game (reaching a mean score of greater than 18 for Pong).
To illustrate the effect of tuning, for every DQN extension, we check the training dynamics using a fixed set of parameters (the same as in Chapter 6), and the dynamics using the best hyperparameters found after 20-30 rounds of tuning. If you wish, you can do your own experiments, optimizing more hyperparameters. Most likely, this will allow you to find a better configuration for the training.
The core of the process is implemented in the common.tune_params function. Let’s take a look at its code. We start with the type declaration and hyperparameter space:
TrainFunc = tt.Callable[ [Hyperparams, torch.device, dict], tt.Optional[int]]
BASE_SPACE = { "learning_rate": tune.loguniform(1e-5, 1e-4), "gamma": tune.choice([0.9, 0.92, 0.95, 0.98, 0.99, 0.995]),}Here, we first define the type for the training function, which takes the Hyperparams dataclass torch.device to use and a dictionary with extra parameters (as some DQN extensions we’re going to present might require extra parameters besides those declared in Hyperparams).
The result of the function is either the int value, which will be the amount of games we played before reaching the score of 18, or None if we decided to stop the training early. This is required, as some hyperparameter combinations might fail to converge or converge too slowly, so to save time we stop the training without waiting for too long.
Then we define the hyperparameter search space — which is a dict with string keys (parameter name) and the tune declaration of possible values to explore. It could be a probability distribution (uniform, loguniform, normal, etc.) or an explicit list of values to try. You can also use tune.grid_search declaration with a list of values. In that case, all the values will be tried.
In our case, we sample the learning rate from the loguniform distribution and gamma from the list of 6 values ranging from 0.9 to 0.995.
Next, we have the tune_params function:
def tune_params( base_params: Hyperparams, train_func: TrainFunc, device: torch.device, samples: int = 10, extra_space: tt.Optional[tt.Dict[str, tt.Any]] = None,): search_space = dict(BASE_SPACE) if extra_space is not None: search_space.update(extra_space) config = tune.TuneConfig(num_samples=samples)
def objective(config: dict, device: torch.device) -> dict: keys = dataclasses.asdict(base_params).keys() upd = {"tuner_mode": True} for k, v in config.items(): if k in keys: upd[k] = v params = dataclasses.replace(base_params, **upd) res = train_func(params, device, config) return {"episodes": res if res is not None else 10**6}This function is given the following arguments:
- Basic set of hyperparameters that will be used for training
- Training function
- Torch device to use
- Amount of samples to perform during the round
- Additional dictionary with search space
Inside this function, we have an objective function, which creates the Hyperparameters object from the sampled dict, calls the training function, and returns the dictionary (which is a requirement of the ray.tune library).
The rest of the tune_params function is simple:
obj = tune.with_parameters(objective, device=device)if device.type == "cuda": obj = tune.with_resources(obj, {"gpu": 1})tuner = tune.Tuner(obj, param_space=search_space, tune_config=config)results = tuner.fit()best = results.get_best_result(metric="episodes", mode="min")print(best.config)print(best.metrics)Here, we wrap the objective function to pass the torch device and take into account GPU resources. This is needed to allow Ray to properly parallelize the tuning process. If you have multiple GPUs installed on the machine, it will run several trainings in parallel. Then, we just create the Tuner object and ask it to perform the hyperparameter search.
The final piece relevant to hyperparameter tuning is in the setup_ignite function. It checks for situations when the training process is not converging, so we stop the training to avoid infinite waiting. To do this, we install the Ignite event handler if we’re in the hyperparameter tuning mode:
if params.tuner_mode: @engine.on(ptan_ignite.EpisodeEvents.EPISODE_COMPLETED) def episode_completed(trainer: Engine): avg_reward = trainer.state.metrics.get(’avg_reward’) max_episodes = params.episodes_to_solve * 1.1 if trainer.state.episode > tuner_reward_episode and \ avg_reward < tuner_reward_min: trainer.should_terminate = True trainer.state.solved = False elif trainer.state.episode > max_episodes: trainer.should_terminate = True trainer.state.solved = False if trainer.should_terminate: print(f"Episode {trainer.state.episode}, " f"avg_reward {avg_reward:.2f}, terminating")Here, we check for two conditions:
- If the mean reward is lower than tuner_reward_min (which is an argument to the setup_ignite function and equal to -19 by default) after 100 games (provided in the tuner_reward_episode argument). This means that it’s quite unlikely that we’ll converge at all.
- We played more than max_episodes amount of games and still haven’t solved the game. In the default config, we set this limit to 500 games.
In both cases, we stop the training and set the solved attribute to False, which will return a high constant metric value in our tuning process.
That’s it for the hyperparameter tuning code. Before we run it and check the results, let’s first start a single training using the parameters we used in Chapter 6.
Results with common parameters
Section titled “Results with common parameters”If we run the training with the argument —params common, we’ll train the Pong game using hyperparameters from the common.py module. As an option, you can use the —params best command line to train on the best values for this particular DQN extension.
Okay, let’s start the training using the following command:
Chapter08$ ./01_dqn_basic.py --dev cuda --params commonA.L.E: Arcade Learning Environment (version 0.8.1+53f58b7)[Powered by Stella]Episode 1: reward=-21, steps=848, speed=0.0 f/s, elapsed=0:00:11Episode 2: reward=-21, steps=850, speed=0.0 f/s, elapsed=0:00:11Episode 3: reward=-19, steps=1039, speed=0.0 f/s, elapsed=0:00:11Episode 4: reward=-21, steps=884, speed=0.0 f/s, elapsed=0:00:11Episode 5: reward=-19, steps=1146, speed=0.0 f/s, elapsed=0:00:11Episode 6: reward=-20, steps=997, speed=0.0 f/s, elapsed=0:00:11Episode 7: reward=-21, steps=972, speed=0.0 f/s, elapsed=0:00:11Episode 8: reward=-21, steps=882, speed=0.0 f/s, elapsed=0:00:11Episode 9: reward=-21, steps=898, speed=0.0 f/s, elapsed=0:00:11Episode 10: reward=-20, steps=947, speed=0.0 f/s, elapsed=0:00:11Episode 11: reward=-21, steps=762, speed=227.7 f/s, elapsed=0:00:12Episode 12: reward=-20, steps=991, speed=227.8 f/s, elapsed=0:00:17Episode 13: reward=-21, steps=762, speed=227.9 f/s, elapsed=0:00:20Episode 14: reward=-20, steps=948, speed=227.9 f/s, elapsed=0:00:24Episode 15: reward=-20, steps=992, speed=228.0 f/s, elapsed=0:00:28......Every line in the output is written at the end of the game episode, showing the episode reward, a count of steps, the speed, and the total training time. For the basic DQN version and common hyperparameters, it usually takes about 700K frames and about 400 games to reach the mean reward of 18, so be patient. During the training, we can check the dynamics of the training process in TensorBoard, which shows charts for epsilon, raw reward values, average reward, and speed. The following charts show the reward and the number of steps for episodes (the bottom x axis shows the wall clock time, and the top axis is the episode number):

Figure 8.1: Plots with reward (left) and count of steps per episode (right)

Figure 8.2: Plots with training speed (left) and average training loss (right)
It is also worth noting how the count of steps per episode changes during the training. Initially, it increases, as our network starts winning more and more games, but after a certain level, the count of steps decreases 2x and stays almost constant. This is driven by our γ parameter, which discounts the agent’s reward over time, so it tries not just to accumulate as much of a reward as possible, but also to do it efficiently.
Tuned baseline DQN
Section titled “Tuned baseline DQN”After running the baseline DQN with the command-line argument —tune 30 (which took about a day on one GPU), I was able to find the following parameters, which solves Pong in 340 episodes (instead of 360):
learning_rate=9.932831968547505e-05,gamma=0.98,As you can see, the learning rate is almost the same as before (10 −4), but gamma is lower (0.98 versus 0.99). This might be an indication that Pong has relatively short subtrajectories with action-reward causality, so decreasing the γ has a stabilizing effect on the training.
In the following figure, you can see a comparison of the reward and steps per episode for both tuned and untuned versions (and the difference is quite minor):

Figure 8.3: Plots with reward (left) and count of steps per episode (right) for tuned and untuned hyperparameters
Now we have our baseline DQN version and are ready to explore method modifications proposed by Hessel et al.
N-step DQN
Section titled “N-step DQN”The first improvement that we will implement and evaluate is quite an old one. It was first introduced by Sutton in the paper Learning to Predict by the Methods of Temporal Differences [Sut88]. To get the idea, let’s look at the Bellman update used in Q-learning once again:
![π (a |s) = P[At = a|St = s]](https://static.packt-cdn.com/products/9781835882702/graphics/Images/eq26.png)
This equation is recursive, which means that we can express Q(s t+1,a t+1) in terms of itself, which gives us this result:
![π (a |s) = P[At = a|St = s]](https://static.packt-cdn.com/products/9781835882702/graphics/Images/eq27.png)
Value r a,t+1 means local reward at time t + 1, after issuing action a. However, if we assume that action a at step t + 1 was chosen optimally, or close to optimally, we can omit the max a operation and obtain this:
![π (a |s) = P[At = a|St = s]](https://static.packt-cdn.com/products/9781835882702/graphics/Images/eq28.png)
This value can be unrolled again and again any number of times. As you may guess, this unrolling can be easily applied to our DQN update by replacing one-step transition sampling with longer transition sequences of n-steps. To understand why this unrolling will help us to speed up training, let’s consider the example illustrated in Figure 8.4. Here, we have a simple environment of four states (s 1, s 2, s 3, s 4) and the only action available at every state, except s 4, which is a terminal state:

Figure 8.4: A transition diagram for a simple environment
So, what happens in a one-step case? We have three total updates possible (we don’t use max, as there is only one action available):
- Q(s 1,a) ←r 1 + γQ(s 2,a)
- Q(s 2,a) ←r 2 + γQ(s 3,a)
- Q(s 3,a) ←r 3
Let’s imagine that, at the beginning of the training, we complete the preceding updates in this order. The first two updates will be useless, as our current Q(s 2,a) and Q(s 3,a) are incorrect and contain initial random values. The only useful update will be update 3, which will correctly assign reward r 3 to the state s 3 prior to the terminal state.
Now let’s perform the updates over and over again. On the second iteration, the correct value will be assigned to Q(s 2,a), but the update of Q(s 1,a) will still be noisy. Only on the third iteration will we get the valid values for every Q. So, even in a one-step case, it takes three steps to propagate the correct values to all the states.
Now let’s consider a two-step case. This situation again has three updates:
- Q(s 1,a) ←r 1 + γr 2 + γ 2 Q(s 3,a)
- Q(s 2,a) ←r 2 + γr 3
- Q(s 3,a) ←r 3
In this case, on the first loop over the updates, the correct values will be assigned to both Q(s 2,a) and Q(s 3,a). On the second iteration, the value of Q(s 1,a) will also be properly updated. So, multiple steps improve the propagation speed of values, which improves convergence. You may be thinking, “If it’s so helpful, let’s unroll the Bellman equation, say, 100 steps ahead. Will it speed up our convergence 100 times?” Unfortunately, the answer is no. Despite our expectations, our DQN will fail to converge at all.
To understand why, let’s again return to our unrolling process, especially where we dropped the max a. Was it correct? Strictly speaking, no. We omitted the max operation at the intermediate step, assuming that our action selection during experience gathering (or our policy) was optimal. What if it wasn’t, for example, at the beginning of the training, when our agent acted randomly? In that case, our calculated value for Q(s t,a t) may be smaller than the optimal value of the state (as some steps have been taken randomly, but not following the most promising paths by maximizing the Q-value). The more steps on which we unroll the Bellman equation, the more incorrect our update could be.
Our large experience replay buffer will make the situation even worse, as it will increase the chance of getting transitions obtained from the old bad policy (dictated by old bad approximations of Q). This will lead to a wrong update of the current Q approximation, so it can easily break our training progress. This problem is a fundamental characteristic of RL methods, as was briefly mentioned in Chapter 4, when we talked about RL methods’ taxonomy.
There are two large classes of methods:
- Off-policy methods: The first class of off-policy methods doesn’t depend on the “freshness of data.” For example, a simple DQN is off-policy, which means that we can use very old data sampled from the environment several million steps ago, and this data will still be useful for learning. That’s because we are just updating the value of the action, Q(s t,a t), with the immediate reward, plus the discounted current approximation of the best action’s value. Even if action a t was sampled randomly, it doesn’t matter because for this particular action a t, in the state s t, our update will be correct. That’s why in off-policy methods, we can use a very large experience buffer to make our data closer to being independent and identically distributed (iid).
- On-policy methods: On the other hand, on-policy methods heavily depend on the training data to be sampled according to the current policy that we are updating. That happens because on-policy methods try to improve the current policy indirectly (as in the previous n-step DQN) or directly (all of Part 3 of the book is devoted to such methods).
So, which class of methods is better? Well, it depends. Off-policy methods allow you to train on the previous large history of data or even on human demonstrations, but they usually are slower to converge. On-policy methods are typically faster, but require much more fresh data from the environment, which can be costly. Just imagine a self-driving car trained with the on-policy method. It will cost you a lot of crashed cars before the system learns that walls and trees are things that it should avoid!
You may have a question: why are we talking about an n-step DQN if this “n-stepness” turns it into an on-policy method, which will make our large experience buffer useless? In practice, this is usually not black and white. You may still use an n-step DQN if it will help to speed up DQNs, but you need to be modest with the selection of n. Small values of two or three usually work well, because our trajectories in the experience buffer are not that different from one-step transitions. In such cases, convergence speed usually improves proportionally, but large values of n can break the training process. So, the number of steps should be tuned, but convergence speeding up usually makes it worth doing.
Implementation
Section titled “Implementation”As the ExperienceSourceFirstLast class already supports the multi-step Bellman unroll, our n-step version of a DQN is extremely simple. There are only two modifications that we need to make to the basic DQN to turn it into an n-step version:
- Pass the count of steps that we want to unroll on ExperienceSourceFirstLast creation in the steps_count parameter.
- Pass the correct gamma to the calc_loss_dqn function. This modification is really easy to overlook, which could be harmful to convergence. As our Bellman is now n-steps, the discount coefficient for the last state in the experience chain will no longer be just γ, but γ n.
You can find the whole example in Chapter08/02_dqn_n_steps.py, with only the modified lines shown here:
exp_source = ptan.experience.ExperienceSourceFirstLast( env, agent, gamma=params.gamma, env_seed=common.SEED, steps_count=n_steps)The n_steps value is a count of steps passed in command-line arguments; the default is to use four steps.
Another modification is in gamma passed to the calc_loss_dqn function:
loss_v = common.calc_loss_dqn( batch, net, tgt_net.target_model, gamma=params.gamma**n_steps, device=device)Results
Section titled “Results”The training module Chapter08/02_dqn_n_steps.py can be started as before, with the additional command-line option -n, which gives a count of steps to unroll the Bellman equation. These are charts for our baseline and n-step DQN (using a common set of parameters), with n being equal to 2 and 3. As you can see, the Bellman unroll has given a significant convergence speedup:

Figure 8.5: The reward and number of steps for basic (one-step) DQN and n-step versions
As you can see in the diagram, the three-step DQN converges significantly faster than the simple DQN, which is a nice improvement. So, what about a larger n? Figure 8.6 shows the reward dynamics for n = 3…6:

Figure 8.6: Reward dynamics for cases with n = 3…6 with common hyperparameters
As you can see, going from three steps to four has given some improvement, but it is much less than before. The variant with n = 5 is worse and very close to n = 2. The same is true for n = 6. So, in our case, n = 3 looks optimal.
Hyperparameter tuning
Section titled “Hyperparameter tuning”In this extension, hyperparameter tuning was done individually for every n from 2 to 7. The following table shows the best parameters and number of games they require to solve the game:
| n | Learning rate | γ | Games |
|---|---|---|---|
| 2 | 3.97 ⋅ 10 −5 | 0.98 | 293 |
| 3 | 7.82 ⋅ 10 −5 | 0.98 | 260 |
| 4 | 6.07 ⋅ 10 −5 | 0.98 | 290 |
| 5 | 7.52 ⋅ 10 −5 | 0.99 | 268 |
| 6 | 6.78 ⋅ 10 −5 | 0.995 | 261 |
| 7 | 8.59 ⋅ 10 −5 | 0.98 | 284 |
Table 8.1: The best hyperparameters (learning rate and gamma) for every n
This table also confirms the conclusions of the untuned version comparison — unrolling the Bellman equation for two and three steps improves the convergence, but a further increase of n produces worse results. n = 6 gives us a comparable result to n = 3, but the outcomes for n = 4 and n = 5 are worse, so we should stop at n = 3.
Figure 8.7 compares the training dynamics of tuned versions of the baseline and N-step DQN with n = 2 and n = 3.

Figure 8.7: The reward and number of steps after hyperparameter tuning
Double DQN
Section titled “Double DQN”The next fruitful idea on how to improve a basic DQN came from DeepMind researchers in the paper titled Deep reinforcement learning with double Q-learning [VGS16]. In the paper, the authors demonstrated that the basic DQN tends to overestimate values for Q, which may be harmful to training performance and sometimes can lead to suboptimal policies. The root cause of this is the max operation in the Bellman equation, but the strict proof is a bit complicated (you can find the full explanation in the paper). As a solution to this problem, the authors proposed modifying the Bellman update a bit.
In the basic DQN, our target value for Q looked like this:
![π (a |s) = P[At = a|St = s]](https://static.packt-cdn.com/products/9781835882702/graphics/Images/eq29.png)
Q′(s t+1,a) was Q-values calculated using our target network, the weights of which are copied from the trained network every n steps. The authors of the paper proposed choosing actions for the next state using the trained network, but taking values of Q from the target network. So, the new expression for target Q-values will look like this:
![π (a |s) = P[At = a|St = s]](https://static.packt-cdn.com/products/9781835882702/graphics/Images/eq30.png)
The authors proved that this simple tweak fixes overestimation completely, and they called this new architecture double DQN.
Implementation
Section titled “Implementation”The core implementation is very simple. What we need to do is slightly modify our loss function. But let’s go a step further and compare action values produced by basic DQN and double DQN. According to the paper author’s our baseline DQN should have consistently higher values predicted for the same states than the double DQN version. To do this, we store a random held-out set of states and periodically calculate the mean value of the best action for every state in the evaluation set.
The complete example is in Chapter08/03_dqn_double.py. Let’s first take a look at the loss function:
def calc_loss_double_dqn( batch: tt.List[ptan.experience.ExperienceFirstLast], net: nn.Module, tgt_net: nn.Module, gamma: float, device: torch.device): states, actions, rewards, dones, next_states = common.unpack_batch(batch)
states_v = torch.as_tensor(states).to(device) actions_v = torch.tensor(actions).to(device) rewards_v = torch.tensor(rewards).to(device) done_mask = torch.BoolTensor(dones).to(device)We will use this function instead of common.calc_loss_dqn and they both share lots of code. The main difference is in the next Q-values estimation:
actions_v = actions_v.unsqueeze(-1)state_action_vals = net(states_v).gather(1, actions_v)state_action_vals = state_action_vals.squeeze(-1)with torch.no_grad(): next_states_v = torch.as_tensor(next_states).to(device) next_state_acts = net(next_states_v).max(1)[1] next_state_acts = next_state_acts.unsqueeze(-1) next_state_vals = tgt_net(next_states_v).gather(1, next_state_acts).squeeze(-1) next_state_vals[done_mask] = 0.0 exp_sa_vals = next_state_vals.detach() * gamma + rewards_vreturn nn.MSELoss()(state_action_vals, exp_sa_vals)The preceding code snippet calculates the loss in a slightly different way. In the double DQN version, we calculate the best action to take in the next state using our main trained network, but values corresponding to this action come from the target network.
This part could be implemented in a faster way, by combining next_states_v with states_v and calling our main network only once, but it will make the code less clear.
The rest of the function is the same: we mask completed episodes and compute the mean squared error (MSE) loss between Q-values predicted by the network and approximated Q-values.
The last function that we consider calculates the values of our held-out state:
@torch.no_grad()def calc_values_of_states(states: np.ndarray, net: nn.Module, device: torch.device): mean_vals = [] for batch in np.array_split(states, 64): states_v = torch.tensor(batch).to(device) action_values_v = net(states_v) best_action_values_v = action_values_v.max(1)[0] mean_vals.append(best_action_values_v.mean().item()) return np.mean(mean_vals)There is nothing complicated here: we just split our held-out states array into equal chunks and pass every chunk to the network to obtain action values. From those values, we choose the action with the largest value (for every state) and calculate the mean of such values. As our array with states is fixed for the whole training process, and this array is large enough (in the code, we store 1,000 states), we can compare the dynamics of this mean value in both DQN variants. The rest of the 03_dqn_double.py file is almost the same; the two differences are usage of our tweaked loss function and keeping a randomly sampled 1,000 states for periodical evaluation. This happens in the process_batch function:
if engine.state.iteration % EVAL_EVERY_FRAME == 0: eval_states = getattr(engine.state, "eval_states", None) if eval_states is None: eval_states = buffer.sample(STATES_TO_EVALUATE) eval_states = [ np.asarray(transition.state) for transition in eval_states ] eval_states = np.asarray(eval_states) engine.state.eval_states = eval_states engine.state.metrics["values"] = \ common.calc_values_of_states(eval_states, net, device)Results
Section titled “Results”My experiments show that with common hyperparameters, double DQN has a negative effect on reward dynamics. Sometimes, double DQN leads to better initial dynamics and the trained agent learns how to win more games faster, but reaching the end reward boundary takes longer. You can perform your own experiment on other games or try parameters from the original paper.
The following are reward charts from the experiment where double DQN was a bit better than the baseline version:

Figure 8.8: Reward dynamics for double and baseline DQN
Besides the standard metrics, the example also outputs the mean value for the held-out set of states, which are shown in Figure 8.9.

Figure 8.9: Values predicted by the network for held-out states
As you can see, the basic DQN does an overestimation of values, so values decrease after a certain level. In contrast, the double DQN grows more consistently. In my experiments, the double DQN has only a small effect on the training time, but this doesn’t necessarily mean that the double DQN is useless, as Pong is a simple environment. In more complicated games, the double DQN could give better results.
Hyperparameter tuning
Section titled “Hyperparameter tuning”The tuning of hyperparameters also wasn’t very successful for the double DQN. After 30 trials, the best values for the learning rate and gamma were able to solve Pong in 412 games, which is worse than the baseline DQN.
Noisy networks
Section titled “Noisy networks”The next improvement that we are going to look at addresses another RL problem: exploration of the environment. The paper that we will draw from is called Noisy networks for exploration [For+17] and it has a very simple idea for learning exploration characteristics during training instead of having a separate schedule related to exploration.
A classical DQN achieves exploration by choosing random actions with a specially defined hyperparameter 𝜖, which is slowly decreased over time from 1.0 (fully random actions) to some small ratio of 0.1 or 0.02. This process works well for simple environments with short episodes, without much non-stationarity during the game; but even in such simple cases, it requires tuning to make the training processes efficient.
In the Noisy Networks paper, the authors proposed a quite simple solution that, nevertheless, works well. They add noise to the weights of fully connected layers of the network and adjust the parameters of this noise during training using backpropagation.
This method shouldn’t be confused with “the network decides where to explore more,” which is a much more complex approach that also has widespread support (for example, see articles about intrinsic motivation and count-based exploration methods [Ost+17], [Mar+17]). We will discuss advanced exploration techniques in Chapter 21.
The authors proposed two ways of adding the noise, both of which work according to their experiments, but they have different computational overheads:
- Independent Gaussian noise: For every weight in a fully connected layer, we have a random value that we draw from the normal distribution. Parameters of the noise, μ and σ, are stored inside the layer and get trained using backpropagation in the same way that we train weights of the standard linear layer. The output of such a “noisy layer” is calculated in the same way as in a linear layer.
- Factorized Gaussian noise: To minimize the number of random values to be sampled, the authors proposed keeping only two random vectors: one with the size of the input and another with the size of the output of the layer. Then, a random matrix for the layer is created by calculating the outer product of the vectors.
Implementation
Section titled “Implementation”In PyTorch, both methods can be easily implemented in a very straightforward way. What we need to do is create our own custom nn.Linear layer with weights calculated as w i,j = μ i,j + σ i,j ⋅𝜖 i,j, where μ and σ are trainable parameters and 𝜖 ∼𝒩(0,1) is random noise sampled from the normal distribution after every optimization step.
Previous editions of the book used my implementation of both methods, but now we’ll simply use the implementation from the popular TorchRL library I mentioned in Chapter 7. Let’s take a look at relevant parts of the implementation (the full code can be found in torchrl/modules/models/exploration.py in the TorchRL repository). The following is the constructor of the NoisyLinear class, which creates all the parameters we need to optimize:
class NoisyLinear(nn.Linear): def __init__( self, in_features: int, out_features: int, bias: bool = True, device: Optional[DEVICE_TYPING] = None, dtype: Optional[torch.dtype] = None, std_init: float = 0.1, ): nn.Module.__init__(self) self.in_features = int(in_features) self.out_features = int(out_features) self.std_init = std_init
self.weight_mu = nn.Parameter( torch.empty(out_features, in_features, device=device, dtype=dtype, requires_grad=True) ) self.weight_sigma = nn.Parameter( torch.empty(out_features, in_features, device=device, dtype=dtype, requires_grad=True) ) self.register_buffer( "weight_epsilon", torch.empty(out_features, in_features, device=device, dtype=dtype), ) if bias: self.bias_mu = nn.Parameter( torch.empty(out_features, device=device, dtype=dtype, requires_grad=True) ) self.bias_sigma = nn.Parameter( torch.empty(out_features, device=device, dtype=dtype, requires_grad=True) ) self.register_buffer( "bias_epsilon", torch.empty(out_features, device=device, dtype=dtype), ) else: self.bias_mu = None self.reset_parameters() self.reset_noise()In the constructor, we create matrices for μ and σ. This implementation inherits from torch.nn.Linear, but calls the nn.Module.__init__() method, so normal Linear weights and bias buffers are not created.
To make new matrices trainable, we need to wrap their tensors in an nn.Parameter. The register_buffer method creates a tensor in the network that won’t be updated during backpropagation, but will be handled by the nn.Module machinery (for example, it will be copied to the GPU with the cuda() call). An extra parameter and buffer are created for the bias of the layer. At the end, we call the reset_parameters() and reset_noise() methods, which perform the initialization of the created trainable parameters and the buffer with the epsilon value.
In the following three methods, we initialize the trainable parameters μ and σ according to the paper:
def reset_parameters(self) -> None: mu_range = 1 / math.sqrt(self.in_features) self.weight_mu.data.uniform_(-mu_range, mu_range) self.weight_sigma.data.fill_(self.std_init / math.sqrt(self.in_features)) if self.bias_mu is not None: self.bias_mu.data.uniform_(-mu_range, mu_range) self.bias_sigma.data.fill_(self.std_init / math.sqrt(self.out_features))
def reset_noise(self) -> None: epsilon_in = self._scale_noise(self.in_features) epsilon_out = self._scale_noise(self.out_features) self.weight_epsilon.copy_(epsilon_out.outer(epsilon_in)) if self.bias_mu is not None: self.bias_epsilon.copy_(epsilon_out)
def _scale_noise( self, size: Union[int, torch.Size, Sequence]) -> torch.Tensor: if isinstance(size, int): size = (size,) x = torch.randn(*size, device=self.weight_mu.device) return x.sign().mul_(x.abs().sqrt_())The matrix for μ is initialized with uniform random values. The initial value for σ is constant depending on the count of neurons in the layer.
For the noise initialization, factorized Gaussian noise is used – we sample two random vectors and calculate the outer product to get the matrix for 𝜖. The outer product is a linear algebra operation when two vectors of the same size are producing the square matrix filled with product of all combination of each vector’s element. The rest is simple: we redefine the weight and bias properties, which are expected in nn.Linear layer, so NoisyLinear could be used everywhere nn.Linear is used:
@propertydef weight(self) -> torch.Tensor: if self.training: return self.weight_mu + self.weight_sigma * self.weight_epsilon else: return self.weight_mu
@propertydef bias(self) -> Optional[torch.Tensor]: if self.bias_mu is not None: if self.training: return self.bias_mu + self.bias_sigma * self.bias_epsilon else: return self.bias_mu else: return NoneThis implementation is simple, but has one very subtle nuance — the 𝜖 values are not updated after every optimization step (and it is not mentioned in the documentation). This issue is already reported in the TorchRL repo, but for the current stable release, we have to call the reset_noise() method explicitly. Hopefully, it will be fixed and the NoisyLinear layer will update the noise automatically.
From the implementation point of view, that’s it. What we now need to do to turn the classic DQN into a noisy network variant is just replace nn.Linear (which are the two last layers in our DQN network) with the NoisyLinear layer. Of course, you have to remove all the code related to the epsilon-greedy strategy.
To check the internal noise level during training, we can monitor the signal-to-noise ratio (SNR) of our noisy layers, which is RMS(μ)∕RMS(σ), where RMS is the root mean square of the corresponding weights. In our case, the SNR shows how many times the stationary component of the noisy layer is larger than the injected noise.
Results
Section titled “Results”After the training, the TensorBoard charts show much better training dynamics. The model was able to reach the mean score of 18 after 250 games, which is an improvement in comparison to 350 for the baseline DQN. But because of extra operations required for noisy networks, their training is a bit slower (194 FPS versus 240 FPS for the baseline), so, time-wise, the difference is less impressive. But still, the results look good:

Figure 8.10: Noisy networks compared to the baseline DQN
After checking the SNR chart (Figure 8.11), you may notice that both layers’ noise levels have decreased very quickly.

Figure 8.11: SNR change in layer 1 (left) and layer 2 (right)
The first layer has gone from to almost ratio of noise. The second layer is even more interesting, as its noise level decreased from in the beginning to, but after 450K frames (roughly the same time as when raw rewards climbed close to the 20 score), the level of noise in the last layer started to increase again, pushing the agent to explore the environment more. This makes a lot of sense, as after reaching high score levels, the agent basically knows how to play at a good level, but still needs to “polish” its actions to improve the results even more.
Hyperparameter tuning
Section titled “Hyperparameter tuning”After the tuning, the best set of parameters was able to solve the game after 273 rounds, which is an improvement over the baseline:
learning_rate=7.142520950425814e-05,gamma=0.99,The following are charts comparing the reward dynamics and steps for tuned baseline DQN and tuned noisy networks:

Figure 8.12: Comparison of tuned baseline DQN and tuned noisy network
On both charts, we see improvements introduced by noisy networks: it takes fewer games to reach a score of 21 and during the training, games have a smaller amount of steps.
Prioritized replay buffer
Section titled “Prioritized replay buffer”The next very useful idea on how to improve DQN training was proposed in 2015 in the paper Prioritized experience replay [Sch+15]. This method tries to improve the efficiency of samples in the replay buffer by prioritizing those samples according to the training loss.
The basic DQN used the replay buffer to break the correlation between immediate transitions in our episodes. As we discussed in Chapter 6, the examples we experience during the episode will be highly correlated, as most of the time, the environment is ”smooth” and doesn’t change much according to our actions. However, the stochastic gradient descent (SGD) method assumes that the data we use for training has an iid property. To solve this problem, the classic DQN method uses a large buffer of transitions, randomly and uniformly sampled to get the next training batch.
The authors of the paper questioned this uniform random sample policy and proved that by assigning priorities to buffer samples, according to training loss and sampling the buffer proportional to those priorities, we can significantly improve convergence and the policy quality of the DQN. This method’s basic idea could be explained as “train more on data that surprises you.” The tricky point here is to keep the balance of training on an “unusual” sample and training on the rest of the buffer. If we focus only on a small subset of the buffer, we can lose our i.i.d. property and simply overfit on this subset.
From the mathematical point of view, the priority of every sample in the buffer is calculated as, where p i is the priority of the i-th sample in the buffer and α is the number that shows how much emphasis we give to the priority. If α = 0, our sampling will become uniform as in the classic DQN method. Larger values for α put more stress on samples with higher priority. So, it’s another hyperparameter to tune, and the starting value of α proposed by the paper is 0.6.
There were several options proposed in the paper for how to define the priority, and the most popular is to make it proportional to the loss for this particular example in the Bellman update. New samples added to the buffer need to be assigned a maximum value of priority to be sure that they will be sampled soon.
By adjusting the priorities for the samples, we are introducing bias into our data distribution (we sample some transitions much more frequently than others), which we need to compensate for if SGD is to work. To get this result, the authors of the study used sample weights, which needed to be multiplied by the individual sample loss. The value of the weight for each sample is defined as w i = (N ⋅P(i)) −β, where β is another hyperparameter that should be between 0 and 1.
With β = 1, the bias introduced by the sampling is fully compensated for, but the authors showed that it’s good for convergence to start with β between 0 and 1 and slowly increase it to 1 during the training.
Implementation
Section titled “Implementation”To implement this method, we have to introduce certain changes in our code:
- First of all, we need a new replay buffer that will track priorities, sample a batch according to them, calculate weights, and let us update priorities after the loss has become known.
- The second change will be the loss function itself. Now we not only need to incorporate weights for every sample, but we need to pass loss values back to the replay buffer to adjust the priorities of the sampled transitions.
In the main module, Chapter08/05_dqn_prio_replay.py, we have all those changes implemented. For the sake of simplicity, the new priority replay buffer class uses a very similar storage scheme to our previous replay buffer. Unfortunately, new requirements for prioritization make it impossible to implement sampling in 𝒪(1) time (in other words, sampling time will grow with an increase in buffer size). If we are using simple lists, every time that we sample a new batch, we need to process all the priorities, which makes our sampling have 𝒪(N) time complexity in proportion to the buffer size. It’s not a big deal if our buffer is small, such as 100k samples, but may become an issue for real-life large buffers of millions of transitions. There are other storage schemes that support efficient sampling in 𝒪(log N) time, for example, using the segment tree data structure. There are different versions of such optimized buffers available in various libraries – for example, in TorchRL.
The PTAN library also provides an efficient prioritized replay buffer in the class ptan.experience.PrioritizedReplayBuffer. You can update the example to use the more efficient version and check the effect on training performance.
But, for now, let’s take a look at the naïve version, whose source code you will find in lib/dqn_extra.py.
In the beginning, we define parameters for the β increase rate:
BETA_START = 0.4BETA_FRAMES = 100_000Our beta will be changed from 0.4 to 1.0 during the first 100k frames.
Next comes the prioritized replay buffer class:
class PrioReplayBuffer(ExperienceReplayBuffer): def __init__(self, exp_source: ExperienceSource, buf_size: int, prob_alpha: float = 0.6): super().__init__(exp_source, buf_size) self.experience_source_iter = iter(exp_source) self.capacity = buf_size self.pos = 0 self.buffer = [] self.prob_alpha = prob_alpha self.priorities = np.zeros((buf_size, ), dtype=np.float32) self.beta = BETA_STARTThe class for the priority replay buffer inherits from the simple replay buffer in PTAN, which stores samples in a circular buffer (it allows us to keep a fixed amount of entries without reallocating the list). Our subclass uses a NumPy array to keep priorities.
The update_beta() method needs to be called periodically to increase beta according to a schedule. The populate() method needs to pull the given number of transitions from the ExperienceSource object and store them in the buffer:
def update_beta(self, idx: int) -> float: v = BETA_START + idx * (1.0 - BETA_START) / BETA_FRAMES self.beta = min(1.0, v) return self.beta
def populate(self, count: int): max_prio = self.priorities.max(initial=1.0) for _ in range(count): sample = next(self.experience_source_iter) if len(self.buffer) < self.capacity: self.buffer.append(sample) else: self.buffer[self.pos] = sample self.priorities[self.pos] = max_prio self.pos = (self.pos + 1) % self.capacityAs our storage for the transitions is implemented as a circular buffer, we have two different situations with this buffer:
- When our buffer hasn’t reached the maximum capacity, we just need to append a new transition to the buffer.
- If the buffer is already full, we need to overwrite the oldest transition, which is tracked by the pos class field, and adjust this position modulo buffer’s size.
In the sample method, we need to convert priorities to probabilities using our α hyperparameter:
def sample(self, batch_size: int) -> tt.Tuple[ tt.List[ExperienceFirstLast], np.ndarray, np.ndarray]: if len(self.buffer) == self.capacity: prios = self.priorities else: prios = self.priorities[:self.pos] probs = prios ** self.prob_alpha probs /= probs.sum()Then, using those probabilities, we sample our buffer to obtain a batch of samples:
indices = np.random.choice(len(self.buffer), batch_size, p=probs)samples = [self.buffer[idx] for idx in indices]As the last step, we calculate weights for samples in the batch:
total = len(self.buffer)weights = (total * probs[indices]) ** (-self.beta)weights /= weights.max()return samples, indices, np.array(weights, dtype=np.float32)This returns three objects: the batch, indices, and weights. Indices for batch samples are required to update priorities for sampled items.
The last function of the priority replay buffer allows us to update new priorities for the processed batch:
def update_priorities(self, batch_indices: np.ndarray, batch_priorities: np.ndarray): for idx, prio in zip(batch_indices, batch_priorities): self.priorities[idx] = prioIt’s the responsibility of the caller to use this function with the calculated losses for the batch.
The next custom function that we have in our example is the loss calculation. As the MSELoss class in PyTorch doesn’t support weights (which is understandable, as MSE is loss used in regression problems, but weighting of the samples is commonly utilized in classification losses), we need to calculate the MSE and explicitly multiply the result on the weights:
def calc_loss(batch: tt.List[ExperienceFirstLast], batch_weights: np.ndarray, net: nn.Module, tgt_net: nn.Module, gamma: float, device: torch.device) -> tt.Tuple[torch.Tensor, np.ndarray]: states, actions, rewards, dones, next_states = common.unpack_batch(batch)
states_v = torch.as_tensor(states).to(device) actions_v = torch.tensor(actions).to(device) rewards_v = torch.tensor(rewards).to(device) done_mask = torch.BoolTensor(dones).to(device) batch_weights_v = torch.tensor(batch_weights).to(device)
actions_v = actions_v.unsqueeze(-1) state_action_vals = net(states_v).gather(1, actions_v) state_action_vals = state_action_vals.squeeze(-1) with torch.no_grad(): next_states_v = torch.as_tensor(next_states).to(device) next_s_vals = tgt_net(next_states_v).max(1)[0] next_s_vals[done_mask] = 0.0 exp_sa_vals = next_s_vals.detach() * gamma + rewards_v l = (state_action_vals - exp_sa_vals) ** 2 losses_v = batch_weights_v * l return losses_v.mean(), (losses_v + 1e-5).data.cpu().numpy()In the last part of the loss calculation, we implement the same MSE loss but write our expression explicitly, rather than using the library. This allows us to take into account the weights of samples and keep individual loss values for every sample. Those values will be passed to the priority replay buffer to update priorities. A small value is added to every loss to handle the situation of zero loss value, which will lead to zero priority for an entry in the replay buffer.
In the main section of our program, we have only two updates: the creation of the replay buffer and our processing function. Buffer creation is straightforward, so we will only take a look at a new processing function:
def process_batch(engine, batch_data): batch, batch_indices, batch_weights = batch_data optimizer.zero_grad() loss_v, sample_prios = calc_loss( batch, batch_weights, net, tgt_net.target_model, gamma=params.gamma, device=device) loss_v.backward() optimizer.step() buffer.update_priorities(batch_indices, sample_prios) epsilon_tracker.frame(engine.state.iteration) if engine.state.iteration % params.target_net_sync == 0: tgt_net.sync() return { "loss": loss_v.item(), "epsilon": selector.epsilon, "beta": buffer.update_beta(engine.state.iteration), }There are several changes here:
- Our batch now contains three entities: the batch of data, indices of sampled items, and samples’ weights.
- We call our new loss function, which accepts weights and returns the additional items’ priorities. They are passed to the buffer.update_priorities() function to reprioritize items that we have sampled.
- We call the update_beta() method of the buffer to change the beta parameter according to the schedule.
Results
Section titled “Results”This example can be trained as usual. According to my experiments, the prioritized replay buffer took almost the same absolute time to solve the environment: almost an hour. But it took fewer training iterations and fewer episodes. So, wall clock time is the same mostly due to the less efficient replay buffer, which, of course, could be resolved by proper 𝒪(log N) implementation of the buffer.
Here is the comparison of reward dynamics of the baseline and prioritized replay buffer (right). The x axis is the game episodes:

Figure 8.13: Reward dynamics for prioritized replay buffer in comparison to basic DQN
Another difference to note on the TensorBoard charts is a much lower loss for the prioritized replay buffer. The following chart shows the comparison:

Figure 8.14: The comparison of loss during the training
Lower loss value is also expected and is a good sign that our implementation works. The idea of prioritization is to train more on samples with high loss value, so training becomes more efficient. But there is a danger here: loss value during the training is not the primary objective to optimize; we can have very low loss, but due to a lack of exploration, the final policy learned could be far from being optimal.
Hyperparameter tuning
Section titled “Hyperparameter tuning”Hyperparameter tuning for the prioritized replay buffer was done with an additional parameter for α, which was sampled from a fixed list of values ranging from 0.3 to 0.9 (with steps of 0.1). The best combination was able to solve Pong after 330 episodes and had α = 0.6 (the same as in the paper):
learning_rate=8.839010139505506e-05,gamma=0.99,The following are charts comparing the tuned baseline DQN with the tuned prioritized replay buffer:

Figure 8.15: Comparison of tuned baseline DQN and tuned prioritized replay buffer
Here, we see the prioritized replay buffer had faster gameplay improvement, but it took almost the same amount of games to reach score 21. On the right chart (with the amount of game steps), the prioritized replay buffer was also a bit better.
Dueling DQN
Section titled “Dueling DQN”This improvement to DQN was proposed in 2015, in the paper called Dueling network architectures for deep reinforcement learning [Wan+16]. The core observation of this paper is that the Q-values, Q(s,a), that our network is trying to approximate can be divided into quantities: the value of the state, V (s), and the advantage of actions in this state, A(s,a).
You have seen the quantity V (s) before, as it was the core of the value iteration method from Chapter 5. It is just equal to the discounted expected reward achievable from this state. The advantage A(s,a) is supposed to bridge the gap from V (s) to Q(s,a), as, by definition, Q(s,a) = V (s) + A(s,a). In other words, the advantage A(s,a) is just the delta, saying how much extra reward some particular action from the state brings us. The advantage could be positive or negative and, in general, could have any magnitude. For example, at some tipping point, the choice of one action over another can cost us a lot of the total reward.
The Dueling paper’s contribution was an explicit separation of the value and the advantage in the network’s architecture, which brought better training stability, faster convergence, and better results on the Atari benchmark. The architecture difference from the classic DQN network is shown in the following illustration. The classic DQN network (top) takes features from the convolution layer and, using fully connected layers, transforms them into a vector of Q-values, one for each action. On the other hand, dueling DQN (bottom) takes convolution features and processes them using two independent paths: one path is responsible for V (s) prediction, which is just a single number, and another path predicts individual advantage values, having the same dimension as Q-values in the classic case. After that, we add V (s) to every value of A(s,a) to obtain Q(s,a), which is used and trained as normal. Figure 8.16 (from the paper) compares the basic DQN and dueling DQN:

Figure 8.16: A basic DQN (top) and dueling architecture (bottom)
These changes in the architecture are not enough to make sure that the network will learn V (s) and A(s,a) as we want it to. Nothing prevents the network, for example, from predicting some state, V (s) = 0, and A(s) = [1,2,3,4], which is completely wrong, as the predicted V (s) is not the expected value of the state. We have yet another constraint to set: we want the mean value of the advantage of any state to be zero. In that case, the correct prediction for the preceding example will be V (s) = 2.5 and A(s) = [−1.5,−0.5,0.5,1.5].
This constraint could be enforced in various ways, for example, via the loss function; but in the Dueling paper, the authors proposed the very elegant solution of subtracting the mean value of the advantage from the Q expression in the network, which effectively pulls the mean for the advantage to zero:
![π (a |s) = P[At = a|St = s]](https://static.packt-cdn.com/products/9781835882702/graphics/Images/eq36.png)
This keeps the changes that need to be made in the classic DQN very simple: to convert it to the double DQN, you need to change only the network architecture, without affecting other pieces of the implementation.
Implementation
Section titled “Implementation”The complete example is available in Chapter08/06_dqn_dueling.py. All the changes sit in the network architecture, so here, I’ll only show the network class (which is in the lib/dqn_extra.py module).
The convolution part is exactly the same as before:
class DuelingDQN(nn.Module): def __init__(self, input_shape: tt.Tuple[int, ...], n_actions: int): super(DuelingDQN, self).__init__()
self.conv = nn.Sequential( nn.Conv2d(input_shape[0], 32, kernel_size=8, stride=4), nn.ReLU(), nn.Conv2d(32, 64, kernel_size=4, stride=2), nn.ReLU(), nn.Conv2d(64, 64, kernel_size=3, stride=1), nn.ReLU(), nn.Flatten() )Instead of defining a single path of fully connected layers, we create two different transformations: one for advantages and one for value prediction:
size = self.conv(torch.zeros(1, *input_shape)).size()[-1]self.fc_adv = nn.Sequential( nn.Linear(size, 256), nn.ReLU(), nn.Linear(256, n_actions))self.fc_val = nn.Sequential( nn.Linear(size, 256), nn.ReLU(), nn.Linear(256, 1))Also, to keep the number of parameters in the model comparable to the original network, the inner dimension in both paths is decreased from 512 to 256. The changes in the forward() function are also very simple, thanks to PyTorch’s expressiveness:
def forward(self, x: torch.ByteTensor): adv, val = self.adv_val(x) return val + (adv - adv.mean(dim=1, keepdim=True))
def adv_val(self, x: torch.ByteTensor): xx = x / 255.0 conv_out = self.conv(xx) return self.fc_adv(conv_out), self.fc_val(conv_out)Here, we calculate the value and advantage for our batch of samples and add them together, subtracting the mean of the advantage to obtain the final Q-values. A subtle, but important, difference lies in calculating the mean along the second dimension of the tensor, which produces a vector of the mean advantage for every sample in our batch.
Results
Section titled “Results”After training a dueling DQN, we can compare it to the classic DQN convergence on our Pong benchmark. Dueling architecture has faster convergence in comparison to the basic DQN version:

Figure 8.17: The reward dynamic of dueling DQN compared to the baseline version
Our example also outputs the advantage and value for a fixed set of states, shown in the following charts. They meet our expectations: the advantage is not very different from zero, but the value improves over time (and resembles the value from the Double DQN section):

Figure 8.18: Mean advantage (left) and value (right) on a fixed set of states
Hyperparameter tuning
Section titled “Hyperparameter tuning”The tuning of the hyperparameters was not very fruitful. After 30 tuning iterations, there were no combinations of learning rate and gamma that were able to converge faster than the common set of parameters.
Categorical DQN
Section titled “Categorical DQN”The last, and the most complicated, method in our DQN improvements toolbox is from the paper published by DeepMind in June 2017, called A distributional perspective on reinforcement learning [BDM17]. Although this paper is a few years old now, it remains highly relevant, and active research is still ongoing in this area. The book Distributional reinforcement learning was published in 2023, where the same authors describe the method in greater detail [BDR23].
In the paper, the authors questioned the fundamental pieces of Q-learning — Q-values — and tried to replace them with a more generic Q-value probability distribution. Let’s try to understand the idea. Both the Q-learning and value iteration methods work with the values of the actions or states represented as simple numbers and showing how much total reward we can achieve from a state, or an action and a state. However, is it practical to squeeze all future possible rewards into one number? In complicated environments, the future could be stochastic, giving us different values with different probabilities.
For example, imagine the commuter scenario when you regularly drive from home to work. Most of the time, the traffic isn’t that heavy, and it takes you around 30 minutes to reach your destination. It’s not exactly 30 minutes, but on average it’s 30. From time to time, something happens, like road repairs or an accident, and due to traffic jams, it takes you three times longer to get to work. The probability of your commute time can be represented as a distribution of the “commute time” random variable, and it is shown in the following chart:

Figure 8.19: The probability distribution of commute time
Now, imagine that you have an alternative way to get to work: the train. It takes a bit longer, as you need to get from home to the train station and from the station to the office, but they are much more reliable than traveling by car (in some contries, like Germany, it might not be the case, but let’s consider Swiss trains for our example). Say, for instance, that the train commute time is 40 minutes on average, with a small chance of train disruption, which adds 20 minutes of extra time to the journey. The distribution of the train commute is shown in the following graph:

Figure 8.20: The probability distribution of train commute time
Imagine that now we want to make the decision on how to commute. If we know only the mean time for both car and train, a car looks more attractive, as on average it takes 35.43 minutes to travel, which is better than 40.54 minutes for the train.
However, if we look at full distributions, we may decide to go by train, as even in the worst-case scenario, it will be one hour of commuting versus one hour and 30 minutes. Switching to statistical language, the car distribution has much higher variance, so in situations when you really have to be at the office in 60 minutes max, the train is better.
The situation becomes even more complicated in the Markov decision process (MDP) scenario, when the sequence of decisions needs to be made and every decision might influence the future situation. In the commute example, it might be the time of an important meeting that you need to arrange given the way that you are going to commute. In that case, working with mean reward values might mean losing lots of information about the underlying environment dynamics.
Exactly the same idea was proposed by the authors of Distributional Perspective on Reinforcement Learning [9]. Why do we limit ourselves by trying to predict an average value for an action, when the underlying value may have a complicated underlying distribution? Maybe it will help us to work with distributions directly. The results presented in the paper show that, in fact, this idea could be helpful, but at the cost of introducing a more complicated method. I’m not going to put a strict mathematical definition here, but the overall idea is to predict the distribution of value for every action, similar to the distributions for our car/train example. As the next step, the authors showed that the Bellman equation can be generalized for a distribution case, and it will have the form Z(x,a)R(x,a) + γZ(x′,a′), which is very similar to the familiar Bellman equation, but now Z(x,a) and R(x,a) are the probability distributions and are not single numbers. The notation AB indicates eqality of distributions A and B.
The resulting distribution can be used to train our network to give better predictions of value distribution for every action of the given state, exactly in the same way as with Q-learning. The only difference will be in the loss function, which now has to be replaced with something suitable for distribution comparison. There are several alternatives available, for example, Kullback-Leibler (KL) divergence (or cross-entropy loss), which is used in classification problems, or the Wasserstein metric. In the paper, the authors gave theoretical justification for the Wasserstein metric, but when they tried to apply it in practice, they faced limitations. So, in the end, the paper used KL divergence.
Implementation
Section titled “Implementation”As mentioned, the method is quite complex, so it took me a while to implement it and make sure it was working. The complete code is in Chapter08/07_dqn_distrib.py, which uses the distr_projection function in lib/dqn_extra.py to perform distribution projection. Before we check it, I need to say a few words about the implementation logic.
The central part of the method is the probability distribution, which we are approximating. There are lots of ways to represent the distribution, but the authors of the paper chose a quite generic parametric distribution, which is basically a fixed number of values placed regularly on a values range. The range of values should cover the range of possible accumulated discounted reward. In the paper, the authors did experiments with various numbers of atoms, but the best results were obtained with the range split on N_ATOMS=51 intervals in the range of values from Vmin=-10 to Vmax=10.
For every atom (we have 51 of them), our network predicts the probability that the future discounted value will fall into this atom’s range. The central part of the method is the code, which performs the contraction of distribution of the next state’s best action using gamma, adds local reward to the distribution, and projects the results back into our original atoms. This logic is implemented in the dqn_extra.distr_projection function. In the beginning, we allocate the array that will keep the result of the projection:
def distr_projection(next_distr: np.ndarray, rewards: np.ndarray, dones: np.ndarray, gamma: float): batch_size = len(rewards) proj_distr = np.zeros((batch_size, N_ATOMS), dtype=np.float32) delta_z = (Vmax - Vmin) / (N_ATOMS - 1)This function expects the batch of distributions with a shape (batch_size, N_ATOMS), the array of rewards, flags for completed episodes, and our hyperparameters: Vmin, Vmax, N_ATOMS, and gamma. The delta_z variable is the width of every atom in our value range.
In the following code, we iterate over every atom in the original distribution that we have and calculate the place that this atom will be projected to by the Bellman operator, taking into account our value bounds:
for atom in range(N_ATOMS): v = rewards + (Vmin + atom * delta_z) * gamma tz_j = np.minimum(Vmax, np.maximum(Vmin, v))For example, the very first atom, with index 0, corresponds with the value Vmin=-10, but for the sample with reward +1 will be projected into the value −10 ⋅ 0.99 + 1 = −8.9. In other words, it will be shifted to the right (assume gamma=0.99). If the value falls beyond our value range given by Vmin and Vmax, we clip it to the bounds.
In the next line, we calculate the atom numbers that our samples have projected:
b_j = (tz_j - Vmin) / delta_zOf course, samples can be projected between atoms. In such situations, we spread the value in the original distribution at the source atom between the two atoms that it falls between. This spreading should be carefully handled, as our target atom can land exactly at some atom’s position. In that case, we just need to add the source distribution value to the target atom.
The following code handles the situation when the projected atom lands exactly on the target atom. Otherwise, b_j won’t be the integer value and variables l and u (which correspond to the indices of atoms below and above the projected point):
l = np.floor(b_j).astype(np.int64)u = np.ceil(b_j).astype(np.int64)eq_mask = u == lproj_distr[eq_mask, l[eq_mask]] += next_distr[eq_mask, atom]When the projected point lands between atoms, we need to spread the probability of the source atom between the atoms below and above. This is carried out by two lines in the following code:
ne_mask = u != lproj_distr[ne_mask, l[ne_mask]] += next_distr[ne_mask, atom] * (u - b_j)[ne_mask]proj_distr[ne_mask, u[ne_mask]] += next_distr[ne_mask, atom] * (b_j - l)[ne_mask]Of course, we need to properly handle the final transitions of episodes. In that case, our projection shouldn’t take into account the next distribution and should just have a 1 probability corresponding to the reward obtained.
However, we again need to take into account our atoms and properly distribute this probability if the reward value falls between atoms. This case is handled by the following code branch, which zeroes the resulting distribution for samples with the done flag set and then calculates the resulting projection:
if dones.any(): proj_distr[dones] = 0.0 tz_j = np.minimum(Vmax, np.maximum(Vmin, rewards[dones])) b_j = (tz_j - Vmin) / delta_z l = np.floor(b_j).astype(np.int64) u = np.ceil(b_j).astype(np.int64) eq_mask = u == l eq_dones = dones.copy() eq_dones[dones] = eq_mask if eq_dones.any(): proj_distr[eq_dones, l[eq_mask]] = 1.0 ne_mask = u != l ne_dones = dones.copy() ne_dones[dones] = ne_mask if ne_dones.any(): proj_distr[ne_dones, l[ne_mask]] = (u - b_j)[ne_mask] proj_distr[ne_dones, u[ne_mask]] = (b_j - l)[ne_mask]return proj_distrTo give you an illustration of what this function does, let’s look at artificially made distributions processed by this function (Figure 8.21). I used them to debug the function and make sure that it worked as intended. The code for these checks is in Chapter08/adhoc/distr_test.py.

Figure 8.21: The sample of the probability distribution transformation applied to a normal distribution
The top chart of Figure 8.21 (named Source) is a normal distribution with μ = 0 and σ = 3. The second chart (named Projected) is obtained from distribution projection with γ = 0.9 and is shifted to the right with reward=2.
In the situation where we pass done=True with the same data, the result will be different and is shown in Figure 8.22. In such cases, the source distribution will be ignored completely, and the result will have only the reward projected.

Figure 8.22: The projection of distribution for the final step in the episode
The implementation of this method is in Chapter08/07_dqn_distrib.py, which has an optional command-line parameter, —img-path. If this option is given, it has to be a directory where plots with a probability distribution from a fixed set of states will be stored during the training. This is useful to monitor how the model converges from uniform probability in the beginning of the training to a more spiked weight of probability masses. Sample images from my experiments are shown in Figure 8.24 and Figure 8.25.
I’m going to show only essential pieces of the implementation here. The core of the method, the distr_projection function, was already covered, and it is the most complicated piece. What is still missing is the network architecture and modified loss function, which we will describe here.
Let’s start with the network, which is in lib/dqn_extra.py, in the DistributionalDQN class:
Vmax = 10Vmin = -10N_ATOMS = 51DELTA_Z = (Vmax - Vmin) / (N_ATOMS - 1)
class DistributionalDQN(nn.Module): def __init__(self, input_shape: tt.Tuple[int, ...], n_actions: int): super(DistributionalDQN, self).__init__()
self.conv = nn.Sequential( nn.Conv2d(input_shape[0], 32, kernel_size=8, stride=4), nn.ReLU(), nn.Conv2d(32, 64, kernel_size=4, stride=2), nn.ReLU(), nn.Conv2d(64, 64, kernel_size=3, stride=1), nn.ReLU(), nn.Flatten() ) size = self.conv(torch.zeros(1, *input_shape)).size()[-1] self.fc = nn.Sequential( nn.Linear(size, 512), nn.ReLU(), nn.Linear(512, n_actions * N_ATOMS) )
sups = torch.arange(Vmin, Vmax + DELTA_Z, DELTA_Z) self.register_buffer("supports", sups) self.softmax = nn.Softmax(dim=1)The main difference is the output of the fully connected layer. Now it outputs the vector of n_actions * N_ATOMS values, which is 6 × 51 = 306 for Pong. For every action, it needs to predict the probability distribution on 51 atoms. Every atom (called support) has a value, which corresponds to a particular reward. Those atoms’ rewards are evenly distributed from -10 to 10, which gives a grid with step 0.4. Those supports are stored in the network’s buffer.
The forward() method returns the predicted probability distribution as a 3D tensor (batch, actions, and supports):
def forward(self, x: torch.ByteTensor) -> torch.Tensor: batch_size = x.size()[0] xx = x / 255 fc_out = self.fc(self.conv(xx)) return fc_out.view(batch_size, -1, N_ATOMS)
def both(self, x: torch.ByteTensor) -> tt.Tuple[torch.Tensor, torch.Tensor]: cat_out = self(x) probs = self.apply_softmax(cat_out) weights = probs * self.supports res = weights.sum(dim=2) return cat_out, resBesides forward(), we define the both() method, which calculates the probability distribution for atoms and Q-values in one call.
The network also defines several helper functions to simplify the calculation of Q-values and apply softmax on the probability distribution:
def qvals(self, x: torch.ByteTensor) -> torch.Tensor: return self.both(x)[1]
def apply_softmax(self, t: torch.Tensor) -> torch.Tensor: return self.softmax(t.view(-1, N_ATOMS)).view(t.size())The final change is the new loss function that has to apply distribution projection instead of the Bellman equation, and calculate KL divergence between predicted and projected distributions:
def calc_loss(batch: tt.List[ExperienceFirstLast], net: dqn_extra.DistributionalDQN, tgt_net: dqn_extra.DistributionalDQN, gamma: float, device: torch.device) -> torch.Tensor: states, actions, rewards, dones, next_states = common.unpack_batch(batch) batch_size = len(batch)
states_v = torch.as_tensor(states).to(device) actions_v = torch.tensor(actions).to(device) next_states_v = torch.as_tensor(next_states).to(device)
# next state distribution next_distr_v, next_qvals_v = tgt_net.both(next_states_v) next_acts = next_qvals_v.max(1)[1].data.cpu().numpy() next_distr = tgt_net.apply_softmax(next_distr_v) next_distr = next_distr.data.cpu().numpy()
next_best_distr = next_distr[range(batch_size), next_acts] proj_distr = dqn_extra.distr_projection(next_best_distr, rewards, dones, gamma)
distr_v = net(states_v) sa_vals = distr_v[range(batch_size), actions_v.data] state_log_sm_v = F.log_softmax(sa_vals, dim=1) proj_distr_v = torch.tensor(proj_distr).to(device)
loss_v = -state_log_sm_v * proj_distr_v return loss_v.sum(dim=1).mean()The preceding code is not very complicated; it just prepares to call distr_projection and KL divergence, which is defined as:
![π (a |s) = P[At = a|St = s]](https://static.packt-cdn.com/products/9781835882702/graphics/Images/eq38.png)
To calculate the logarithm of probability, we use the PyTorch log_softmax function, which combines both log and softmax in a numerically stable way.
Results
Section titled “Results”From my experiments, the distributional version of DQN converged a bit slower and less stably than the original DQN, which is not surprising, as the network output is now 51 times larger and the loss function has changed. Without hyperparameter tuning (which will be described in the next subsection), the distributional version requires 20% more episodes to solve the game.
Another factor that might be important here is that Pong is just too simple a game to draw conclusions. In the A Distributional Perspective paper, the authors reported state-of-the-art scores (at the time of publishing in 2017) for more than half of the games from the Atari benchmark (Pong was not among them).
The following are charts comparing reward dynamics and loss for the distributional DQN. As you can see, the reward dynamics for the distributional method is worse than the baseline DQN:

Figure 8.23: Reward dynamics (left) and loss decrease (right)
It might be interesting to look into the dynamics of the probability distribution during the training. If you start the training with the —img-path parameter (providing the directory name), the training process will save plots with the probability distribution for a fixed set of states. For example, the following figure shows the probability distribution for all six actions for one state at the beginning of the training (after 30k frames):

Figure 8.24: Probability distribution at the beginning of training
All the distributions are very wide (as the network hasn’t converged yet), and the peak in the middle corresponds to the negative reward that the network expects to get from its actions. The same state after 500k frames of training is shown in the following figure:

Figure 8.25: Probability distribution produced by the trained network
Now we can see that different actions have different distributions. The first action (which corresponds to the NOOP, the do nothing action) has its distribution shifted to the left, so doing nothing in this state usually leads to losing. The fifth action, which is RIGHTFIRE, has the mean value shifted to the right, so this action leads to a better score.
Hyperparameter tuning
Section titled “Hyperparameter tuning”The tuning of hyperparameters was not very fruitful. After 30 tuning iterations, there were no combinations of learning rate and gamma that were able to converge faster than the common set of parameters.
Combining everything
Section titled “Combining everything”You have now seen all the DQN improvements mentioned in the paper Rainbow: Combining Improvements in Deep Reinforcement Learning, but it was done in an incremental way, which (I hope) was helpful to understand the idea and implementation of every improvement. The main point of the paper was to combine those improvements and check the results. In the final example, I’ve decided to exclude categorical DQN and double DQN from the final system, as they haven’t shown too much improvement on our guinea pig environment. If you want, you can add them and try using a different game. The complete example is available in Chapter08/08_dqn_rainbow.py.
First of all, we need to define our network architecture and the methods that have contributed to it:
- Dueling DQN: Our network will have two separate paths for the value of the state distribution and advantage distribution. On the output, both paths will be summed together, providing the final value probability distributions for actions. To force the advantage distribution to have a zero mean, we will subtract the distribution with the mean advantage in every atom.
- Noisy networks: Our linear layers in the value and advantage paths will be noisy variants of nn.Linear.
In addition to network architecture changes, we will use the prioritized replay buffer to keep environment transitions and sample them proportionally to the MSE loss.
Finally, we will unroll the Bellman equation to n-steps.
I’m not going to repeat all the code, as individual methods have already been given in the preceding sections, and it should be obvious what the final result of combining the methods will look like. If you have any trouble, you can find the code on GitHub.
Results
Section titled “Results”The following are charts comparing the smoothed reward and count of steps with the baseline DQN. In both, we can see significant improvement in terms of the amount of games played:

Figure 8.26: Comparison of baseline DQN with combined system
In addition to the averaged reward, it is worth checking the raw reward chart, which is even more dramatic than the smoothed reward. It shows that our system was able to jump from the negative outcome to the positive very quickly – after just 100 games, it won almost every game. So, it took us another 100 games to make the smoothed reward reach +18:

Figure 8.27: Raw reward for combined system
As a downside, the combined system is slower than the baseline, as we have a more complicated NN architecture and prioritized replay buffer. The FPS chart shows that the combined system starts at 170 FPS and degrades to 130 FPS due to the 𝒪(n) buffer complexity:

Figure 8.28: Performance comparison (in frames per second)
Hyperparameter tuning
Section titled “Hyperparameter tuning”Tuning was done as before and was able to further improve the combined system training in terms of games played before solving the game. The following are charts comparing the tuned baseline DQN with the tuned combined system:

Figure 8.29: Comparison of tuned baseline DQN with tuned combined system
Another chart showing the effect of the tuning is the comparison of raw game rewards before and after the tuning. The tuned system starts to get the maximum score even earlier — just after 40 games, which is quite impressive:

Figure 8.30: Raw reward for untuned and tuned combined DQN
Summary
Section titled “Summary”In this chapter, we have walked through and implemented a lot of DQN improvements that have been discovered by researchers since the first DQN paper was published in 2015. This list is far from complete. First of all, for the list of methods, I used the paper Rainbow: Combining improvements in deep reinforcement learning [Hes+18], which was published by DeepMind, so the list of methods is definitely biased to DeepMind papers. Secondly, RL is so active nowadays that new papers come out almost every day, which makes it very hard to keep up, even if we limit ourselves to one kind of RL model, such as a DQN. The goal of this chapter was to give you a practical view of different ideas that the field has developed.
In the next chapter, we will continue discussing practical DQN applications from an engineering perspective by talking about ways to improve DQN performance without touching the underlying method.