top of page


Reinforcement Learning (RL) has come a long way with the advent of Deep Q-Networks (DQN), a sophisticated architecture that enables agents to make optimal decisions in complex environments. We'll explore the DQN's architecture, its training process, and how it handles different states and actions, all through the lens of the popular CartPole game.

What is a Deep Q-Network?

A Deep Q-Network is an advanced neural network architecture used in RL for approximating the Q-value function, which evaluates the quality of particular actions taken in given states. Unlike traditional Q-learning that uses a Q-table, DQN uses a deep neural network to estimate Q-values, making it scalable to problems with vast state and action spaces.

Architecture of DQN

DQN typically consists of several layers:

  • Input Layer: Matches the dimension of the state space. For instance, in the CartPole game, where the state is described by a 4-vector (cart position, cart velocity, pole angle, pole angular velocity), the input layer consists of four neurons.

  • Hidden Layers: One or more dense layers that allow the network to learn complex patterns in data.

  • Output Layer: Has a number of neurons equal to the number of possible actions, providing a Q-value for each action given the current state.

Depending on the input data, DQNs might use convolutional neural networks (CNNs) for processing visual inputs like frames from a video game, making them versatile for different types of environments.

Training Phases of DQN

The DQN training involves several crucial steps:

1.Input Current State:

The current state of the environment is input into the neural network. This state is represented as a vector of features, which, in the case of CartPole, includes the cart's position, velocity, pole angle, and angular velocity.

2. Predict Q-Values:

The neural network processes the input state and predicts Q-values for each possible action. This is achieved through forward propagation, where the input data passes through the network layers to produce output.

3. Policy Decision:

A policy, typically epsilon-greedy during training, uses these predicted Q-values to choose the next action. While the action that has the highest Q-value is usually selected, the epsilon-greedy policy allows for occasional random action selection to encourage exploration of the action space.

4. Execute Action and Observe Reward:

The chosen action is executed in the environment, leading to the next state and a reward. This reward and the subsequent state are critical for learning, as they provide the feedback necessary to evaluate the quality of the action taken.

5. Adjust Weights (Learning):

With the new state and reward observed, the network updates its weights to better predict Q-values that align with the observed outcomes. Calculating the "loss" between the predicted Q-value (before the action was taken) and the target Q-value. The target Q-value is derived from the reward received for taking the action plus the discounted highest Q-value predicted for the next state (from all possible next actions). Using backpropagation, the gradients of the loss are calculated, and the network's weights are adjusted (using an optimizer like Adam or SGD) to minimize this loss.

6. Repeat:

This process is repeated for many iterations and episodes. Each iteration involves inputting a state, predicting Q-values, choosing and executing an action, observing the result, and updating the network. Through this process, the network gradually learns more accurate Q-value predictions for state-action pairs.

7. Convergence:

The training phase continues until the changes in the learned Q-values converge to a stable state where further training does not significantly alter the values, or a predetermined number of episodes or iterations are completed.

Example: CartPole Game

In the CartPole game, the goal is to balance a pole on a moving cart by applying forces to the cart's left or right sides. The DQN learns to predict the outcome (Q-values) of moving left or right in various cart and pole conditions (states), allowing it to keep the pole balanced as long as possible.

CNNs vs. Feedforward Networks in DQN

While basic DQNs often use feedforward neural networks, more complex sensory environments (like those involving visual data) utilize CNNs to capitalize on their ability to handle high-dimensional data and recognize patterns across spatial hierarchies.


Deep Q-Networks represent a significant leap in the capability of reinforcement learning systems, handling complex, high-dimensional environments with unprecedented efficiency. Whether balancing a pole in CartPole or mastering a video game, DQNs continue to push the boundaries of what automated systems can learn and achieve.

6 views0 comments

Recent Posts

See All


bottom of page