concept DQN in category deep learning

appears as: DQN, DQN, The DQN
Deep Reinforcement Learning in Action

This is an excerpt from Manning's book Deep Reinforcement Learning in Action.

An off-policy reinforcement learning algorithm like Q-learning means that the choice of policy does not affect the ability to learn accurate Q values. Indeed, our Q-network could learn accurate Q values if we selected actions at random; eventually it would experience a number of winning and losing games and infer the values of states and actions. Of course, this is terribly inefficient, but the policy matters only insofar as it helps us learn with the least amount of data. In contrast, an on-policy algorithm will explicitly depend on the choice of policy or will directly aim at learning a policy from the data. In other words, in order to train our DQN, we need to collect data (experiences) from the environment, and we could do this using any policy, so DQN is off-policy. In contrast, an on-policy algorithm learns a policy while simultaneously using the same policy to collect experiences for training itself.

Most convincingly, an empirical study was done that evaluated several popular variants and improvements to the original DQN algorithm, including a distributional variant of DQN, to see which were most effective alone and which were most important in combination (“Rainbow: Combining Improvements in Deep Reinforcement Learning” by Hessel et al., 2017). It turns out that distributional Q-learning was the best-performing algorithm overall, among all the individual improvements to DQN that they tested. They combined all the techniques together in a “Rainbow” DQN, which was shown to be far more effective than any individual technique. They then tested to see which components were most crucial to the success of Rainbow, and the results were that distributional Q-learning, multistep Q-learning (covered in chapter 5), and prioritized replay (which will be briefly covered in section 7.7) were the most important to the Rainbow algorithm’s performance.

Figure 8.15. The DQN and the ICM contribute to a single overall loss function that is given to the optimizer to minimize with respect to the DQN and ICM parameters. The DQN’s Q-value predictions are compared to the observed rewards. The observed rewards, however, are summed together with the ICM’s prediction error to get a new reward value.
Deep Learning with JavaScript: Neural networks in TensorFlow.js

This is an excerpt from Manning's book Deep Learning with JavaScript: Neural networks in TensorFlow.js.

You might have noticed that this [height, width, channel] input format is exactly what convnets are designed to process. The DQN we use is of the familiar convnet architecture. The code that defines the topology of the DQN can be found in listing 11.5 (excerpted from snake-dqn/dqn.js, with some error-checking code removed for clarity). As the code and the diagram in figure 11.12 show, the network consists of a stack of conv2d layers followed by an MLP. Additional layers including batchNormalization and dropout are inserted to increase the generalization power of the DQN. The output of the DQN has a shape of [3] (excluding the batch dimension). The three elements of the output are the predicted Q-values of the corresponding actions (turn left, going straight, and turn right). Thus our model of Q(s, a) is a neural network that takes a state as the input and outputs the Q-values for all possible actions given that state.

Figure 11.12. A schematic illustration of the DQN that we use as an approximation to the function Q(s, a) for the snake problem. In the “Online DQN” box, “BN” stands for BatchNormalization.
Listing 11.5. Creating the DQN for the snake problem
export function createDeepQNetwork(h, w, numActions) {
  const model = tf.sequential();
  model.add(tf.layers.conv2d({                            #1
    filters: 128,
    kernelSize: 3,
    strides: 1,
    activation: 'relu',
    inputShape: [h, w, 2]                                 #2
  }));
  model.add(tf.layers.batchNormalization());              #3
  model.add(tf.layers.conv2d({
    filters: 256,
    kernelSize: 3,
    strides: 1,
    activation: 'relu'
  }));
  model.add(tf.layers.batchNormalization());
  model.add(tf.layers.conv2d({
    filters: 256,
    kernelSize: 3,
    strides: 1,
    activation: 'relu'
  }));
  model.add(tf.layers.flatten());                         #4
  model.add(tf.layers.dense({units: 100, activation: 'relu'}));
  model.add(tf.layers.dropout({rate: 0.25}));             #5
  model.add(tf.layers.dense({units: numActions}));
  return model;
}

Let’s pause for a moment and think about why it makes sense to use a neural network as the function Q(s, a) in this problem. The snake game has a discrete state space, unlike the continuous state space in the cart-pole problem, which consisted of four floating-point numbers. Therefore, the Q(s, a) function could in principle be implemented as a lookup table—that is, one that maps every single possible combination of board configuration and action into a value of Q. So why do we prefer a DQN over such a lookup table? The reason: there are far too many possible board configurations with even the relatively small board size (9 × 9),[8] which leads to two major shortcomings of the lookup table approach. First, the system RAM is unable to hold such a huge lookup table. Second, even if we manage to build a system with sufficient RAM, it will take a prohibitively long time for the agent to visit all the states during RL. The DQN addresses the first (memory space) problem thanks to its moderate size (about 1 million parameters). It addresses the second (state-visit time) problem because of neural networks’ generalization power. As we’ve seen ample evidence for in the previous chapters, a neural network doesn’t need to see all the possible inputs; it learns to interpolate between training examples through generalization. Therefore, by using DQN, we kill two birds with one stone.

Now we have a DQN that estimates the Q-values of all three possible actions at every step of the snake game. To achieve the greatest possible cumulative reward, all we have to do is run the DQN using the observation at every step and pick the action with the highest Q-value. Are we done yet? No, because the DQN is not trained yet! Without proper training, the DQN will contain only randomly initialized weights, and the actions it gives us will be no better than random guesses. Now the snake RL problem has been reduced to the question of how to train the DQN, a topic we’ll cover in this section. The process is somewhat involved. But don’t worry: we’ll use plenty of diagrams, accompanied by code excerpts, to spell out the training algorithm step-by-step.

Figure 11.13. The replay memory used during the training of the DQN. Five pieces of data are pushed to the end of the replay memory at every step. These data are sampled during the DQN’s training.

These pieces of data will go into the backpropagation-based training of the DQN. The replay memory can be thought of as a “dataset” for the DQN’s training. However, it’s different from the kind of datasets in supervised learning, in the sense that it keeps getting updated as the training goes on. The replay memory has a fixed length M (M = 10,000 by default in the example code). When a record (si, ai, ri, di, si+1) is pushed to its end after a new game step, an old record is popped out from its beginning, which maintains a fixed replay-memory length. This ensures that the replay memory keeps track of what happened in the most recent M steps of the training, in addition to avoiding out-of-memory problems. It is beneficial to always train the DQN using the latest game records. Why? Consider the following: once the DQN has been trained for a while and starts to “get the hang of” the game, we won’t want to teach it using old game records like the ones from the beginning of the training because those may contain naive moves that are no longer relevant or conducive to the further training of the network.