keras-team/keras · error · ValueError
The number of input channels must be evenly divisible by…
Error message
The number of input channels must be evenly divisible by kernel's in_channels. Received input channels {channels} and kernel in_channels {kernel_in_channels}. What it means
Pre-check in the JAX backend's convolution op: it reads the input's channel dimension (last axis for channels_last, axis 1 for channels_first) and the kernel's in_channels axis (kernel.shape[-2]) and requires the former to be a multiple of the latter, because the channels are split into feature_group_count equal groups for jax.lax.conv_general_dilated.
Solutions
- For grouped convolution, make input channels an exact multiple of the kernel's in_channels (e.g. in_channels=64, kernel in_channels=32 gives 2 groups)
- For a normal convolution, set the kernel's in_channels equal to the input's channel count
- Add a 1x1 conv or channel-padding layer upstream to align the channel counts
- Check data_format — a mismatched channels_first/channels_last setting reads the wrong axis as channels
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/jax/ops/nn.py:757 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/41e5d1b99cbd4c1d.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/jax/ops/nn.py:757
strides = _convert_to_spatial_operand(
strides,
num_spatial_dims,
data_format,
include_batch_and_channels=False,
)
dilation_rate = _convert_to_spatial_operand(
dilation_rate,
num_spatial_dims,
data_format,
include_batch_and_channels=False,
)
if data_format == "channels_last":
channels = inputs.shape[-1]
else:
channels = inputs.shape[1]
kernel_in_channels = kernel.shape[-2]
if channels % kernel_in_channels > 0:
raise ValueError(
"The number of input channels must be evenly divisible by "
f"kernel's in_channels. Received input channels {channels} and "
f"kernel in_channels {kernel_in_channels}. "
)
feature_group_count = channels // kernel_in_channels
kernel = convert_to_tensor(kernel)
inputs = convert_to_tensor(inputs, dtype=kernel.dtype)
result = jax.lax.conv_general_dilated(
inputs,
kernel,
strides,
padding,
rhs_dilation=dilation_rate,
dimension_numbers=dimension_numbers,
feature_group_count=feature_group_count,
)
if result.size == 0 and inputs.size != 0:
raise ValueError(View on GitHub (pinned to 7a34a03db6)