{"record":{"id":"79c31a7ffe2af2ab","repo":"tracel-ai/burn","slug":"top-k-must-be-non-zero-79c31a","errorCode":null,"errorMessage":"top_k must be non-zero","messagePattern":"top_k must be non-zero","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-train/src/metric/precision.rs","lineNumber":63,"sourceCode":"    pub fn binary(threshold: f64) -> Self {\n        Self::new(ClassificationMetricConfig {\n            decision_rule: DecisionRule::Threshold(threshold),\n            // binary classification results are the same independently of class_reduction\n            ..Default::default()\n        })\n    }\n\n    /// Precision metric for multiclass classification.\n    ///\n    /// # Arguments\n    ///\n    /// * `top_k` - The number of highest predictions considered to find the correct label (typically `1`).\n    /// * `class_reduction` - [Class reduction](ClassReduction) type.\n    #[allow(dead_code)]\n    pub fn multiclass(top_k: usize, class_reduction: ClassReduction) -> Self {\n        Self::new(ClassificationMetricConfig {\n            decision_rule: DecisionRule::TopK(\n                NonZeroUsize::new(top_k).expect(\"top_k must be non-zero\"),\n            ),\n            class_reduction,\n        })\n    }\n\n    /// Precision metric for multi-label classification.\n    ///\n    /// # Arguments\n    ///\n    /// * `threshold` - The threshold to transform a probability into a binary value.\n    /// * `class_reduction` - [Class reduction](ClassReduction) type.\n    #[allow(dead_code)]\n    pub fn multilabel(threshold: f64, class_reduction: ClassReduction) -> Self {\n        Self {\n            config: ClassificationMetricConfig {\n                decision_rule: DecisionRule::Threshold(threshold),\n                class_reduction,\n            },","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/precision.rs#L45-L81","documentation":"Config validation in `PrecisionRecallMetric`/`ClassificationMetricConfig`: `top_k` of zero was supplied to the multiclass precision constructor, where a zero top-k is meaningless for label selection, so construction panics.","triggerScenarios":"Thrown at crates/burn-train/src/metric/precision.rs:63 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use top_k >= 1 when creating the metric","Clamp or validate top_k from user config before construction","Choose the binary(threshold) constructor when top-k is irrelevant"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}