tracel-ai/burn · error
Outputs and targets must have the same dimensions. Got {:?}
Error message
Outputs and targets must have the same dimensions. Got {:?} and {:?} What it means
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.
Source
Thrown at crates/burn-train/src/metric/vision/dice.rs:247
Self {
name,
config,
..Default::default()
}
}
}
impl<const D: usize> Metric for DiceMetric<D> {
type Input = DiceInput<D>;
fn name(&self) -> MetricName {
self.name.clone()
}
fn update(&mut self, item: &Self::Input, _metadata: &MetricMetadata) -> SerializedEntry {
// Dice coefficient: 2 * (|X ∩ Y|) / (|X| + |Y|)
if item.outputs.dims() != item.targets.dims() {
panic!(
"Outputs and targets must have the same dimensions. Got {:?} and {:?}",
item.outputs.dims(),
item.targets.dims()
);
}
let dims = item.outputs.dims();
let batch_size = dims[0];
let n_classes = dims[1];
let mut outputs = item.outputs.clone();
let mut targets = item.targets.clone();
if !self.config.include_background && n_classes > 1 {
// If not including background, we can ignore the first class
outputs = outputs.slice(s![.., 1..]);
targets = targets.slice(s![.., 1..]);
} else if self.config.include_background && n_classes < 2 {View on GitHub (pinned to d16f7ba2ed)
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
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/burn-train/src/metric/vision/dice.rs:247 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05).
Data as JSON: /api/errors/79608c8bc79c2ec1.
Report an issue: GitHub.