huggingface/candle · error · candle::Error

input and target must have the same shape, got inp: {:?}, ta

Error message

input and target must have the same shape, got inp: {:?}, target: {:?}

What it means

candle_nn::loss::huber computes the element-wise Huber loss between inp and target, which requires both tensors to have identical shapes so elementwise subtraction and comparisons are well defined. This bail fires when inp.dims() != target.dims(), reporting both shapes.

Source

Thrown at candle-nn/src/loss.rs:91

    Ok(loss)
}

/// HuberLoss
///
/// A robust loss function that combines `MAE` and `MSE` losses:
///
/// - When the absolute element-wise error is less than `delta`, it uses a squared term (MSE loss).
/// - When the absolute element-wise error is greater than or equal to `delta`, it uses a linear term (MAE loss scaled by `delta`).
/// # Formula
///
/// HuberLoss =
/// ```tex
/// 0.5(x_n - y_n)^2, & |x_n - y_n| < delta
/// delta(|x_n - y_n| - 0.5delta), & |x_n - y_n| >= delta
/// ```
pub fn huber(inp: &Tensor, target: &Tensor, delta: f64) -> Result<Tensor> {
    if inp.dims() != target.dims() {
        candle::bail!(
            "input and target must have the same shape, got inp: {:?}, target: {:?}",
            inp.dims(),
            target.dims()
        );
    }
    let diff = (inp - target)?;
    let abs_diff = diff.abs()?;
    let mask = abs_diff.le(delta)?;
    let squared_loss = ((&diff * &diff)? * 0.5)?;
    let linear_loss = ((abs_diff * delta)? - 0.5 * delta.powi(2))?;
    let loss = mask.where_cond(&squared_loss, &linear_loss)?;
    loss.mean_all()
}

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Reshape the target to match inp exactly: e.g. target.reshape_like(&inp)? or squeeze the trailing singleton dim.
  2. Alternatively reshape predictions: out.squeeze(1)? (or unsqueeze) so both are [B].
  3. Check dataset collation so targets and model outputs share the same shape convention; fix at the source rather than reshaping at the loss.
  4. Assert inp.dims() == target.dims() with a clear log right after the forward pass to catch drift early.

Example fix

// before
let loss = huber(&pred, &target, 1.0)?; // pred [B], target [B,1]
// after
let target = target.squeeze(1)?;
let loss = huber(&pred, &target, 1.0)?;
Defensive patterns

Strategy: validation

Validate before calling

if inp.dims() != target.dims() {
    return Err(anyhow!("huber shape mismatch: {:?} vs {:?}", inp.dims(), target.dims()));
}

Try / catch

let target = if target.dims() != pred.dims() {
    target.reshape_like(&pred)?
} else { target };
let loss = huber(&pred, &target, delta)?;

Prevention

When it happens

Trigger: Calling huber(&inp, &target, delta) with mismatched shapes — e.g. predictions [B] vs targets [B,1], predictions of a different sequence length, or broadcast-shaped targets that NumPy/PyTorch would silently broadcast but candle rejects.

Common situations: Regression targets squeezed/unsqueezed inconsistently between pipelines; targets stored with an extra singleton dim; predictions from a head with different output size than labels; mixing per-step and per-sequence reductions.

Related errors


AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02). Data as JSON: /api/errors/d83ad9f979bca00f. Report an issue: GitHub.