{"record":{"id":"79608c8bc79c2ec1","repo":"tracel-ai/burn","slug":"outputs-and-targets-must-have-the-same-dimensions","errorCode":null,"errorMessage":"Outputs and targets must have the same dimensions. Got {:?} and {:?}","messagePattern":"Outputs and targets must have the same dimensions\\. Got (.+?) and (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-train/src/metric/vision/dice.rs","lineNumber":247,"sourceCode":"        Self {\n            name,\n            config,\n            ..Default::default()\n        }\n    }\n}\n\nimpl<const D: usize> Metric for DiceMetric<D> {\n    type Input = DiceInput<D>;\n\n    fn name(&self) -> MetricName {\n        self.name.clone()\n    }\n\n    fn update(&mut self, item: &Self::Input, _metadata: &MetricMetadata) -> SerializedEntry {\n        // Dice coefficient: 2 * (|X ∩ Y|) / (|X| + |Y|)\n        if item.outputs.dims() != item.targets.dims() {\n            panic!(\n                \"Outputs and targets must have the same dimensions. Got {:?} and {:?}\",\n                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 {","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-train/src/metric/vision/dice.rs#L229-L265","documentation":"Validation guard in `DiceMetric::update`: the prediction tensor and the target tensor passed to the metric have different shapes, so the per-pixel intersection/union needed for the Dice coefficient cannot be computed element-wise. Typically caused by a mismatch between model output resolution/class layout and label shape.","triggerScenarios":"Thrown at crates/burn-train/src/metric/vision/dice.rs:247 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check that model outputs and targets share the same spatial dimensions (resize/crop targets to match)","Verify one-hot encoding matches the number of predicted classes","Inspect `outputs.dims()` and `targets.dims()` printed in the message to locate the mismatched axis"],"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"}