ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing
ApX logoApX logo
ModelsCalculatorHardwareCoursesPricing

All Courses

Introduction to Reinforcement Learning
Chapter 1: Foundations of Reinforcement Learning
What is Reinforcement Learning?
Agents and Environments
States, Actions, and Rewards
Policies: Mapping States to Actions
The RL Workflow: Interaction Loops
Types of RL Tasks: Episodic vs Continuing
Comparing RL with Other Learning Types
Setting up Your Python Environment for RL
Quiz for Chapter 1
Chapter 2: Markov Decision Processes (MDPs)
Modeling Sequential Decision Making
Formal Definition of an MDP
State Transition Probabilities
Reward Functions
Return: Cumulative Future Rewards
Discounting Future Rewards
Policies and Value Functions (Vπ, Qπ)
Finding Optimal Policies
Quiz for Chapter 2
Chapter 3: Estimating Value Functions
The Bellman Expectation Equation
The Bellman Optimality Equation
Solving Bellman Equations (Overview)
Dynamic Programming: Policy Iteration
Dynamic Programming: Value Iteration
Limitations of Dynamic Programming
Quiz for Chapter 3
Chapter 4: Monte Carlo Methods
Learning from Complete Episodes
Monte Carlo Prediction: Estimating Vπ
Monte Carlo Control: Estimating Qπ
On-Policy vs Off-Policy Learning
MC Control without Exploring Starts
On-Policy First-Visit MC Control Implementation
Off-Policy MC Prediction and Control Intro
Practice: Implementing MC Prediction
Quiz for Chapter 4
Chapter 5: Temporal-Difference Learning
Learning from Incomplete Episodes
TD(0) Prediction: Estimating Vπ
Advantages of TD Learning over MC
SARSA: On-Policy TD Control
Q-Learning: Off-Policy TD Control
Comparing SARSA and Q-Learning
Expected SARSA
Hands-on Practical: Implementing Q-Learning
Quiz for Chapter 5
Chapter 6: Function Approximation in RL
Handling Large State Spaces
Value Function Approximation (VFA)
Feature Vectors for State Representation
Linear Methods for VFA
Gradient Descent for Parameter Learning
Semi-gradient TD Methods
Using Neural Networks for VFA
Practice: Applying Linear VFA
Quiz for Chapter 6
Chapter 7: Introduction to Deep Q-Networks (DQN)
Combining Q-Learning with Deep Learning
Challenges with Neural Networks in RL
Experience Replay Mechanism
Fixed Q-Targets (Target Networks)
The DQN Algorithm Structure
Architectural Considerations for DQNs
Hands-on Practical: Building a Basic DQN
Quiz for Chapter 7
Chapter 8: Introduction to Policy Gradient Methods
Learning Policies Directly
Policy Gradient Theorem (Concept)
REINFORCE Algorithm
Baselines for Variance Reduction
Actor-Critic Methods Overview
Comparing Value-Based and Policy-Based Methods
Practice: Implementing REINFORCE
Quiz for Chapter 8

The DQN Algorithm Structure

The Deep Q-Network (DQN) algorithm combines deep neural networks with Q-learning. It utilizes Experience Replay and Target Networks to improve training stability. This standard algorithm provides a framework for training an agent to learn optimal action-value functions in complex environments.

The core idea remains rooted in Q-learning: we want to learn an action-value function Q(s,a)Q(s, a)Q(s,a) that approximates the expected return after taking action aaa in state sss and following the optimal policy thereafter. However, instead of a table, we use a deep neural network, parameterized by weights θ\thetaθ, to represent this function: Q(s,a;θ)Q(s, a; \theta)Q(s,a;θ). To handle the instability issues discussed previously, we incorporate two main modifications:

  1. Experience Replay: We store the agent's experiences (state, action, reward, next state tuples) in a replay memory buffer. During training, we sample mini-batches of these experiences randomly from the buffer to update the network. This breaks the temporal correlations in the sequence of observations and smooths out changes in the data distribution.
  2. Target Network: We use a separate neural network, called the target network, with weights θ−\theta^-θ−, to calculate the target Q-values used in the update rule. The weights θ−\theta^-θ− are periodically copied from the primary Q-network's weights θ\thetaθ, but kept fixed for a certain number of steps in between updates. This provides a more stable target for the Q-learning update, preventing the target values from chasing the constantly changing predictions of the primary network.

The DQN Algorithm Flow

Here's a breakdown of the typical DQN training loop:

  1. Initialization:

    • Initialize the replay memory buffer DDD with a fixed capacity.
    • Initialize the primary Q-network QQQ with random weights θ\thetaθ.
    • Initialize the target Q-network Q^\hat{Q}Q^​ with weights θ−=θ\theta^- = \thetaθ−=θ.
  2. Episode Loop: For each episode:

    • Reset the environment and get the initial state s1s_1s1​.
    • Preprocess the initial state if necessary (e.g., stacking frames) to create the input representation for the network.
  3. Step Loop: For each step t=1,2,...Tt = 1, 2, ... Tt=1,2,...T within the episode:

    • Action Selection: Choose an action ata_tat​ based on the current state sts_tst​ using an ϵ\epsilonϵ-greedy strategy derived from the primary Q-network:
      • With probability ϵ\epsilonϵ, select a random action.
      • With probability 1−ϵ1-\epsilon1−ϵ, select at=arg⁡max⁡aQ(st,a;θ)a_t = \arg\max_a Q(s_t, a; \theta)at​=argmaxa​Q(st​,a;θ). (Often, ϵ\epsilonϵ is annealed, starting high and decreasing over time to shift from exploration to exploitation).
    • Execute Action: Take action ata_tat​ in the environment. Observe the reward rt+1r_{t+1}rt+1​ and the next state st+1s_{t+1}st+1​. Preprocess st+1s_{t+1}st+1​.
    • Store Experience: Store the transition tuple (st,at,rt+1,st+1)(s_t, a_t, r_{t+1}, s_{t+1})(st​,at​,rt+1​,st+1​) in the replay memory DDD.
    • Sample Mini-batch: If the replay memory contains enough experiences, sample a random mini-batch of NNN transitions (sj,aj,rj+1,sj+1)(s_j, a_j, r_{j+1}, s_{j+1})(sj​,aj​,rj+1​,sj+1​) from DDD.
    • Calculate Targets: For each transition (sj,aj,rj+1,sj+1)(s_j, a_j, r_{j+1}, s_{j+1})(sj​,aj​,rj+1​,sj+1​) in the mini-batch, calculate the target value yjy_jyj​:
      • If sj+1s_{j+1}sj+1​ is a terminal state, the target is simply the reward: yj=rj+1y_j = r_{j+1}yj​=rj+1​.
      • Otherwise, the target is calculated using the target network Q^\hat{Q}Q^​: yj=rj+1+γmax⁡a′Q^(sj+1,a′;θ−)y_j = r_{j+1} + \gamma \max_{a'} \hat{Q}(s_{j+1}, a'; \theta^-)yj​=rj+1​+γa′max​Q^​(sj+1​,a′;θ−) where γ\gammaγ is the discount factor. Notice the max operation is performed over the outputs of the target network for the next state sj+1s_{j+1}sj+1​.
    • Gradient Descent Update: Perform a gradient descent step on the primary Q-network weights θ\thetaθ. The loss function is typically the Mean Squared Error (MSE) between the predicted Q-values (from the primary network for the actions taken, aja_jaj​) and the calculated target values yjy_jyj​: L(θ)=1N∑j=1N(yj−Q(sj,aj;θ))2L(\theta) = \frac{1}{N} \sum_{j=1}^{N} (y_j - Q(s_j, a_j; \theta))^2L(θ)=N1​j=1∑N​(yj​−Q(sj​,aj​;θ))2 Update θ\thetaθ using an optimizer like Adam or RMSprop: θ←θ−α∇θL(θ)\theta \leftarrow \theta - \alpha \nabla_\theta L(\theta)θ←θ−α∇θ​L(θ).
    • Update Target Network: Every CCC steps (where CCC is a hyperparameter), update the target network weights by copying the primary network weights: θ−←θ\theta^- \leftarrow \thetaθ−←θ.
    • Advance State: Set st←st+1s_t \leftarrow s_{t+1}st​←st+1​.
    • Check Termination: If sts_tst​ is a terminal state, end the current episode.

High-level interaction flow in the DQN algorithm. The agent uses its Q-Network for action selection, stores experiences in Replay Memory, samples batches to compute targets using the Target Network, and updates the Q-Network via gradient descent. The Target Network is periodically updated from the Q-Network.

This structured approach, integrating experience replay and fixed Q-targets, allows DQN to successfully train deep neural networks for Q-learning, enabling agents to learn complex behaviors directly from high-dimensional sensory inputs like pixels, which was a significant advancement in reinforcement learning. The next section looks at some of the architectural choices for the neural networks used within DQNs.

  • Human-level control through deep reinforcement learning, Volodymyr Mnih, Koray Kavukcuoglu, David Silver, Alex Graves, Ioannis Antonoglou, Daan Wierstra, and Martin Riedmiller, 2015 Nature, Vol. 518 DOI: 10.1038/nature14236 - The original paper introducing the Deep Q-Network (DQN) algorithm, showing how deep neural networks learn to play Atari games from raw pixel inputs using experience replay and target networks.
  • Reinforcement Learning: An Introduction, Richard S. Sutton and Andrew G. Barto, 2018 (MIT Press) - A standard textbook providing a full introduction to reinforcement learning, including a clear explanation of Q-learning, which is the foundation for DQN. (2nd edition)
  • Spinning Up in Deep RL, Joshua Achiam, 2018 (OpenAI) - A practical online resource from OpenAI, offering clear explanations of deep reinforcement learning algorithms like DQN and their implementations.
  • Deep Learning, Ian Goodfellow, Yoshua Bengio, and Aaron Courville, 2016 (MIT Press) - This book covers the foundations of deep learning, providing necessary background for understanding the neural network components used in algorithms such as DQN.

© 2026 ApX Machine Learning