Lecture 11: Reinforcement Learning

Shen Shen

Nov 23, 2026

2:30pm, Room 10-250

Slides and Lecture Recording

Intro to Machine Learning

  • \(\mathcal{S}\) : state space, contains all possible states \(s\).
  • \(\mathcal{A}\) : action space, contains all possible actions \(a\).
  • \(\mathrm{T}\left(s, a, s^{\prime}\right)\) : the probability of transition from state \(s\) to \(s^{\prime}\) when action \(a\) is taken.
  • \(\mathrm{R}(s, a)\) : the reward received for taking action \(a\) in state \(s\).
  • \(\gamma \in [0,1]\): discount factor, a scalar.
  • \(\pi{(s)}\) : policy, takes in a state and returns an action.

The goal of an MDP is to find a good policy.

MDP: Definition and terminologies

In 6.390,

  • \(\mathcal{S}\) and \(\mathcal{A}\) are small discrete sets, unless otherwise specified.
  • \(s^{\prime}\) and \(a^{\prime}\) are shorthand for the next-timestep state and action.
  • \(\mathrm{R}(s, a)\) is deterministic and bounded.
  • \(\pi(s)\) is deterministic.
Recall
Recall

1️⃣ V definition

\(\mathrm{V}_h^\pi(s) := \mathbb{E}\left[\sum_{t=0}^{h-1} \gamma^t \mathrm{R}\left(s_t, \pi\left(s_t\right)\right) \mid s_0=s\right]\)

2️⃣ Bellman recursion

\(\mathrm{V}_h^\pi(s) = \mathrm{R}(s, \pi(s)) + \gamma \sum_{s^{\prime}} \mathrm{T}\left(s, \pi(s), s^{\prime}\right) \mathrm{V}_{h-1}^\pi\left(s^{\prime}\right)\)

3️⃣ Bellman equation

\(\mathrm{V}_{\infty}^\pi(s) = \mathrm{R}(s, \pi(s)) + \gamma \sum_{s^{\prime}} \mathrm{T}\left(s, \pi(s), s^{\prime}\right) \mathrm{V}_{\infty}^\pi\left(s^{\prime}\right)\)

4️⃣ optimal values

\(\mathrm{V}_h^*(s) = \max_{a} \big[\mathrm{R}(s, a) + \gamma \sum_{s'} \mathrm{T}(s, a, s') \mathrm{V}_{h-1}^*(s') \big] = \max_{a} \mathrm{Q}^*_h(s, a)\)

5️⃣ Q value iteration

\(\mathrm{Q}^*_h(s, a) = \mathrm{R}(s, a) + \gamma \sum_{s'} \mathrm{T}(s, a, s') \max_{a'} \mathrm{Q}^*_{h-1}(s', a')\)

6️⃣ optimal policy

\(\pi^*_h(s) = \arg\max_a \mathrm{Q}^*_h(s, a)\)

Outline

  • Reinforcement learning setup
  • Tabular Q-learning
  • Fitted Q-learning
  • Reinforcement learning setup
  • Tabular Q-learning
  • Fitted Q-learning
  • transition probabilities are known

Mario in a grid-world v1.0

(MDP version) 

  • 9 possible states
  • 4 possible actions: {Up ↑, Down ↓, Left ←, Right →}
  • rewards known
  • discount factor \(\gamma = 0.9\) 
80\%
20\%

e.g., \(\mathrm{T}\left(7, \uparrow, 4\right) = 1\)

\(\mathrm{T}\left(9, \rightarrow, 9\right) = 1\)

\(\mathrm{T}\left(6, \uparrow, 3\right) = 0.8\)

\(\mathrm{T}\left(6, \uparrow, 2\right) = 0.2\)

  • transition probabilities are unknown

Mario in a grid-world v2.0

(RL version) 

  • 9 possible states
  • 4 possible actions: {Up ↑, Down ↓, Left ←, Right →}
  • rewards unknown
  • discount factor \(\gamma = 0.9\) 
?
?
?
\dots
?

e.g., \(\mathrm{T}\left(7, \uparrow, 4\right) = ?\)

\(\mathrm{T}\left(9, \rightarrow, 9\right) = ?\)

\(\mathrm{T}\left(6, \uparrow, 3\right) = ?\)

\(\mathrm{T}\left(6, \uparrow, 2\right) = ?\)

  • \(\mathcal{S}\) : state space, contains all possible states \(s\).
  • \(\mathcal{A}\) : action space, contains all possible actions \(a\).
  • \(\mathrm{T}\left(s, a, s^{\prime}\right)\) : the probability of transition from state \(s\) to \(s^{\prime}\) when action \(a\) is taken.
  • \(\mathrm{R}(s, a)\) : the reward received for taking action \(a\) in state \(s\).
  • \(\gamma \in [0,1]\): discount factor, a scalar.
  • \(\pi{(s)}\) : policy, takes in a state and returns an action.

The goal of an MDP is to find a good policy.

MDP: Definition and terminologies

Reinforcement Learning

RL

Reinforcement learning is very general:

robotics

games

social sciences

chatbots (RLHF)

health care

...

chip design

Model-based RL: Learn the MDP tuple

1. Collect experiences \((s, a, r, s^{\prime})\)

2. Estimate \(\mathrm{\hat{T}}\), \(\mathrm{\hat{R}}\)

3. Solve \(\langle\mathcal{S}, \mathcal{A}, \mathrm{\hat{T}}, \mathrm{\hat{R}}, \gamma\rangle\) via e.g. Value Iteration

\(\frac{\text{observed}  (6, \uparrow, 2) } {\text{total} (6,\uparrow)\text{ play count}}\)

e.g. \({\mathrm{\hat{T}}}(6,\uparrow, 2 )  \approx \)

observed reward received from (6, \(\uparrow\))

e.g. \({\mathrm{\hat{R}}}(6,\uparrow )  =\)

\(\gamma = 0.9\)

Unknown transition:

?
?
?
\dots
?

Unknown rewards

\(\dots\)

\((1,\uparrow)\)

0

1

\((3,\uparrow)\)

1

3

\((3,\downarrow)\)

1

6

\(\dots\)

\((6,\uparrow)\)

-10

3

\(\dots\)

\((6,\uparrow)\)

-10

2

\((s, a)\)

\(r \)

\(s^{\prime}\)

\((1,\downarrow)\)

\(0\)

\(4\)

learn \(\mathrm{T}\) and \(\mathrm{R}\) first, then plan as in an MDP

learn values (or policies) directly,
without learning \(\mathrm{T}\) and \(\mathrm{R}\)

  • In RL we don't say hypothesis; we say model, value, or policy
    Model in MDP/RL typically means the MDP tuple \(\langle\mathcal{S}, \mathcal{A}, \mathrm{T}, \mathrm{R}, \gamma\rangle\)

[A non-exhaustive, but useful taxonomy of algorithms in modern RL. Source]

We'll focus on tabular Q-learning

then touch on fitted
Q-learning, e.g. DQN

Direct Policy-Based

Outline

  • Reinforcement learning setup
  • Tabular Q-learning
    • the update rule
    • exploration vs. exploitation
  • Fitted Q-learning​

Is it possible to get an optimal policy without using transition or rewards explicitly? 

Yes! We know one way already:

(Recall, from the MDP lab)
\pi_h^*(s)=\arg \max _a \mathrm{Q}^*_h(s, a)

Optimal policy \(\pi^*\) easily extracted from \(\mathrm{Q}^*\): 

6️⃣

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow \mathrm{R}(s, a)+\gamma \sum_{s^{\prime}} \mathrm{T}\left(s, a, s^{\prime}\right) \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

Value Iteration

  1. for \(s \in \mathcal{S}, a \in \mathcal{A}\) :
  2.       \(\mathrm{Q}_{\text {old }}(\mathrm{s}, \mathrm{a})=0\)
  3. while True:
  4.       for \(s \in \mathcal{S}, a \in \mathcal{A}\) :
  5.             \(\mathrm{Q}_{\text {new }}(s, a) \leftarrow \mathrm{R}(s, a)+\gamma \sum_{s^{\prime}} \mathrm{T}\left(s, a, s^{\prime}\right) \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)
  6.       if \(\max _{s, a}\left|Q_{\text {old }}(s, a)-Q_{\text {new }}(s, a)\right|<\epsilon:\)
  7.             return \(\mathrm{Q}_{\text {new }}\)
  8.       \(\mathrm{Q}_{\text {old }} \leftarrow \mathrm{Q}_{\text {new }}\)

But… we got \(\mathrm{Q}^*\) by value iteration, which uses \(\mathrm{T}\) and \(\mathrm{R}\) explicitly.

  • Term by term, the value-iteration update:
  • Without \(\mathrm{T}\) and \(\mathrm{R}\), first attempt: try \((s,a)\), observe one \(r\) and one \(s'\), plug them in:

(we'll see why this fails)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow \mathrm{R}(s, a)+\gamma \sum_{s^{\prime}} \mathrm{T}\left(s, a, s^{\prime}\right) \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

immediate reward

future value from \(s'\)

expected over \(s'\)

target

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow ~ r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

observed reward

future value from the observed \(s'\)

\(\mathrm{Q}_\text{old}(s, a)\)

\(\mathrm{Q}_{\text{new}}(s, a)\)

\(\dots\)

\((s, a)\)

\(r \)

\(s^{\prime}\)

\((1,\uparrow)\)

0

1

\((3,\uparrow)\)

1

3

\((3,\downarrow)\)

1

6

\(\dots\)

\((6,\uparrow)\)

-10

3

\(\dots\)

\((6,\uparrow)\)

-10

2

\(\gamma = 0.9\)

\((1,\downarrow)\)

\(0\)

\(4\)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow ~ r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\(\mathrm{Q}_{\text{old}}\) is all zeros, so every update is just the reward:

\(\mathrm{Q}_{\text{new}}(s,a) \leftarrow r + \gamma \cdot 0 = r\)

e.g. \((3,\uparrow)\): \(1 + 0.9 \times 0 = 1\);   \((6,\uparrow)\): \(-10 + 0.9 \times 0 = -10\)

\(\mathrm{Q}_\text{old}(s, a)\)

\(\mathrm{Q}_{\text{new}}(s, a)\)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow ~ r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\((6,\uparrow)\)

-10

3

\(\mathrm{Q}_{\text {new }}(6, \uparrow) \leftarrow\)

\(-10 +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(3, a^{\prime}\right)\)

\(=-10 + 0.9\mkern1.5mu{\times}\mkern1.5mu1 = -9.1 \)

\(\gamma = 0.9\)

\((s, a)\)

\(r \)

\(s^{\prime}\)

\(\dots\)

\((6,\uparrow)\)

-10

2

\(\dots\)

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\(\mathrm{Q}_\text{old}(s, a)\)

\(\mathrm{Q}_{\text{new}}(s, a)\)

\((s, a)\)

\(r \)

\(s^{\prime}\)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow ~ r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\(\gamma = 0.9\)

\(\dots\)

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\(\mathrm{Q}_{\text {new }}(6, \uparrow) \leftarrow\)

\(-10 +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(2, a^{\prime}\right)\)

\(=-10 + 0.9\mkern1.5mu{\times}\mkern1.5mu0 = -10 \)

\(\dots\)

\(\gamma = 0.9\)

\(\mathrm{Q}_{\text {new }}(6, \uparrow) \leftarrow\)

\(-10 +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(2, a^{\prime}\right)\)

\(=-10 + 0.9\mkern1.5mu{\times}\mkern1.5mu0 = -10 \)

🥺 Simply committing to the new                 keeps "washing away" the old belief

target

Whenever observe \((6, \uparrow),\) -10, 3:

Whenever observe \((6, \uparrow),\) -10, 2:

\(\mathrm{Q}_{\text {new }}(6, \uparrow) \leftarrow\)

\(-10 +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(3, a^{\prime}\right)\)

\(=-10 + 0.9\mkern1.5mu{\times}\mkern1.5mu1 = -9.1 \)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow ~ r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow ~ r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

target

\((s, a)\)

\(r \)

\(s^{\prime}\)

\(\dots\)

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\(\dots\)

\bigg(
\bigg)
\alpha
  • \(\alpha \in [0, 1]\): how much we trust the new evidence over the old belief
  • So far \(\alpha = 1\): all new evidence, no old belief. Next, \(\alpha = 0.7\).

😍 The core update rule of Q-learning: merge old belief and new evidence

\alpha

\(r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\(\mathrm{Q}_{\text {new }}(s, a) ~ \leftarrow ~ \)

\((1- \quad ) \)

\(\mathrm{Q}_{\text {old }}(s, a)\)

\(+\)

learning rate

target

old belief

\(+\)

learning rate

\((1- \qquad \qquad \qquad ) \)

\(\mathrm{Q}_\text{old}(s, a)\)

\(\mathrm{Q}_{\text{new}}(s, a)\)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow\)

\((1-\phantom{\alpha})\)

\(\alpha\)

\(\mathrm{Q}_{\text {old }}(s, a)\)

\(+\)

\(\alpha\)

\((\)

\(r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\()\)

\(\gamma = 0.9\)

\(\alpha = 0.7\)

\(\mathrm{Q}_{\text {new }}(3, \uparrow) \leftarrow (1-0.7) \mathrm{Q}_{\text {old }}(3, \uparrow)+0.7\left(1+ 0.9\max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(3, a^{\prime}\right)\right)\)

\(= (1-0.7)\mkern1.5mu{\times}\mkern1.5mu0 + 0.7\mkern1.5mu{\times}\mkern1.5mu( 1 + 0.9\mkern1.5mu{\times}\mkern1.5mu0) = 0.7\)

\((s, a)\)

\(r \)

\(s^{\prime}\)

\((1,\uparrow)\)

0

1

\((3,\uparrow)\)

1

3

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((s, a)\)

\(r \)

\(s^{\prime}\)

\(\mathrm{Q}_\text{old}(s, a)\)

\(\mathrm{Q}_{\text{new}}(s, a)\)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow\)

\((1-\phantom{\alpha})\)

\(\alpha\)

\(\mathrm{Q}_{\text {old }}(s, a)\)

\(+\)

\(\alpha\)

\((\)

\(r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\()\)

\(\alpha = 0.7\)

\(\gamma = 0.9\)

\(\mathrm{Q}_{\text {new }}(6, \uparrow) \leftarrow (1-0.7) \mathrm{Q}_{\text {old }}(6, \uparrow)+0.7\left(-10+ 0.9\max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(3, a^{\prime}\right)\right)\)

\(= (1-0.7)\mkern1.5mu{\times}\mkern1.5mu0 + 0.7\mkern1.5mu{\times}\mkern1.5mu( -10 + 0.9\mkern1.5mu{\times}\mkern1.5mu0.7) = -6.56\)

\((s, a)\)

\(r \)

\(s^{\prime}\)

\((1,\uparrow)\)

0

1

\((3,\uparrow)\)

1

3

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((s, a)\)

\(r \)

\(s^{\prime}\)

\(\mathrm{Q}_\text{old}(s, a)\)

\(\mathrm{Q}_{\text{new}}(s, a)\)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow\)

\((1-\phantom{\alpha})\)

\(\alpha\)

\(\mathrm{Q}_{\text {old }}(s, a)\)

\(+\)

\(\alpha\)

\((\)

\(r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\()\)

\(\gamma = 0.9\)

\(\alpha = 0.7\)

\(\mathrm{Q}_{\text {new }}(6, \uparrow) \leftarrow (1-0.7) \mathrm{Q}_{\text {old }}(6, \uparrow)+0.7\left(-10+ 0.9\max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(2, a^{\prime}\right)\right)\)

\(= (1-0.7)\mkern1.5mu{\times}\mkern1.5mu{-}6.56 + 0.7\mkern1.5mu{\times}\mkern1.5mu( -10 + 0.9\mkern1.5mu{\times}\mkern1.5mu0) = -8.97\)

\((s, a)\)

\(r \)

\(s^{\prime}\)

\((1,\uparrow)\)

0

1

\((3,\uparrow)\)

1

3

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((s, a)\)

\(r \)

\(s^{\prime}\)

\(\mathrm{Q}_\text{old}(s, a)\)

\(\mathrm{Q}_{\text{new}}(s, a)\)

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow\)

\((1-\phantom{\alpha})\)

\(\alpha\)

\(\mathrm{Q}_{\text {old }}(s, a)\)

\(+\)

\(\alpha\)

\((\)

\(r +\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)

\()\)

\(\gamma = 0.9\)

\(\alpha = 0.7\)

\(\mathrm{Q}_{\text {new }}(6, \uparrow) \leftarrow (1-0.7) \mathrm{Q}_{\text {old }}(6, \uparrow)+0.7\left(-10+ 0.9\max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(3, a^{\prime}\right)\right)\)

\(= (1-0.7)\mkern1.5mu{\times}\mkern1.5mu{-}8.97 + 0.7\mkern1.5mu{\times}\mkern1.5mu( -10 + 0.9\mkern1.5mu{\times}\mkern1.5mu0.7) = -9.25\)

\((s, a)\)

\(r \)

\(s^{\prime}\)

\((1,\uparrow)\)

0

1

\((3,\uparrow)\)

1

3

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((6,\uparrow)\)

-10

3

\((6,\uparrow)\)

-10

2

\((s, a)\)

\(r \)

\(s^{\prime}\)

Check-in: which numbers go where?

  • We only update the \((s,a)\) we actually try.
  • No try, no update.
  • So which \((s,a)\) should we try next?

\(\mathrm{Q}_{\text {new }}(s, a) \leftarrow (1-\alpha) \mathrm{Q}_{\text {old }}(s, a)+\alpha\left(r+\gamma \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\right)\)

It depends on the goal:

  • win now, with the best action we know: exploit
  • learn more, by trying something uncertain: explore

The fundamental dilemma in reinforcement learning... and in life!

  • with probability \(\epsilon\), choose an action \(a \in \mathcal{A}\) uniformly at random

\(\epsilon\) trades off exploration versus exploitation.

During learning, especially in early stages, we'd like to explore and observe diverse \((s,a\)) consequences.

During later stages, we can act more greedily w.r.t. the current estimated Q values

\(\epsilon\)-greedy action selection strategy

  • with probability \((1-\epsilon)\), choose \(\arg \max _{\mathrm{a}} \mathrm{Q}_{\text{old}}(s, \mathrm{a})\)

the name is flipped: \(\epsilon\)-greedy means greedy with probability \((1-\epsilon)\).

 

  1. for \(s \in \mathcal{S}, a \in \mathcal{A}\) :
  2.       \(\mathrm{Q}_{\text {old }}(\mathrm{s}, \mathrm{a})=0\)
  3. while True:
  4.       for \(s \in \mathcal{S}, a \in \mathcal{A}\) :
  5.             \(\mathrm{Q}_{\text {new }}(s, a) \leftarrow \mathrm{R}(s, a)+\gamma \sum_{s^{\prime}} \mathrm{T}\left(s, a, s^{\prime}\right) \max _{a^{\prime}} \mathrm{Q}_{\text {old }}\left(s^{\prime}, a^{\prime}\right)\)
  6.       if \(\max _{s, a}\left|Q_{\text {old }}(s, a)-Q_{\text {new }}(s, a)\right|<\epsilon:\)
  7.             return \(\mathrm{Q}_{\text {new }}\)
  8.       \(\mathrm{Q}_{\text {old }} \leftarrow \mathrm{Q}_{\text {new }}\)

Value Iteration\((\mathcal{S}, \mathcal{A}, \mathrm{T}, \mathrm{R}, \gamma, \epsilon)\)

"calculating"

"learning" (estimating)

Q-Learning \(\left(\mathcal{S}, \mathcal{A}, \gamma, \alpha, s_0,\right. \text{max-iter})\)

1. \(i=0\)
2. for \(s \in \mathcal{S}, a \in \mathcal{A}:\)
3.       \({\mathrm{Q}_\text{old}}(s, a) = 0\)
4. \(s \leftarrow s_0\)
5. while \(i < \text{max-iter}:\)
6.       \(a \gets \text{select}\_\text{action}(s, {\mathrm{Q}_\text{old}}(s, a))\)
7.       \(r,s' \gets \text{execute}(a)\)
8.      \({\mathrm{Q}}_{\text{new}}(s, a)  \leftarrow (1-\alpha){\mathrm{Q}}_{\text{old}}(s, a) + \alpha(r + \gamma \max_{a'}{\mathrm{Q}}_{\text{old}}(s', a'))\)
9.      \(s  \leftarrow s'\)
10.    \(i  \leftarrow (i+1)\)
11.      \(\mathrm{Q}_{\text{old}}  \leftarrow \mathrm{Q}_{\text{new}}\)
12. return \(\mathrm{Q}_{\text{new}}\)

"learning"

Q-Learning \(\left(\mathcal{S}, \mathcal{A}, \gamma, \alpha, s_0,\right. \text{max-iter})\)

1. \(i=0\)
2. for \(s \in \mathcal{S}, a \in \mathcal{A}:\)
3.       \({\mathrm{Q}_\text{old}}(s, a) = 0\)
4. \(s \leftarrow s_0\)
5. while \(i < \text{max-iter}:\)
6.       \(a \gets \text{select}\_\text{action}(s, {\mathrm{Q}_\text{old}}(s, a))\)
7.       \(r,s' \gets \text{execute}(a)\)
8.      \({\mathrm{Q}}_{\text{new}}(s, a)  \leftarrow (1-\alpha){\mathrm{Q}}_{\text{old}}(s, a) + \alpha(r + \gamma \max_{a'}{\mathrm{Q}}_{\text{old}}(s', a'))\)
9.      \(s  \leftarrow s'\)
10.    \(i  \leftarrow (i+1)\)
11.      \(\mathrm{Q}_{\text{old}}  \leftarrow \mathrm{Q}_{\text{new}}\)
12. return \(\mathrm{Q}_{\text{new}}\)

  • Remarkably, 👈 can converge to the true \(\mathrm{Q}^*_{\infty}\)
    only the infinite-horizon one: the update has no \(h\)

given that we visit all \(s,a\) infinitely often, and decay the learning rate \(\alpha\) as in SGD.

  • Once converged, act greedily on \(\mathrm{Q}^*\) again.
  • But convergence can be extremely slow,
    and often not practical.
  • Three hyperparameters, each in \([0,1]\),
    each controlling a trade-off:
    • discount factor \(\gamma\)
    • learning rate \(\alpha\)
    • exploration rate \(\epsilon\)

Outline

  • Reinforcement learning setup
  • Tabular Q-learning
  • Fitted Q-learning
  • So far, Q-learning is sensible for the (small) tabular setting. 
  • What if \(\mathcal{S}\) and/or \(\mathcal{A}\) are large, or even continuous?
  • We can't keep a table; we must approximate \(\mathrm{Q}(s,a)\) with a function \(\mathrm{Q}_{\theta}(s,a).\)

Continuous state and action space

\(10^{16992}\) (pixels) states

Fitted Q-learning: from table to functions

What changes: the table becomes a function

tabular: look up \(\mathrm{Q}(s,a)\)

⟹

scale up:
large/continuous \(\mathcal{S}\), \(\mathcal{A}\)

fitted: compute \(\mathrm{Q}_\theta(s,a)\)

\(\mathrm{Q}_\theta\)

features

\(\mathrm{Q}_\theta(s,a) =\)

parameters

raw numbers

\(\theta^\top [\,s,\ a\,]\)

\(2\)

one-hot of \(s\), one-hot of \(a\)

\(\theta^\top [\,\text{onehot}(s),\ \text{onehot}(a)\,]\)

\(9 + 4 = 13\)

one-hot of the \((s,a)\) pair

\(\theta^\top \text{onehot}(s,a)\)

\(9 \times 4 = 36\)

learned, from \((s,a)\)

\(\text{network}(s,a;\,\theta)\)

our choice

If \(\mathrm{Q}^*\) were given, fitting \(\mathrm{Q}_\theta\) is plain regression

⟹

  • We don't have \(\mathrm{Q}^*\). So regress onto what we've used all along, the target:

target

\(= \; r + \gamma \max_{a'} \mathrm{Q}_\theta(s', a')\)

plane: the best fit with raw-number features, \(\theta^\top[\,s,\ a\,]\)

  • 36 data points. Fit \(\theta\) by regression: \(\ \min_\theta \sum_{(s,a)} \big(\mathrm{Q}^*(s,a) - \mathrm{Q}_\theta(s,a)\big)^2\)
  • Tabular Q-learning's update rule already has this regression connection:

is equivalent to:

\(\mathrm{Q}_{\text {new}}(s, a) \leftarrow \mathrm{Q}_{\text {old }}(s, a)+\alpha\,\big(\,\text{target} - \mathrm{Q}_{\text {old }}(s, a)\,\big)\)

\mathrm{Q}_{\text {new }}(s, a) \leftarrow (1-\alpha)\, \mathrm{Q}_{\text {old }}(s, a)+\alpha\ \, \text{target}

 \(\theta_{\text{new}} \leftarrow \theta_{\text{old}} + \eta (\text{target} - \text{guess}_{\theta})\nabla_{\theta}\text{guess}\) 

  • Similar shape to the gradient-descent step in fitting \(\theta\)
    to minimize \((\text{target} - \text{guess}_\theta)^2\) in regular regression:
  • Fitted Q-learning does this regression explicitly: fit \(\mathrm{Q}_\theta\) to the target.
    However, that target is parameterized by \(\theta\) too.

One experience, \((s,a,r,s') = (6,\uparrow,-10,3)\), two methods

before

tabular  \((\alpha = 1)\)

\(\mathrm{Q}(6,\uparrow) \leftarrow \mathrm{Q}(6,\uparrow) + \alpha\,\big(\text{target} - \mathrm{Q}(6,\uparrow)\big)\)

36 numbers; green: the entry we update

fitted  \((\eta = 0.01)\)

\(\theta \leftarrow \theta + \eta\,\big(\text{target} - \mathrm{Q}_\theta(6,\uparrow)\big)\,\nabla_\theta \mathrm{Q}_\theta(6,\uparrow)\)

plane: \(\mathrm{Q}_\theta = \theta^\top[\,s,\ a\,]\)   dots: targets
green: the one we regress onto

What does one update change?

One experience, \((s,a,r,s') = (6,\uparrow,-10,3)\), two methods

after one update

tabular  \((\alpha = 1)\)

\(\mathrm{Q}(6,\uparrow) \leftarrow \mathrm{Q}(6,\uparrow) + \alpha\,\big(\text{target} - \mathrm{Q}(6,\uparrow)\big)\)

one number changed

fitted  \((\eta = 0.01)\)

\(\theta \leftarrow \theta + \eta\,\big(\text{target} - \mathrm{Q}_\theta(6,\uparrow)\big)\,\nabla_\theta \mathrm{Q}_\theta(6,\uparrow)\)

the plane tilted, and every dot moved,
not just the one we meant to fit
(faint dashed plane and dots: before the update)

With shared \(\theta\), one update moves every value, and so every target.

No convergence guarantee.

[DQN Breakout, Google DeepMind]

No guarantee, yet it can work: DQN, with a replay memory to steady the chase.

[Slide Credit: Yann LeCun]

Summary

  • Last week the MDP was known. Now \(\mathrm{T}\) and \(\mathrm{R}\) are unknown: we learn from experience.
  • Q-learning merges old belief and new evidence: \(\mathrm{Q}_{\text{new}}(s,a) \leftarrow (1-\alpha)\,\mathrm{Q}_{\text{old}}(s,a) + \alpha\ \,\text{target}\)
  • The target, \(r + \gamma\, \max_{a'} \mathrm{Q}_{\text{old}}(s',a')\), is built from our own current estimate.
  • Exploration vs. exploitation: \(\epsilon\)-greedy acts randomly with probability \(\epsilon\).
  • Tabular Q-learning converges to \(\mathrm{Q}^*_\infty\), given enough visits and a decaying \(\alpha\).
  • Fitted Q-learning regresses \(\mathrm{Q}_\theta\) onto the target; no convergence guarantee.
  • (Beyond this lecture: policy gradient methods learn \(\pi_\theta\) directly, without Q.)

🌭

\xrightarrow{\,h_\theta\,}
y \text{ (true label)}
= [1,\; 0,\; 0]

current prediction \(g=\text{softmax}(\cdot)\)

= [0.1,\; 0.2,\; 0.7]
\xrightarrow{\;-\log\;}
-\!\log(g)
\approx [2.30,\; 1.61,\; 0.36]
\odot
\mathcal{L}_{\mathrm{nllm}} = -\sum_{k} y_k \cdot \log(g_k)
\text{Loss} \approx 2.30

To reduce the loss, \(\theta\) needs to change so \(g_{\text{hotdog}}\) can go up.

This signal (from the true label \(y\)) backprop to \(\theta\) through \(-\!\log\) and softmax, so we can optimize via gradient descent.

Policy gradient, another model-free RL algorithm, is also similar to supervised learning. E.g., recall:

🤷 ↑ ? ↓

[image edited from: Andrej Karpathy]

2. Run \(\pi_\theta\) for a while. Keep track of the return.

Policy gradients:

1. Parametrize the policy \(\pi\) with \(\theta.\)

3. Update \(\theta\) so "good" trajectories more likely to happen.