Fix closure variable bug in classification_error#3033
Open
Mr-Neutr0n wants to merge 1 commit intospeechbrain:developfrom
Open
Fix closure variable bug in classification_error#3033Mr-Neutr0n wants to merge 1 commit intospeechbrain:developfrom
Mr-Neutr0n wants to merge 1 commit intospeechbrain:developfrom
Conversation
The inner `error` function was using `probabilities` from the enclosing scope instead of its own `predictions` parameter. This meant argmax was applied to the closure variable rather than the argument passed by `compute_masked_loss`. Use the function parameter `predictions` directly so the function behaves correctly regardless of how it is called.
Collaborator
|
Good find. When the tests are complete, we can merge this. |
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.
Summary
error()function inclassification_errorwas referencingprobabilitiesfrom the enclosing scope instead of using its ownpredictionsparametertorch.argmaxwas applied to the closure variable rather than the argument passed bycompute_masked_loss, which is incorrect — the function should operate on its own parametertorch.argmax(probabilities, dim=-1)totorch.argmax(predictions, dim=-1)Details
compute_masked_losscallsloss_fn(predictions, targets)and passes the predictions tensor as the first argument. Theerrorfunction receives this as itspredictionsparameter, but then ignores it and capturesprobabilitiesfrom the outer scope via closure. While in the current call site these happen to be the same object, this is a latent bug:compute_masked_lossis ever updated to transform predictions before calling the loss function, this would silently produce wrong resultsTest plan
classification_error(probs, torch.tensor([1, 1]))producestensor(0.5000)(unchanged)