SARSA and policy gradient intuition
Quick answer
SARSA is an on-policy temporal-difference control algorithm. Its target uses the next action actually chosen by the behavior policy. Policy gradient methods instead adjust parameters of the policy itself to increase expected return. Examples are static Python snippets tested locally on CPU; Only Python standard-library modules are required. ByteMind execution is unverified.
SARSA versus Q-learning
from fractions import Fraction as Fq=[2,8];a=0print(1+F(1,2)*q[a],1+F(1,2)*max(q))Expected output (locally tested): 2 5. Exploration changes the SARSA target, but not the Q-learning target.
How it works
SARSA uses (s,a,r,s_next,a_next) and updates Q toward r+gamma Q(s_next,a_next). Q-learning uses max_b Q(s_next,b) instead. For example, if next values are [2,8] and exploration picks action 0, SARSA continuation is 2 while Q-learning continuation is 8. Carry the sampled next action into the following step rather than resample it; at true termination use only r. At external truncation choose a hypothetical next action for the bootstrap but do not execute it after reset. SARSA can learn values reflecting exploratory risk, so a greedy evaluation of its table measures a different policy from the exploratory training behavior. Policy gradients parameterize pi_theta(a|s), commonly through softmax logits. The episodic REINFORCE intuition is that gradient ascent increases log probability of actions associated with high return. For discounted start-state objective, one Monte Carlo estimator sums gamma**t * G_t * grad log pi_theta(a_t|s_t); subtracting a state-dependent baseline independent of sampled action can reduce variance without changing the expected gradient. For a two-action softmax, derivative of log probability of chosen action j with respect to logit k is indicator(j=k)-p_k. Use gradient ascent on return or descent on its negative. Finite trajectories and high variance require careful evaluation; this is not an implementation of PPO or an actor-critic system.
Softmax log probability derivative
from fractions import Fraction as Fp=[F(1,4),F(3,4)];chosen=0print([str(int(i==chosen)-v) for i,v in enumerate(p)])Expected output (locally tested): ['3/4', '-3/4']. The chosen logit derivative is positive; the derivatives sum to zero.
Common mistakes
Do not substitute max Q for the sampled SARSA action, resample a different action for the following step, or maximize loss with a descent optimizer. A return-dependent baseline using the same sampled action can bias a policy gradient.
Practice and next steps
Compare one SARSA and Q-learning target with reward=1, gamma=0.5, next values [2,8], sampled action 0. Then compute the two logit derivatives when the selected action has probability 0.25.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
SARSA and policy gradient intuition — knowledge check
Apply sarsa and policy gradient intuition to small examples and debugging scenarios.
Which action enters SARSA continuation?
What happens to the sampled next action on the next step?
What is a policy gradient parameterizing?
What can an action-independent state baseline reduce?
What does a positive return generally do in REINFORCE?