Fix confusion_matrix raising on tensor sample weights - #365
Merged
josevalim merged 1 commit intoAug 28, 2026
Merged
Conversation
validate_weights reads :type from opts, which is optional. The nil and list branches work without it, but the tensor branch calls Nx.as_type with a nil type, which raises. confusion_matrix is the only caller that passes no type, so it is the only one that hits this. Convert only when a type is given, so the tensor branch behaves like the list branch instead.
Contributor
|
💚 💙 💜 💛 ❤️ |
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.
Scholar.Metrics.Classification.confusion_matrix/3takes:sample_weightsas a list but raises on a tensor:A scalar weight tensor fails the same way. Both shapes are accepted input:
Scholar.Options.weightsvalidates rank 0 and rank 1 tensors, andvalidate_weights/3lists[{}, {num_samples}]among the shapes it handles. So the option accepts shapes the implementation then rejects, and the error names none of it.Shared.validate_weights/3reads:typefrom its opts, which is optional. The nil and list branches work without it, sinceNx.tensor(weights, type: nil)infers the type. The tensor branch callsNx.as_type(weights, nil), which raises. Of the ten call sites,confusion_matrixis the only one that passes no type, so it is the only one that reaches this.The fix converts only when a type is given, which leaves the tensor branch behaving like the list branch: the weights keep their own type.
Fixing it at the call site instead, passing
to_float_type(y_true)like the other callers do, also works but changes what the function returns. An unnormalized count matrix built from integer weights would come back as floats.Two tests cover it: tensor weights matching the same weights as a list, and a scalar weight applying to every sample.