1. Home
  2. Explore
  3. Computer Science
  4. Reinforcement Learning Introduction

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

Reinforcement Learning Introduction

Reinforcement Learning: Model-Free, Gradient-Free Methods

Introduction and Core Concepts

This section reviews foundational ideas from dynamic programming that set the stage for model-free methods. The Markov decision process (MDP) provides the mathematical framework: an agent moves through states sss, takes actions aaa, receives rewards rrr, and transitions probabilistically to a next state s′s's′ according to p(s′∣s,a)p(s'|s,a)p(s′∣s,a).

The value function V(s)V(s)V(s) tells the expected cumulative future discounted reward from state sss, assuming optimal actions thereafter. The quality function Q(s,a)Q(s,a)Q(s,a) is richer: it gives the value of being in state sss and taking a specific action aaa, again assuming optimal behavior afterward. They are related by:

V(s)=max⁡aQ(s,a)V(s) = \max_a Q(s,a)V(s)=maxa​Q(s,a)

The optimal policy π∗(s)\pi^*(s)π∗(s) is simply the action that maximizes Q(s,a)Q(s,a)Q(s,a):

π∗(s)=arg⁡max⁡aQ(s,a)\pi^*(s) = \arg\max_a Q(s,a)π∗(s)=argmaxa​Q(s,a)

The Bellman optimality principle expresses these functions recursively:

Q(s,a)=E[rk+γV(sk+1)∣sk=s,ak=a]Q(s,a) = \mathbb{E}[r_k + \gamma V(s_{k+1}) \mid s_k = s, a_k = a]Q(s,a)=E[rk​+γV(sk+1​)∣sk​=s,ak​=a]

While dynamic programming (value iteration, policy iteration) can solve for VVV and QQQ when the model (transition probabilities ppp and reward function rrr) is known, many real-world problems lack such a model. This motivates model-free reinforcement learning — learning purely through trial and error.

Key idea: Without a model of the environment, an agent must experience actual transitions and rewards to estimate value and quality functions.


Monte Carlo Learning

Monte Carlo learning is the simplest model-free approach. It is episodic: learning occurs only after completing an entire trajectory (e.g., a game of chess, a race to a finish line).

For each episode, the agent follows some policy (often the current best estimate) and accumulates the discounted cumulative reward Rk=∑t=kTγt−krtR_k = \sum_{t=k}^{T} \gamma^{t-k} r_tRk​=∑t=kT​γt−krt​ for each time step kkk. The value function update is:

V(sk)=Vold(sk)+α(Rk−Vold(sk))V(s_k) = V_{\text{old}}(s_k) + \alpha \left( R_k - V_{\text{old}}(s_k) \right)V(sk​)=Vold​(sk​)+α(Rk​−Vold​(sk​))

where α\alphaα is a learning rate. In the basic formulation, the total episode reward RRR is divided equally among all visited states (or each state gets its own RkR_kRk​). The same idea applies to QQQ updates:

Q(sk,ak)=Qold(sk,ak)+α(Rk−Qold(sk,ak))Q(s_k, a_k) = Q_{\text{old}}(s_k, a_k) + \alpha \left( R_k - Q_{\text{old}}(s_k, a_k) \right)Q(sk​,ak​)=Qold​(sk​,ak​)+α(Rk​−Qold​(sk​,ak​))

Characteristics of Monte Carlo learning:

  • No bias — if enough episodes are run, it converges to the true values.

  • High variance — because entire episode returns vary widely.

  • Inefficient — must wait until the episode ends to learn; all steps are weighted equally regardless of temporal proximity to reward.

Definition: Monte Carlo learning averages complete episode returns to estimate value functions. It is unbiased but requires many episodes and does not exploit temporal structure.


Temporal Difference (TD) Learning

Temporal difference (TD) learning is a breakthrough that combines ideas from dynamic programming and Monte Carlo methods. Instead of waiting for the end of an episode, TD updates are made after every time step using the Bellman equation as a target.

TD(0) — One-Step TD

The value function update rule for TD(0) is:

V(sk)=Vold(sk)+α(rk+γV(sk+1)−Vold(sk))V(s_k) = V_{\text{old}}(s_k) + \alpha \left( r_k + \gamma V(s_{k+1}) - V_{\text{old}}(s_k) \right)V(sk​)=Vold​(sk​)+α(rk​+γV(sk+1​)−Vold​(sk​))

Here:

  • TD target: rk+γV(sk+1)r_k + \gamma V(s_{k+1})rk​+γV(sk+1​) — the observed reward plus the estimated value of the next state.

  • TD error: δk=rk+γV(sk+1)−Vold(sk)\delta_k = r_k + \gamma V(s_{k+1}) - V_{\text{old}}(s_k)δk​=rk​+γV(sk+1​)−Vold​(sk​) — the difference between the target and the current estimate.

The update nudges V(sk)V(s_k)V(sk​) toward the observed one-step return, which is a sample of the Bellman expectation. This makes TD(0) a bootstrapping method (it uses an estimate to update another estimate).

Biological connection: The TD error signal resembles dopamine release in the brain. When the actual reward differs from the expected reward, dopamine strengthens connections between neurons that fired just before the reward, reinforcing behaviors that lead to reward.

N-Step TD

One can expand the Bellman recursion to use nnn steps of actual rewards:

V(sk)=Vold(sk)+α(∑t=0n−1γtrk+t+γnV(sk+n)−Vold(sk))V(s_k) = V_{\text{old}}(s_k) + \alpha \left( \sum_{t=0}^{n-1} \gamma^t r_{k+t} + \gamma^n V(s_{k+n}) - V_{\text{old}}(s_k) \right)V(sk​)=Vold​(sk​)+α(∑t=0n−1​γtrk+t​+γnV(sk+n​)−Vold​(sk​))

As n→∞n \to \inftyn→∞ (to the end of the episode), this converges toward Monte Carlo learning. Small nnn gives faster updates but more bias; larger nnn reduces bias at the cost of higher variance.

TD(λ\lambdaλ)

TD(λ\lambdaλ) elegantly combines all nnn-step returns using an exponential weighting with decay parameter λ∈[0,1]\lambda \in [0,1]λ∈[0,1]:

V(sk)=Vold(sk)+α(Rkλ−Vold(sk))V(s_k) = V_{\text{old}}(s_k) + \alpha \left( R_k^\lambda - V_{\text{old}}(s_k) \right)V(sk​)=Vold​(sk​)+α(Rkλ​−Vold​(sk​))

where RkλR_k^\lambdaRkλ​ is the λ\lambdaλ-return, a weighted average of all nnn-step returns. This allows tuning the bias-variance trade-off continuously.

Key insight: Temporal difference learning exploits the temporal structure of rewards — events closer in time to a reward are weighted more heavily, mirroring biological learning mechanisms.


Q-Learning: Off-Policy TD on the Quality Function

Q-learning applies the temporal difference idea directly to the quality function Q(s,a)Q(s,a)Q(s,a). The update rule (the most common form, TD(0) for QQQ) is:

Q(sk,ak)=Qold(sk,ak)+α(rk+γmax⁡a′Q(sk+1,a′)−Qold(sk,ak))Q(s_k, a_k) = Q_{\text{old}}(s_k, a_k) + \alpha \left( r_k + \gamma \max_{a'} Q(s_{k+1}, a') - Q_{\text{old}}(s_k, a_k) \right)Q(sk​,ak​)=Qold​(sk​,ak​)+α(rk​+γmaxa′​Q(sk+1​,a′)−Qold​(sk​,ak​))

Key difference from earlier TD on VVV: The TD target uses max⁡a′Q(sk+1,a′)\max_{a'} Q(s_{k+1}, a')maxa′​Q(sk+1​,a′) — it assumes the agent will act optimally in the future, regardless of which action was actually taken at time kkk to get reward rkr_krk​.

This max⁡\maxmax operator makes Q-learning off-policy: the agent can learn about the optimal policy even while taking suboptimal or exploratory actions. The action aka_kak​ used to get rkr_krk​ does not have to be the greedy action with respect to QQQ; it can be random, exploratory, or even taken from another agent's behavior.

Why off-policy matters:

  • Experience replay: Past experiences (even suboptimal ones) can be reused to improve QQQ, as long as the target uses max⁡a′Q\max_{a'} Qmaxa′​Q.

  • Imitation learning: Watching an expert (or even a non-expert) and updating QQQ based on their actions, provided the future is evaluated optimally.

  • Faster convergence: Exploration can be aggressive (e.g., taking random actions) without corrupting the learning target.

Exploration Strategies

To balance exploration and exploitation, Q-learning often uses ϵ\epsilonϵ-greedy policy:

  • With probability 1−ϵ1-\epsilon1−ϵ, choose the greedy action arg⁡max⁡aQ(s,a)\arg\max_a Q(s,a)argmaxa​Q(s,a).

  • With probability ϵ\epsilonϵ, choose a random action (uniformly).

Typically ϵ\epsilonϵ starts high (e.g., 0.5 or 1.0) and anneals toward zero over time, so the agent explores broadly early on and exploits its knowledge later.

Definition: Q-learning is an off-policy, model-free, gradient-free reinforcement learning algorithm that learns the optimal quality function Q∗(s,a)Q^*(s,a)Q∗(s,a) by using the Bellman optimality operator on sampled transitions.


SARSA: On-Policy TD on the Quality Function

SARSA (State-Action-Reward-State-Action) is almost identical to Q-learning but with a single, crucial difference. The update rule is:

Q(sk,ak)=Qold(sk,ak)+α(rk+γQ(sk+1,ak+1)−Qold(sk,ak))Q(s_k, a_k) = Q_{\text{old}}(s_k, a_k) + \alpha \left( r_k + \gamma Q(s_{k+1}, a_{k+1}) - Q_{\text{old}}(s_k, a_k) \right)Q(sk​,ak​)=Qold​(sk​,ak​)+α(rk​+γQ(sk+1​,ak+1​)−Qold​(sk​,ak​))

Here, instead of using max⁡a′Q(sk+1,a′)\max_{a'} Q(s_{k+1}, a')maxa′​Q(sk+1​,a′), we use Q(sk+1,ak+1)Q(s_{k+1}, a_{k+1})Q(sk+1​,ak+1​) — the quality of the actual next action ak+1a_{k+1}ak+1​ that was taken (or will be taken) by the current policy. This makes SARSA on-policy: the target depends on the very policy being followed.

Implications:

  • If the policy is greedy (always takes arg⁡max⁡aQ\arg\max_a Qargmaxa​Q), SARSA and Q-learning become equivalent.

  • If the policy includes exploration (e.g., ϵ\epsilonϵ-greedy), SARSA accounts for the exploration's effect on future returns. Q-learning ignores it by assuming optimal future actions.

  • SARSA tends to be safer during training because it doesn't assume perfect future optimality — it learns a policy that works well given the exploration noise.

Extending SARSA to N-Step and TD(λ\lambdaλ)

Like TD learning on VVV, SARSA can be extended to nnn-step returns and the TD(λ\lambdaλ) framework. The TD target simply uses the observed rewards and the QQQ values of the actual actions taken over those nnn steps.


Comparison: Q-Learning vs. SARSA

Key Differences

Aspect

Q-Learning

SARSA

Policy type

Off-policy

On-policy

TD target

rk+γmax⁡a′Q(sk+1,a′)r_k + \gamma \max_{a'} Q(s_{k+1}, a')rk​+γmaxa′​Q(sk+1​,a′)

rk+γQ(sk+1,ak+1)r_k + \gamma Q(s_{k+1}, a_{k+1})rk​+γQ(sk+1​,ak+1​)

Assumption about future

Always optimal (max over actions)

Follows current policy (which may be suboptimal)

Exploration handling

Can learn from any behavior; ignores exploration in target

Learns a policy that accounts for its own exploration

Convergence

Converges to optimal Q∗Q^*Q∗ under standard conditions

Converges to the optimal policy for the given exploration schedule

Typical behavior

Faster convergence, higher variance, riskier during training

Slower, safer, higher cumulative reward during learning

Practical Trade-Offs

  • Q-learning is preferred when safe exploration is possible and speed of learning matters. It can leverage diverse experiences, including offline data or demonstrations.

  • SARSA is better when safety during training is critical (e.g., autonomous driving, robotic manipulation). It tends to avoid dangerous actions because the target penalizes exploratory blunders.

Key insight: The off-policy nature of Q-learning allows it to learn from any behavior (including past suboptimal experiences), while SARSA's on-policy nature makes it inherently conservative but often safer.


Summary and Outlook

Model-free, gradient-free reinforcement learning provides a powerful framework for learning optimal behavior when the environment dynamics are unknown. The progression from Monte Carlo (wait until the end) to TD (update every step) to Q-learning and SARSA (apply TD to the quality function) shows how each innovation improves sample efficiency and flexibility.

Key takeaways:

  • Monte Carlo: Unbiased but high variance and requires complete episodes.

  • TD learning: Bootstraps from current estimates, enabling online learning; characterized by the TD error signal.

  • Q-learning (off-policy TD on QQQ): Learns the optimal QQQ while exploring arbitrarily; enables experience replay and imitation.

  • SARSA (on-policy TD on QQQ): Learns a policy that is safe and consistent with the exploration strategy.

These methods are the foundation of modern deep reinforcement learning, where deep neural networks approximate the QQQ function. The combination of Q-learning with deep networks (DQN) led to breakthroughs in game playing and robotics. Future lectures will build on these gradient-free methods to incorporate function approximation and policy gradients.

Ask anything

We've read this whole study set, so questions about it get the best answers.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent