huggingface/candle · error
error in prelu: unexpected number of channels for the input,
Error message
error in prelu: unexpected number of channels for the input, got {num_channels}, weight dim is {num_weights} What it means
candle-nn's PReLU with a per-channel weight requires the number of weights to equal the number of input channels (dim 1 for rank>=2 inputs). When neither an exact broadcast match nor a full-shape match applies, the library validates channel count and bails on mismatch.
Source
Thrown at candle-nn/src/activation.rs:82
&self.weight
}
pub fn is_scalar(&self) -> bool {
self.is_scalar
}
}
impl candle::Module for PReLU {
fn forward(&self, xs: &Tensor) -> Result<Tensor> {
let weight = if self.is_scalar {
self.weight.reshape(())?
} else if xs.shape() == self.weight.shape() {
self.weight.clone()
} else if xs.rank() >= 2 {
let num_channels = xs.dim(1)?;
let num_weights = self.weight.elem_count();
if num_weights != num_channels {
candle::bail!("error in prelu: unexpected number of channels for the input, got {num_channels}, weight dim is {num_weights}")
}
let mut s = vec![1; xs.rank()];
s[1] = num_weights;
self.weight.reshape(s)?
} else {
self.weight.clone()
};
let zeros = xs.zeros_like()?;
xs.maximum(&zeros)? + xs.minimum(&zeros)?.broadcast_mul(&weight)?
}
}
/// Create or initialize a new PReLU layer.
///
/// This uses some default name for weights, namely `"weight"`.
/// # Arguments
///
/// * `num_channels` - The number of channels. Use `None` to have as single trainable value andView on GitHub (pinned to d5fee525bf)
Solutions
- Resize the PReLU weight to match the input channel count: Prelu::new(Tensor::ones((channels,), ...))
- Reshape the input so dim(1) equals num_weights
- Use a scalar weight (rank-0) PReLU if channel-wise slopes are not needed
- Check the upstream layer's out_channels matches the PReLU construction
Example fix
// before let prelu = Prelu::new(Tensor::new(0.25f32, &dev)?); // scalar but input needs 64 channels let y = prelu.forward(&x)?; // x.dim(1)==64 -> error // after let prelu = Prelu::new(Tensor::ones((64,), &dev)? * 0.25)?; let y = prelu.forward(&x)?;
Defensive patterns
Strategy: validation
Validate before calling
let channels = xs.dim(1)?;
if weight.elem_count() != channels && weight.shape() != xs.shape() {
return Err(anyhow!("prelu weight len {} != input channels {channels}", weight.elem_count()));
} Try / catch
match result {
Err(e) if e.to_string().contains("error in prelu") => {
eprintln!("rebuild PReLU with weight len = input dim(1)");
}
other => other?,
} Prevention
- Construct PReLU with one weight per input channel
- Check upstream layer out_channels before wiring PReLU
- Use a scalar weight if you do not need per-channel slopes
When it happens
Trigger: Creating Prelu::new with a weight of length N but feeding an input whose dim(1) != N and whose full shape does not equal the weight shape.
Common situations: Config mismatch: PReLU built for one channel count but a conv/linear upstream produces another; input rank-1 tensors vs multi-channel expectations; reusing a PReLU module across differently-shaped layers.
Understand the failure class
Background: Tensor shape mismatch errors ("must have shape", "expected shape ... got ..."): when tensor dimensions disagree with what an op or layer was told to expect — this error's family across 6 libraries.
Related errors
- backward not supported for non uniform upscaling factors
- in_channel mismatch between input ({c_in}) and kernel ({c_in
- in_channel {c_in} is not divisible by the number of groups
- in_channel mismatch between input ({c_in}, groups {groups})
- two elements have different len {m} {}
AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02).
Data as JSON: /api/errors/67cb8e5ba6a92750.
Report an issue: GitHub.