1. Home
  2. Explore
  3. Computer Science
  4. Value Functions and Q Functions

Study methods

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

Value Functions and Q Functions

Speaker 1Speaker 2

Value Functions and Q Functions

6:25
0:00-6:25
  1. Anna

    Today we're moving into value-based RL, and I think the star of the show is going to be Q-learning. It was arguably the first deep RL method, and it's a great example of how we can do RL without explicitly learning a policy.

  2. Corey

    Right, and we've already seen how value functions and Q functions can represent expected future rewards. But how exactly do we go from a Q function to a policy without learning one as a separate network?

  3. Anna

    Great question. Here's a thought exercise: suppose you have a policy π and an accurate estimate of its Q function. You could define a new policy that, at each state, picks the action with the highest Q value. That new policy is deterministic, it always picks the argmax.

  4. Corey

    So it's like we're improving the policy by asking 'What would the current Q function recommend?' rather than sticking with the original policy's action. But is that new policy guaranteed to be better?

  5. Anna

    Yes, it's always at least as good as the original policy, assuming the Q estimates are accurate. The expected return from taking the argmax action in a state and then following the original policy afterward is at least as high as following the original policy from that state. That's the policy improvement theorem in action.

  6. Corey

    But in the example we saw—a 2D navigation where the old policy always goes right—the new policy was better in some states but still not optimal. Why is that?

  7. Anna

    Exactly. In that example, for states in the row above the star, the Q values for 'up' were higher than 'right', so the new policy goes up. But for the starting row, all Q values were zero, so it defaults to 'right'—which misses the star. So one improvement step isn't enough. If we iterate this process—evaluate Q for the new policy, then improve again—we can eventually back up the reward and get the optimal policy.

  8. Corey

    That iterative process is policy iteration: evaluate the Q function for the current policy, then improve by setting the policy to be greedy with respect to Q. But in practice, we don't store a table of Q values for every state-action pair, right?

  9. Anna

    Right. With large state spaces, we use function approximation—like a neural network—to represent Q. And the improvement step just becomes taking the argmax over actions using that network. For discrete action spaces, that's straightforward: we evaluate Q for each action and pick the max. For continuous actions, it's trickier—you might use gradient ascent or sample a bunch of actions and pick the best one.

  10. Corey

    So we still have a policy, just not represented explicitly. The greedy policy is derived from Q. But then we also need to think about exploration. If our policy is deterministic, we won't try new actions. How do we handle that?

  11. Anna

    That's where the off-policy nature of Q-learning comes in. We can collect data from a different exploration policy—like epsilon-greedy, which mostly follows the greedy action but sometimes takes a random action. This ensures coverage over actions so that our Q estimates are accurate for all actions, not just the ones the current policy would take.

  12. Corey

    And epsilon can be decayed over time: lots of exploration early, then less as Q improves. There's also Boltzmann exploration, where action probabilities are proportional to exponentiated Q values. That gives a softer exploration strategy.

  13. Anna

    Exactly. Now, the key equation behind Q-learning is the Bellman optimality equation, which says that for the optimal policy, Q*(s,a) = r + γ * max_{a'} Q*(s',a'). Our update is essentially trying to make the current Q satisfy that equation. We sample a transition, compute the target r + γ * max_{a'} Q(s',a') with a frozen target network, and minimize the squared error.

  14. Corey

    But we talked about convergence issues. In the tabular case with full exploration, Q-learning converges. But with function approximation, it can diverge. What makes it unstable?

  15. Anna

    One big issue is the moving target: as we update Q, the targets change because they depend on the same Q network. That makes learning non-stationary. The target network trick helps by freezing the target for a fixed number of steps—say, every 1000 iterations we copy the current Q to the target. That stabilizes learning because the labels are fixed between updates.

  16. Corey

    That's the DQN algorithm. They showed it could play Atari games from pixels. But there's another problem: overestimation. Q values tend to be overoptimistic because the max operator selects actions with high Q estimates, even if those estimates are noisy. Double DQN fixes that by using two networks: one to select the action and another to evaluate it.

  17. Anna

    Yes. In practice, we already have two networks: the current Q and the target Q. So double DQN uses the current network to pick the best action and the target network to compute its Q value. That decorrelates the noise and reduces overestimation significantly.

  18. Corey

    And another trick is n-step returns. Instead of using a one-step target, we can sum rewards over n steps and then bootstrap. This can speed up learning early on because it relies more on actual rewards and less on potentially inaccurate Q estimates. But there's a catch: those rewards come from the exploration policy, not the optimal one, so it introduces bias.

  19. Anna

    Right. Despite the bias, n-step returns often work well in practice. People usually ignore the off-policy correction and just use it anyway because it helps. There are ways to correct for it—like importance sampling or dynamically choosing n—but the simple version is often good enough.

  20. Corey

    So to wrap up, Q-learning with neural networks has three key stabilizers: a target network, double Q learning, and sometimes n-step returns. But how do we choose between this and other algorithms like PPO or SAC?

  21. Anna

    PPO is great for stability and ease of use—it doesn't need much tuning but is data-inefficient. SAC is very data-efficient but requires more tuning and is less stable. DQN, with the tricks we discussed, is a solid choice for discrete or low-dimensional continuous action spaces, and it's fairly data-efficient if you use n-step returns. So it really depends on your problem and what you prioritize.

  22. Corey

    We covered a lot today: policy iteration, Bellman equations, Q-learning, exploration, and practical improvements. I think the key takeaway is that you can do RL without an explicit policy network—just learn Q and act greedily. But you need to be careful about stability and exploration.

  23. Anna

    Exactly. And with those stabilizers, Q-learning became the first deep RL success story. Hopefully, now you have a solid understanding of how it works and when to use it.

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent