Lesson 57
SARSA
Update an action value from the reward and from the next action the same policy actually takes. One example is on this page.
Intermediate12 min
What You Will Learn
- Idea
- Example
- When to use it
Idea
SARSA is the on-policy cousin of Q-learning. The target is the reward plus the discounted value of the next action that was really taken. If the policy explores, those exploratory actions pull the values down. The learned policy matches how the agent behaved.
Example
next_q = 0.2
reward, gamma, q, alpha = 1.0, 0.9, 0.0, 0.5
updated = q + alpha * (reward + gamma * next_q - q)
print(f"Q: {updated:.2f}")Output:
Q: 0.59When to use it
- Use it when the values should include the cost of exploring.
- The next action must come from the same policy that chose the first action.
- On the same numbers, SARSA is more cautious than Q-learning whenever the sampled next action is weak.
Loading playground…