RL @ ECE-UofT - Lecture 06: SARSA Algorithms and Q-Learning
Overview
Ali Bereyhi develops model-free control from Monte Carlo methods through on-policy SARSA and off-policy Q-learning, emphasizing how exploration, bootstrapping, and eligibility traces affect learning. The lecture derives importance sampling and the Q-learning max target, compares the algorithms on cliff walking, and closes by motivating function approximation and deep Q-networks for continuous or high-dimensional state spaces.
Key takeaways
- SARSA's target is on-policy: Rₜ₊₁ + γQ(Sₜ₊₁, Aₜ₊₁) uses the next action actually selected by the epsilon-greedy behavior policy.
- Q-learning separates behavior from its target: it explores with epsilon-greedy actions but updates toward Rₜ₊₁ + γ maxₐ Q(Sₜ₊₁, a), estimating greedy action values.
- GLIE requires both repeated exploration of every state-action pair and a policy that becomes greedy in the limit; a fixed epsilon preserves exploration but does not become fully greedy.
- SARSA(λ) uses eligibility traces to distribute each TD error across previously visited state-action pairs, enabling online credit assignment beyond a single Q entry.
- Importance sampling reweights behavior-policy data by target-to-behavior probabilities, but large policy differences can create high-variance estimates.
- Tabular RL scales poorly with continuous or high-dimensional states; function approximation and DQN address this by estimating Q values beyond a finite lookup table.
Chapters
- Model-based policy and value iteration rely on knowing the MDP; model-free control instead estimates action values from sampled interactions.
- Generalized policy iteration alternates evaluation and improvement, but online control can update after just one new sample.
- Epsilon-greedy improvement balances exploitation of the highest estimated action value with random exploration.
- Monte Carlo control waits for a complete trajectory ending in a terminal state before computing sample returns.
- For each state-action pair on the episode, returns are accumulated backward and used to update its action-value estimate.
- After an episode updates the Q table, epsilon-greedy behavior guides the next episode instead of repeatedly sampling a fixed initial policy.
- In the cliff-walking grid, the shortest route reaches the destination but risks a severe penalty for falling into the gray cliff.
- A greedy agent can become attached to a safe but longer route after discovering it, leaving better routes unexplored.
- Epsilon-greedy action selection occasionally tests alternatives, allowing the agent to discover and evaluate the shorter path.
- Accurate action-value estimates require every state-action pair to be visited repeatedly as the number of episodes grows.
- A purely greedy policy can stop visiting alternatives permanently, so additional samples do not guarantee discovery of the optimal policy.
- Persistent exploration supports coverage, but an indefinitely fixed epsilon still causes random, potentially suboptimal actions.
- GLIE means Greedy in the Limit with Infinite Exploration: state-action pairs keep receiving visits while the policy becomes greedy asymptotically.
- A practical design starts with larger epsilon and reduces it over time, rather than stopping exploration at a hard episode threshold.
- Under suitable coverage and update conditions, increasingly accurate estimates plus a greedy limiting policy support convergence to optimal control.
- The lecture gives epsilon proportional to 1/k, where k counts episodes, as a classic decay schedule.
- The harmonic series sum of 1/k diverges, preserving cumulative opportunities to explore; the sum of 1/k squared is bounded.
- Epsilon can also be reduced periodically, such as multiplying it by a factor below one after several trajectories.
- Temporal-difference control updates during an episode, avoiding Monte Carlo's need to wait for the terminal return.
- TD(λ) combines information across different return horizons using eligibility traces, aiming to use sampled experience efficiently.
- SARSA is named for the sequence State, Action, Reward, State, Action; TD(0), n-step TD, and TD(λ) yield SARSA variants.
- After taking action Aₜ, the agent observes reward Rₜ₊₁ and next state Sₜ₊₁.
- A first TD target can use Rₜ₊₁ plus the discounted value of Sₜ₊₁, with that state value estimated by averaging Q values under the current policy.
- The update changes the Q estimate for the current state-action pair and then uses the updated estimates to improve behavior.
- Averaging over every next action can be noisy early in learning when most entries in the Q table are still zero or poorly estimated.
- Because the agent samples its next action from its current policy, SARSA can use that known action directly in its target.
- The resulting on-policy target is Rₜ₊₁ + γQ(Sₜ₊₁, Aₜ₊₁), rather than a value averaged over possible next actions.
- At each step, SARSA observes the reward and next state, selects the next action using epsilon-greedy behavior, and forms its target from that action.
- The TD error updates the current Q(Sₜ, Aₜ), after which the agent continues with the selected next action.
- Unlike Q-learning, SARSA's target reflects the exploratory policy the agent is actually following.
- An n-step SARSA return adds observed rewards along the sampled on-policy trajectory and bootstraps from a later state-action value.
- The forward view of TD(λ) averages returns across different horizons, from one-step TD through longer returns.
- Computing those returns requires waiting for later trajectory steps, so the forward view adds delay to online learning.
- SARSA(λ) keeps an eligibility table with one entry for each state-action pair, initialized to zero.
- At each step, traces are decayed by γλ and the current state-action pair's trace is incremented.
- The TD error can then update multiple previously visited Q entries in proportion to their eligibility.
- At the start of each episode, SARSA(λ) resets its eligibility table and samples an initial state-action pair.
- Its on-policy TD error is δ = Rₜ₊₁ + γQ(Sₜ₊₁, Aₜ₊₁) − Q(Sₜ, Aₜ).
- Each Q entry is updated using its eligibility trace, so one transition can credit several earlier state-action pairs.
- Off-policy methods ask whether trajectories generated by behavior policy π can evaluate or improve a different target policy π̄.
- Reusing earlier trajectories can improve sample efficiency after the target policy changes.
- Bereyhi motivates the idea with chess: observing another player's choices can teach useful moves or reveal mistakes without adopting that player's policy.
- For a random variable sampled from distribution P, an expectation under Q can be expressed using P-weighted samples multiplied by Q(x)/P(x).
- With K samples, the Monte Carlo estimate of an expectation under Q averages each sample after applying that likelihood ratio.
- The ratio converts samples from a behavior distribution into an estimate for a target distribution, provided the relevant target outcomes are supported by P.
- For a trajectory generated under π, its probability factors into action-policy probabilities and environment transition probabilities.
- Comparing the same trajectory under target policy π̄ and behavior policy π cancels the shared MDP transition terms.
- The resulting trajectory weight is the product of π̄(Aₜ|Sₜ)/π(Aₜ|Sₜ) across the trajectory.
- A return observed under behavior policy π can be reweighted to estimate the value of target policy π̄.
- Large differences between π and π̄ can produce high-variance weights, making off-policy estimates unreliable.
- Importance sampling also motivates reuse of older experience in policy optimization, where constraining policy changes helps control variance.
- For state-value TD, the one-step importance ratio is π̄(Aₜ|Sₜ)/π(Aₜ|Sₜ), because the observed next action was drawn by the behavior policy.
- The environment's transition probabilities cancel when comparing the same transition under the two policies.
- This lets a TD update based on a behavior-policy sample estimate a different target policy's value.
- When updating Q(Sₜ, Aₜ), the immediate reward follows the already-observed state-action transition and is not itself reweighted.
- The policy correction applies to the future value, whose action selection is affected by the target policy.
- Separating the immediate reward from the next-state correction avoids incorrectly multiplying the entire target by the importance ratio.
- Q-learning uses epsilon-greedy behavior to collect exploratory transitions but learns action values for a greedy target policy.
- The target policy is greedy with respect to the Q table, while the behavior policy can still choose random actions.
- This separation supports exploration without making the learned Q table an evaluation of the exploratory policy itself.
- For a greedy target policy, the next-state value is the maximum Q(Sₜ₊₁, a) over available actions.
- The TD target becomes Rₜ₊₁ + γ maxₐ Q(Sₜ₊₁, a), yielding the standard Q-learning update.
- Because the maximizing action is known from the Q table, the greedy target can be computed directly instead of explicitly multiplying trajectory importance ratios.
- The agent chooses Aₜ with epsilon-greedy behavior, observes Rₜ₊₁ and Sₜ₊₁, and updates Q(Sₜ, Aₜ).
- Its target uses the largest next-state action value, corresponding to what greedy behavior would select after the transition.
- Richard Sutton is credited in the lecture for the key Q-learning insight that makes this off-policy update simple.
- Q-learning can incorporate eligibility traces by maintaining a Q table and a matching trace table.
- Its TD error uses the reward plus γ maxₐ Q(Sₜ₊₁, a), even though the behavior action is epsilon-greedy.
- The lecture notes that traces should be reset when a random exploratory action starts a fresh trajectory segment.
- SARSA evaluates the epsilon-greedy policy it follows, so its learned route accounts for the risk created by exploratory actions near the cliff.
- Q-learning estimates greedy action values while behavior remains exploratory, which can produce lower observed rewards during training.
- A lower training return does not mean the learned Q table is suboptimal: deploying it greedily can yield stronger performance.
- The lecture extends GLIE reasoning to online control: every state-action pair must be visited infinitely often while behavior approaches greedy action selection.
- For SARSA, epsilon can decay with time, for example proportionally to 1/t, while step sizes must also satisfy appropriate stochastic-approximation conditions.
- Convergence claims are asymptotic; large state spaces can make the required visits impractical.
- The Robbins–Monro pattern described in the lecture requires step sizes whose sum diverges while the sum of their squares remains bounded.
- In practical experiments, alpha and epsilon are often tuned as hyperparameters rather than forced to follow an exact 1/t schedule.
- The cliff-walking assignment illustrates sensitivity: decaying epsilon too quickly can lock SARSA into a poor policy.
- Q-learning offers off-policy learning and can keep exploratory behavior while its Q table targets greedy control.
- SARSA can be preferable when the deployed agent must behave according to the policy currently being learned.
- Eligibility-trace resets and computational cost can also differ, so algorithm choice depends on the application's exploration and online-control requirements.
- Tabular methods require countable state and action sets; Bereyhi illustrates the problem with joint angles discretized into bins.
- Even one angle discretized at half-degree resolution can require about 360 states, and multiple body joints multiply the state count.
- The course's next step is function approximation, which estimates Q values beyond the exact state-action pairs seen in a table.
- A linear model or neural network can fit sampled Q values and provide estimates for states and actions not previously observed.
- Deep Q-networks (DQN), introduced by DeepMind, brought neural function approximation into reinforcement learning at scale.
- Bereyhi previews linear approximation and state-action embeddings, noting that one-hot features recover the tabular case.
Transcript excerpt
0:30 Okay. Shall we start? Okay. So, we talked about uh control last time, right? So, what is the difference between control and prediction that we are we were doing before this? So the key difference here is that I mean before this we talked about the fact that if you do know the environment ex perfectly then there is a way to describe what is the optimal policy right so that was policy iteration or value itration approach basically you can characterize it if you do know MDP right but at the end of the day when we get into this one I mean the u model free approach when you don't
1:19 have the exact expression for MLP then sorry for MDP MDP then obviously you cannot do that right so we first try to somehow mimic that approach that we had in the offline uh model based case basically you use one either temporal difference or Monte Carlo to estimate the value of every single will state and then based on what we did before you try to uh evaluate one specific policy. Once you have the policy you can improve the policy by being greedy on that policy right and if you repeat this one and you keep going on then you are going to
2:06 conversion to optimal policy as before. The only difference is that you don't have exact values. You have just the estimates, right? But the point in control is that you really want to go online. It means that you don't want to wait. You don't want to use a lot of samples to find out what is for example how does the random policy work, right? You you do know it's a random policy. It's not a good policy based on your initial observation every single time
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Ali Bereyhi.