Skip to content
ml-recall

Topic notes

Value-Based Methods

Value-based RL — Bellman equations, value iteration, Monte Carlo vs TD, tabular Q-learning vs SARSA, ε-greedy exploration, and Deep Q-Networks with replay, target networks and Double DQN.

4-min read AI-checked

Practise 31 questions Take a test

Key ideas

Formulas

Vπ(s)=∑aπ(a∣s)[r(s,a)+γ∑s′P(s′∣s,a) Vπ(s′)],Vk+1(s)=max⁡a[r(s,a)+γ∑s′P(s′∣s,a) Vk(s′)]V^\pi(s) = \sum_a \pi(a \mid s) \Big[ r(s, a) + \gamma \sum_{s'} P(s' \mid s, a)\, V^\pi(s') \Big], \qquad V_{k+1}(s) = \max_a \Big[ r(s, a) + \gamma \sum_{s'} P(s' \mid s, a)\, V_k(s') \Big] MC: V(St)←V(St)+α[Gt−V(St)],TD(0): V(St)←V(St)+α[Rt+1+γV(St+1)−V(St)]\text{MC: } V(S_t) \leftarrow V(S_t) + \alpha [G_t - V(S_t)], \qquad \text{TD(0): } V(S_t) \leftarrow V(S_t) + \alpha [R_{t+1} + \gamma V(S_{t+1}) - V(S_t)] Q(St,At)←Q(St,At)+α[Rt+1+γmax⁡a′Q(St+1,a′)−Q(St,At)]Q(S_t, A_t) \leftarrow Q(S_t, A_t) + \alpha \big[ R_{t+1} + \gamma \max_{a'} Q(S_{t+1}, a') - Q(S_t, A_t) \big]

Worked example

Q(s,a)=2Q(s, a) = 2, reward r=1r = 1, γ=0.9\gamma = 0.9, α=0.5\alpha = 0.5, and Q(s′,⋅)=(5,3)Q(s', \cdot) = (5, 3).

In code

import numpy as np
Q, goal = np.zeros((5, 2)), 4              # chain 0-1-2-3-4, actions 0 = left, 1 = right
alpha, gamma, eps = 0.5, 0.9, 0.3
rng = np.random.default_rng(0)
for episode in range(200):
    s = 0
    while s != goal:                       # entering state 4 gives reward 1 and ends
        a = int(rng.integers(2)) if rng.random() < eps else int(Q[s].argmax())  # eps-greedy
        s2 = max(s - 1, 0) if a == 0 else s + 1
        r = 1.0 if s2 == goal else 0.0
        target = r + gamma * Q[s2].max() * (s2 != goal)    # no bootstrap from a terminal state
        Q[s, a] += alpha * (target - Q[s, a])              # move toward the TD target
        s = s2
print(Q[:goal].round(3).tolist())  # [[0.656, 0.729], [0.656, 0.81], [0.729, 0.9], [0.81, 1.0]]

The "right" column is γ3−s\gamma^{3-s} and the "left" one is γ\gamma times the value of the state reached (state 0 stays put), i.e. Q∗Q^* to three decimals: each update moves an entry halfway to its TD target, and the reward spreads backwards from the goal one state at a time.

Common pitfalls

References

  1. Hugging Face Deep Reinforcement Learning Course — Unit 2, Monte Carlo vs Temporal Difference Learning
  2. Hugging Face Deep Reinforcement Learning Course — Unit 2, Introducing Q-Learning: What is Q-Learning?; The Q-Learning algorithm; Off-policy vs On-policy
  3. Dive into Deep Learning — Sec. 17.2 Value Iteration (17.2.2–17.2.6, 17.2.9 Summary)
  4. Dive into Deep Learning — Sec. 17.3 Q-Learning (17.3.1–17.3.3, 17.3.6 Summary)
  5. Hugging Face Deep Reinforcement Learning Course — Unit 3, The Deep Q-Network (DQN) — Preprocessing the input and temporal limitation
  6. Hugging Face Deep Reinforcement Learning Course — Unit 3, The Deep Q-Learning Algorithm: Experience Replay; Fixed Q-Target; Double DQN
  7. Sutton & Barto (2018), Reinforcement Learning: An Introduction, 2nd ed. — Sec. 6.5 Q-learning: Off-policy TD Control, Example 6.6 Cliff Walking
  8. Hugging Face Deep Reinforcement Learning Course — Unit 3, From Q-Learning to Deep Q-Learning