Fix sign error in Q-learning gradient-descent update equations#2701
Open
Chessing234 wants to merge 1 commit into
Open
Fix sign error in Q-learning gradient-descent update equations#2701Chessing234 wants to merge 1 commit into
Chessing234 wants to merge 1 commit into
Conversation
Closes d2l-ai#2649. In section 17.3, the expanded form of the gradient-descent update on the Q-learning loss is written as: Q <- Q - alpha * grad_Q l(Q) = (1 - alpha) Q - alpha (r + gamma max_a' Q') (wrong sign) The loss is l(Q) = (Q - r - gamma max_a' Q')^2, so grad_Q l = 2 (Q - r - gamma max_a' Q'). Substituting into the gradient step and absorbing the factor of 2 into alpha gives: Q - alpha (Q - r - gamma max_a' Q') = (1 - alpha) Q + alpha (r + gamma max_a' Q') The textbook expansion flipped the second sign. The next equation (terminal-state variant) inherits the same flipped sign. The in-chapter code at line 137 already uses the correct form Q[state, action] = Q[state, action] + alpha * (y - Q[state, action]) with y = r + gamma max_a' Q', so the equations were inconsistent with the code they are supposed to describe. This commit flips the two offending minus signs to plus.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2649.
Bug
In
chapter_reinforcement-learning/qlearning.md(section 17.3), the expanded form of the gradient-descent Q-learning update has a flipped sign:The same flipped sign also appears in the next equation (terminal-state variant).
Root cause
The loss is
l(Q) = (Q - r - gamma max_a' Q')^2, sograd_Q l = 2 (Q - r - gamma max_a' Q'). Substituting into the gradient step and absorbing the factor of 2 intoalpha:The book's expansion kept the minus sign from the gradient-descent step instead of distributing it through the parentheses, producing
(1 - alpha) Q - alpha (...).Why the fix is correct
The in-chapter Python implementation at line 137 of the same file already uses the correct form:
Expanding:
(1 - alpha) Q + alpha * y, matching the fixed equation. The text and the code were inconsistent; this commit makes the text match the code (and the standard Q-learning update rule).Change
Two single-character edits (
-→+) on the two equation lines. No other changes.