Intro to RL

Sep 25, 20267 min

In RL, we have an agent that lives in a universe with some reward function that guides how the agent should act. Our goal is for the agent to learn policies ("how to act") that maximize reward gained in this universe.

MDPs

Usually, we model the universe the agent lives in as a Markov Decision Process (MDP).

  • An episodic1 MDP is defined by a tuple M=(S,A,H,T,r)\mathcal{M} = (S, A, H, T, r)
    • State space SS: the representation of the environment
    • Action space AA: the actions the agent can take
    • Horizon HH: the number of time-steps in an "episode"2
    • Transition kernel TT: the "physics engine" of the universe, mapping a state-action pair to a probability distribution over the next state: T:S×A→Δ(S)T: S \times A \rightarrow \Delta(S)
    • Reward function rr: the scalar feedback signal rh(sh,ah)r_h(s_h, a_h) received at step hh

The goal of RL is to find a policy π:S→Δ(A)\pi: S \rightarrow \Delta(A) that maximizes the expected cumulative reward.

Q-function and value functions

How do we evaluate how "good" a policy is? We define value functions.

Value function Vπ(s)V^\pi(s)

The value function represents the expected cumulative reward if the agent starts in state ss and strictly follows policy π\pi until the end of the horizon: Vhπ(s)=E[∑t=hHrt(st,at)∣sh=s,π]V_h^\pi(s) = \mathbb{E}\left[\sum_{t=h}^H r_t(s_t, a_t) \mid s_h = s, \pi\right] This expectation is over two independent sources of randomness - i.e. Est,at∼(T,π)=EstEat∼π(⋅∣st)\mathbb{E}_{s_t, a_t \sim (T, \pi)} = \mathbb{E}_{s_t} \mathbb{E}_{a_t \sim \pi(\cdot \mid s_t)}

  1. the policy may be stochastic: at∼πt(⋅∣st)a_t \sim \pi_t(\cdot \mid s_t)
  2. the environment is stochastic: st+1∼T(⋅∣st,at)s_{t+1} \sim T(\cdot \mid s_t, a_t)

Q-function Qπ(s,a)Q^\pi(s,a)

The Q-function (aka. action-value function) represents the expected cumulative reward if the agent starts in state ss, takes a specific action aa, and then follows policy π\pi afterward: Qhπ(s,a)=rh(s,a)+Es′∼T(⋅∣s,a)[Vh+1π(s′)]Q_h^\pi(s,a) = r_h(s,a) + \mathbb{E}_{s' \sim T(\cdot \mid s, a)}[V^\pi_{h+1}(s')]^ in word: "the best value you can get from taking action aa in state ss equals the reward you can collect right now, plus (averaged over wherever the environment might send you next) the best value obtainable from the next state if you keep acting optimally."

  • max⁡a′\max_{a'} := "act optimally from here onward"
  • Es′\mathbb{E}_{s'} := accounting for the environment's stochastic response to your action

The core idea here is the recursion - optimal long-run value splits cleanly into reward now + optimal value of the future.

Optimal Q-function Q∗Q^*

The goal of RL is to find the optimal policy π∗\pi^*, which corresponds to the optimal Q-function Q∗Q^*. If we know Q∗(s,a)Q^*(s,a) perfectly, we effectively have the optimal policy π∗\pi^*:

  • 🔑 In any state ss, pick the action aa that maximizes Q∗Q^*
  • 💡 The optimal Q-function satisfies the Bellman Optimality Equation: Qh∗(s,a)=rh(s,a)+Es′∼T(⋅∣s,a)[max⁡a′Qh+1∗(s′,a′)]Q^*_h(s,a) = r_h(s,a) + \mathbb{E}_{s' \sim T(\cdot \mid s,a)} \left[\max_{a'} Q^*_{h+1} (s', a')\right]

Model-free RL

Model-free RL encompasses methods that find the optimal policy π∗\pi^* but skip "learning the model - i.e. skips learning the transition kernel TT or the reward function rr. The agent doesn't try to understand the physics of the world; it just tries to learn the Q∗Q^* function directly through trial and error.

Q-learning

Q-learning is one of the foundational model-free RL algorithms! It uses the Bellman equation as an update rule.

  • When the agent experiences a transition (s,a,r,s′)(s, a, r, s'), it updates its estimate of Q(s,a)Q(s,a) by nudging it toward a "target": Q(s,a)←Q(s,a)+α(r+γmax⁡a′Q(s′,a′)−Q(s,a))Q(s,a) \leftarrow Q(s,a) + \alpha \left(r + \gamma \max_{a'} Q(s', a') - Q(s,a)\right)where α\alpha is the learning rate and γ\gamma is a discount factor.
    • α∈(0,1]\alpha \in (0, 1]: how far you move toward the new estimate on each update
    • γ∈[0,1]\gamma \in [0, 1]: how much you care about future reward vs. immediate; a reward kk steps away is worth γk\gamma^k of its face value.
  • 💡 Intuition for the update: We can split the update into two pieces: Q(s,a)←Q(s,a)+α[r+γmax⁡a′Q(s′,a′)⏟target−Q(s,a)⏟current guess]Q(s,a) \leftarrow Q(s,a) + \alpha \left[\underbrace{r + \gamma \max_{a'} Q(s', a')}_\text{target} - \underbrace{Q(s,a)}_\text{current guess}\right]
    • The bracket is the TD error: the gap between what you currently believe Q(s,a)Q(s,a) is, and a freshly informed estimate (the target) built from the reward you actually just received plus your current best guess of the future (max⁡a′Q(s′,a′)\max_{a'} Q(s', a'), i.e. the discounted reward you expect to receive in the future, given that you act optimally based on your current estimate of QQ).
    • 🔑 Connection to the Bellman optimality equation: Notice that the target r+γmax⁡a′Q(s′,a′)r + \gamma \max_{a'} Q(s', a') is precisely the RHS of the Bellman optimality equation (rh(s,a)+Es′∼T(⋅∣s,a)[max⁡a′Qh+1∗(s′,a′)]r_h(s,a) + \mathbb{E}_{s' \sim T(\cdot \mid s,a)} \left[\max_{a'} Q^*_{h+1} (s', a')\right]), except without the expectation over s′s' because this expectation is not computable without knowing TT - so we substitute the single s′s' we happened to land in as one sample of it.
      • 💡 ^ The idea is that if we repeatedly do these updates over enough data points (s,a,r,s′)(s, a, r, s'), nudging our tabular Q(s,a)Q(s,a) a bit each time, we will have approximated the expectation over s′s' using these iterative updates s.t. Q→Q∗Q \rightarrow Q^*.
      • 🔖 The convergence guarantee: Tabular Q-learning converges to Q∗Q^* with probability 1, provided the following conditions hold:
        • Every (s,a)(s,a) pair is visited infinitely often.
        • The learning rate decays correctly: ∑tαt=∞\sum_t \alpha_t = \infty but ∑tαt2<∞\sum_t \alpha_t^2 < \infty → the steps must be large enough in total to travel any distance, but shrink fast enough to stop the noise from perpetually jostling the estimate.
        • Bounded rewards, and γ<1\gamma < 1 (or a proper finite horizon).
        • 🔑 ^ under these conditions, the nudging provably drives Q→Q∗Q \rightarrow Q^*. The mechanism for this proof is a fixed-point argument: the Bellman optimality operator T\mathcal{T} is a contraction in the max-norm with factor γ\gamma, so repeatedly applying it pulls any starting estimate toward its unique fixed point Q∗Q^*. Q-learning is a stochastic, sampled version of iterating that contraction: each update applies T\mathcal{T} approximately, using one sampled s′s' in place of the true expectation, and the decaying step size averages out the sampling noise so the approximate iteration still lands on the same fixed point.
  • ❓ Is Q-learning neural-network based? No, original Q-learning is tabular - you keep a lookup table with one cell per (s,a)(s,a) pair and edit cells directly using the update rule above.

Model-based RL

Model-based RL explicitly learns the rules of the universe. Using collected data, we aim to approximate the transition kernel T^(s′∣s,a)\hat{T}(s' \mid s,a) and the reward r^(s,a)\hat{r}(s,a) (as well as potentially other relevant information about the universe). Once we have this simulated "world model," a planner can be used to search through possible action sequences to find the one that yields the highest return.

  • Model-based methods originally started out fully tabular - e.g. for small discrete MDPs, we can just count what we see.
    • Estimate T^(s′∣s,a)\hat{T}(s' \mid s,a) by the empirical frequency of landing in s′s' after taking aa in ss
    • Estimate r^(s,a)\hat{r}(s,a) by averaging the rewards you saw
    • 🔖 Obtain a policy using these estimated values ^ + value iteration / policy iteration.
  • Modern versions of model-based RL have explored more complex ways of modeling T^\hat{T} and r^\hat{r}, e.g. -
    • Gaussian processes, linear models, Gaussian mixtures
    • Neural networks
    • 💡 These formulations of T^\hat{T} and r^\hat{r} make it possible to scale the "rules of the universe" to apply to large, continuous worlds - i.e. the tabular method fails when SS is continuous.

Once we have a fully specified MDP (that we learned via model-based RL), we can compute the optimal policy from it using methods such as value iteration (VI) and policy iteration (PI). These are essentially the planning engines that model-based RL plugs into.

Footnotes

  1. Alternative would be continuous, i.e. infinite horizon. ↩

  2. One "lifetime" in this universe. ↩