{"record":{"id":"61388172dd1feb9a","repo":"tracel-ai/burn","slug":"dice-metric-requires-at-least-2-classes-when-inclu","errorCode":null,"errorMessage":"Dice metric requires at least 2 classes when including background.","messagePattern":"Dice metric requires at least 2 classes when including background\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-train/src/metric/vision/dice.rs","lineNumber":267,"sourceCode":"                item.outputs.dims(),\n                item.targets.dims()\n            );\n        }\n\n        let dims = item.outputs.dims();\n        let batch_size = dims[0];\n        let n_classes = dims[1];\n\n        let mut outputs = item.outputs.clone();\n        let mut targets = item.targets.clone();\n\n        if !self.config.include_background && n_classes > 1 {\n            // If not including background, we can ignore the first class\n            outputs = outputs.slice(s![.., 1..]);\n            targets = targets.slice(s![.., 1..]);\n        } else if self.config.include_background && n_classes < 2 {\n            // If including background, we need at least 2 classes\n            panic!(\"Dice metric requires at least 2 classes when including background.\");\n        }\n\n        let intersection = (outputs.clone() * targets.clone()).sum();\n        let outputs_sum = outputs.sum();\n        let targets_sum = targets.sum();\n\n        // Convert to f64\n        let intersection_val = intersection.into_scalar::<f64>();\n        let outputs_sum_val = outputs_sum.into_scalar::<f64>();\n        let targets_sum_val = targets_sum.into_scalar::<f64>();\n\n        self.state.update(\n            intersection_val,\n            outputs_sum_val,\n            targets_sum_val,\n            batch_size,\n            self.config.epsilon,\n        );","sourceCodeStart":249,"sourceCodeEnd":285,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/vision/dice.rs#L249-L285","documentation":"Configuration guard in `DiceMetric::update`: `include_background` is enabled but the tensors contain fewer than 2 classes, so there would be no foreground classes left after the background handling and the Dice computation is meaningless.","triggerScenarios":"Thrown at crates/burn-train/src/metric/vision/dice.rs:267 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Ensure the model outputs at least 2 channels (classes)","Set `include_background: false` in the metric config if you only have a single foreground channel","Check that targets/outputs use a class dimension of the expected size"],"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"}