huggingface/candle · error · candle::Error

the target tensor should have a single dimension ({dims:?})

Error message

the target tensor should have a single dimension ({dims:?})

What it means

candle_nn::loss::nll computes negative log likelihood and expects the target tensor to be exactly 1-D with shape [batch_size], holding per-sample class indices. This bail fires when target.dims() is not a single-element slice (e.g. scalar, 2-D one-hot, or higher rank).

Source

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

//! Loss Calculations
//!
use candle::{Result, Tensor};

/// The negative log likelihood loss.
///
/// Arguments
///
/// * [inp]: The input tensor of dimensions `N, C` where `N` is the batch size and `C` the number
///   of categories. This is expected to contain log probabilities.
/// * [target]: The ground truth labels as a tensor of u32 of dimension `N`.
///
/// The resulting tensor is a scalar containing the average value over the batch.
pub fn nll(inp: &Tensor, target: &Tensor) -> Result<Tensor> {
    let b_sz = match target.dims() {
        &[b_sz] => b_sz,
        dims => candle::bail!("the target tensor should have a single dimension ({dims:?})"),
    };
    match inp.dims() {
        &[inp_b_sz, _] => {
            if inp_b_sz != b_sz {
                candle::bail!("batch size mismatch between inp ({inp_b_sz}) and target ({b_sz})")
            }
        }
        dims => candle::bail!("the target tensor should have two dimensions ({dims:?})"),
    }
    inp.gather(&target.unsqueeze(1)?, 1)?
        .sum_all()?
        .affine(-1f64 / b_sz as f64, 0.)
}

/// The cross-entropy loss.
///
/// Arguments
///

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Squeeze the target to 1-D before calling: let target = target.squeeze(1)?; if it is [B,1].
  2. Convert one-hot targets back to indices via argmax (target.argmax(1)?) instead of passing the [B,C] tensor.
  3. If target is scalar, reshape to [1]: target.reshape((1,))?.
  4. Assert target.rank() == 1 in your training loop and log target.dims() when it fails.

Example fix

// before: target dims [B, 1]
let loss = cross_entropy(&logits, &target)?;
// after
let target = target.squeeze(1)?; // dims [B]
let loss = cross_entropy(&logits, &target)?;
Defensive patterns

Strategy: validation

Validate before calling

if target.rank() != 1 {
    return Err(anyhow!("nll target must be 1-D [B], got dims {:?}", target.dims()));
}

Type guard

fn is_class_index_target(t: &Tensor) -> bool { t.rank() == 1 }

Try / catch

let target = if target.rank() == 2 && target.dim(1)? == 1 {
    target.squeeze(1)?
} else { target };
let loss = cross_entropy(&logits, &target)?;

Prevention

When it happens

Trigger: Calling nll(&inp, &target) (directly or via cross_entropy) with a target of rank 0 (scalar), rank 2 (e.g. [B,1] or one-hot [B,C]), or higher.

Common situations: One-hot encoded labels passed instead of class indices; target loaded with an extra trailing dimension of size 1 from a dataloader; softmax cross-entropy ported from PyTorch where targets of shape [B] vs [B,1] both work — candle is stricter.

Related errors


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