Launch the interactive walkthrough to discover each component, including the 3D Q-Sphere Lab & Python in Colab.
Synthesize Deep Learning and Reinforcement Learning: Deep Q-Networks (DQN), Experience Replay, and Target Networks.
| Symbol / Term | Meaning | Beginner Interpretation |
|---|---|---|
| Active network weights | Being updated via gradient descent | |
| Target network weights | Frozen periodically | |
| Replay buffer | Random mini-batch source |
Target network predicts TD target y = 10.0; active network predicts Q = 7.0.
Result: Gradient descent updates theta to reduce loss from 9.0 toward 0
Agent takes epsilon-greedy step and stores transition (s, a, r, s', done) in replay buffer.
Sample mini-batch of 32 transitions uniformly from replay memory.
Compute Bellman targets using frozen target network theta-.
Update active weights theta via Adam optimizer on MSE loss.
Every C steps (e.g. 1000 steps), copy active weights to target network.
Problem: Mastering dozens of video games directly from screen pixels.
Solution: Nature 2015 DQN achieved superhuman performance on over half of all 49 tested Atari games.
Interactive activity component
Implement a full Deep Q-Network (DQN) in PyTorch to solve Gymnasium CartPole-v1 using an experience replay buffer (deque) and epsilon-greedy exploration.
For Google Colab or Jupyter notebooks, run this shell command to install the required dependencies:
!pip install torch gymnasium numpy1 import torch 2 import torch.nn as nn 3 import torch.optim as optim 4 import gymnasium as gym 5 from collections import deque 6 import random
import torch → PyTorch deep learning framework
import torch.nn as nn → Neural network layers
import torch.optim as optim → Adam optimizer
import gymnasium as gym → Gymnasium CartPole simulation
from collections import deque → Double-ended queue for experience replay buffer
import random → Random mini-batch sampling
1 # Setup Gymnasium CartPole Environment 2 env = gym.make("CartPole-v1") 3 state_size = env.observation_space.shape[0] # 4 features: [pos, vel, angle, angular_vel] 4 action_size = env.action_space.n # 2 actions: [push left, push right] 5 6 # Define Deep Q-Network Architecture (MLP) 7 class DQN(nn.Module): 8 def __init__(self, state_size, action_size): 9 super(DQN, self).__init__() 10 self.fc1 = nn.Linear(state_size, 24) 11 self.fc2 = nn.Linear(24, 24) 12 self.fc3 = nn.Linear(24, action_size) 13 def forward(self, x): 14 x = torch.relu(self.fc1(x)) 15 x = torch.relu(self.fc2(x)) 16 return self.fc3(x) 17 18 model = DQN(state_size, action_size) 19 optimizer = optim.Adam(model.parameters(), lr=0.001) 20 loss_function = nn.MSELoss() 21 22 # Replay buffer and hyperparameters 23 memory = deque(maxlen=2000) # Experience replay buffer 24 gamma = 0.95 # Discount factor 25 epsilon = 1.0 # Initial exploration 26 epsilon_min = 0.01 27 epsilon_decay = 0.995 28 batch_size = 64 29 30 def replay(): 31 if len(memory) < batch_size: 32 return 33 minibatch = random.sample(memory, batch_size) 34 for state, action, reward, next_state, done in minibatch: 35 target = reward 36 if not done: 37 target += gamma * torch.max(model(torch.FloatTensor(next_state))).item() 38 output = model(torch.FloatTensor(state))[action] 39 loss = loss_function(output.view(-1), torch.tensor([target], dtype=torch.float32)) 40 optimizer.zero_grad() 41 loss.backward() 42 optimizer.step() 43 44 # Train for sample demonstration episodes 45 for episode in range(10): 46 state = env.reset()[0] 47 done = False 48 total_reward = 0 49 while not done: 50 if np.random.rand() < epsilon: 51 action = random.randrange(action_size) 52 else: 53 action = torch.argmax(model(torch.FloatTensor(state))).item() 54 next_state, reward, done, _, _ = env.step(action) 55 memory.append((state, action, reward, next_state, done)) 56 state = next_state 57 total_reward += reward 58 replay() 59 epsilon = max(epsilon * epsilon_decay, epsilon_min) 60 print(f"Episode {episode+1}, Total Reward: {total_reward:.0f}")
# Setup Gymnasium CartPole Environment
Environment
env = gym.make("CartPole-v1")
CartPole physics simulation
state_size = env.observation_space.shape[0] # 4 features: [pos, vel, angle, angular_vel]
Input dimensions
action_size = env.action_space.n # 2 actions: [push left, push right]
Output action count
# Define Deep Q-Network Architecture (MLP)
Network
class DQN(nn.Module):
Subclass nn.Module
def __init__(self, state_size, action_size):
Constructor
super(DQN, self).__init__()
Initialize base
self.fc1 = nn.Linear(state_size, 24)
Input layer (4 -> 24)
self.fc2 = nn.Linear(24, 24)
Hidden layer (24 -> 24)
self.fc3 = nn.Linear(24, action_size)
Output Q-values (24 -> 2)
def forward(self, x):
Forward computation
x = torch.relu(self.fc1(x))
Layer 1 + ReLU
x = torch.relu(self.fc2(x))
Layer 2 + ReLU
return self.fc3(x)
Linear output Q-values
model = DQN(state_size, action_size)
Instantiate DQN model
optimizer = optim.Adam(model.parameters(), lr=0.001)
Adam optimizer
loss_function = nn.MSELoss()
Mean squared error loss
# Replay buffer and hyperparameters
Buffer setup
memory = deque(maxlen=2000) # Experience replay buffer
Circular memory
gamma = 0.95 # Discount factor
Future reward discount
epsilon = 1.0 # Initial exploration
Exploration rate
epsilon_min = 0.01
Exploration floor
epsilon_decay = 0.995
Decay per episode
batch_size = 64
Mini-batch training size
def replay():
Mini-batch replay training function
if len(memory) < batch_size:
Wait for buffer warm-up
return
Return early
minibatch = random.sample(memory, batch_size)
Uniform random sampling
for state, action, reward, next_state, done in minibatch:
Iterate transitions
target = reward
Base target
if not done:
Bootstrap future Q
target += gamma * torch.max(model(torch.FloatTensor(next_state))).item()
Bellman target
output = model(torch.FloatTensor(state))[action]
Predicted Q(s, a)
loss = loss_function(output.view(-1), torch.tensor([target], dtype=torch.float32))
MSE loss
optimizer.zero_grad()
Zero gradients
loss.backward()
Backpropagation
optimizer.step()
Weight update
# Train for sample demonstration episodes
Training loop
for episode in range(10):
Episode loop
state = env.reset()[0]
Reset episode
done = False
Done flag
total_reward = 0
Reward tally
while not done:
Step loop
if np.random.rand() < epsilon:
Explore
action = random.randrange(action_size)
Random push
else:
Exploit
action = torch.argmax(model(torch.FloatTensor(state))).item()
Argmax action
next_state, reward, done, _, _ = env.step(action)
Step
memory.append((state, action, reward, next_state, done))
Store memory
state = next_state
Next state
total_reward += reward
Accumulate reward
replay()
Train network on replay buffer
epsilon = max(epsilon * epsilon_decay, epsilon_min)
Decay epsilon
print(f"Episode {episode+1}, Total Reward: {total_reward:.0f}")
Log episode
Click "Copy for Colab", open Google Colab, paste into a single cell, and run.
1 # ========================================================= 2 # Deep Q-Network (DQN) with Experience Replay on CartPole-v1 3 # Prepared for Google Colab (Run with Shift + Enter) 4 # ========================================================= 5 6 # Step 1: Install required packages 7 !pip install torch gymnasium numpy 8 9 # Step 2: Import libraries 10 import torch 11 import torch.nn as nn 12 import torch.optim as optim 13 import gymnasium as gym 14 from collections import deque 15 import random 16 17 # Step 3: Run Demonstration 18 # Setup Gymnasium CartPole Environment 19 env = gym.make("CartPole-v1") 20 state_size = env.observation_space.shape[0] # 4 features: [pos, vel, angle, angular_vel] 21 action_size = env.action_space.n # 2 actions: [push left, push right] 22 23 # Define Deep Q-Network Architecture (MLP) 24 class DQN(nn.Module): 25 def __init__(self, state_size, action_size): 26 super(DQN, self).__init__() 27 self.fc1 = nn.Linear(state_size, 24) 28 self.fc2 = nn.Linear(24, 24) 29 self.fc3 = nn.Linear(24, action_size) 30 def forward(self, x): 31 x = torch.relu(self.fc1(x)) 32 x = torch.relu(self.fc2(x)) 33 return self.fc3(x) 34 35 model = DQN(state_size, action_size) 36 optimizer = optim.Adam(model.parameters(), lr=0.001) 37 loss_function = nn.MSELoss() 38 39 # Replay buffer and hyperparameters 40 memory = deque(maxlen=2000) # Experience replay buffer 41 gamma = 0.95 # Discount factor 42 epsilon = 1.0 # Initial exploration 43 epsilon_min = 0.01 44 epsilon_decay = 0.995 45 batch_size = 64 46 47 def replay(): 48 if len(memory) < batch_size: 49 return 50 minibatch = random.sample(memory, batch_size) 51 for state, action, reward, next_state, done in minibatch: 52 target = reward 53 if not done: 54 target += gamma * torch.max(model(torch.FloatTensor(next_state))).item() 55 output = model(torch.FloatTensor(state))[action] 56 loss = loss_function(output.view(-1), torch.tensor([target], dtype=torch.float32)) 57 optimizer.zero_grad() 58 loss.backward() 59 optimizer.step() 60 61 # Train for sample demonstration episodes 62 for episode in range(10): 63 state = env.reset()[0] 64 done = False 65 total_reward = 0 66 while not done: 67 if np.random.rand() < epsilon: 68 action = random.randrange(action_size) 69 else: 70 action = torch.argmax(model(torch.FloatTensor(state))).item() 71 next_state, reward, done, _, _ = env.step(action) 72 memory.append((state, action, reward, next_state, done)) 73 state = next_state 74 total_reward += reward 75 replay() 76 epsilon = max(epsilon * epsilon_decay, epsilon_min) 77 print(f"Episode {episode+1}, Total Reward: {total_reward:.0f}")
A PyTorch MLP replaces the discrete Q-table, mapping 4 continuous state features to 2 action Q-values.
Transitions (s, a, r, s', done) are stored in a 2000-step deque and uniformly sampled in batches of 64 to break temporal correlation.
The network weights are updated via Adam optimizer to minimize the MSE discrepancy between current predictions and bootstrapped Bellman targets.
Continuous 4D CartPole state space [cart position, cart velocity, pole angle, pole angular velocity]
Episode 1, Total Reward: 18 Episode 2, Total Reward: 24 Episode 3, Total Reward: 31 ...
The deep neural network successfully learns to balance the CartPole upright by continuously updating its internal weights from experience replay memories.
Deep Q-Networks (DQN), replay buffer, neural network value function approximation, and CartPole balancing.
Test your understanding with these questions. Remember, this is for learning, not examination!