keras-team/keras · error · ValueError
The argument `kernel_size` cannot contain 0. Received kernel
Error message
The argument `kernel_size` cannot contain 0. Received kernel_size={self.kernel_size}. What it means
Depthwise convolution kernels must have non-zero size in every spatial dimension; a 0 in kernel_size (e.g. (3,0)) is a degenerate filter. The constructor uses all(self.kernel_size) to reject any zero component.
Source
Thrown at keras/src/layers/convolutional/base_depthwise_conv.py:139
self.use_bias = use_bias
self.depthwise_initializer = initializers.get(depthwise_initializer)
self.bias_initializer = initializers.get(bias_initializer)
self.depthwise_regularizer = regularizers.get(depthwise_regularizer)
self.bias_regularizer = regularizers.get(bias_regularizer)
self.depthwise_constraint = constraints.get(depthwise_constraint)
self.bias_constraint = constraints.get(bias_constraint)
self.input_spec = InputSpec(min_ndim=self.rank + 2)
self.data_format = self.data_format
if self.depth_multiplier is not None and self.depth_multiplier <= 0:
raise ValueError(
"Invalid value for argument `depth_multiplier`. Expected a "
"strictly positive value. Received "
f"depth_multiplier={self.depth_multiplier}."
)
if not all(self.kernel_size):
raise ValueError(
"The argument `kernel_size` cannot contain 0. Received "
f"kernel_size={self.kernel_size}."
)
if not all(self.strides):
raise ValueError(
"The argument `strides` cannot contains 0. Received "
f"strides={self.strides}"
)
def build(self, input_shape):
if self.data_format == "channels_last":
channel_axis = -1
input_channel = input_shape[-1]
else:
channel_axis = 1
input_channel = input_shape[1]
self.input_spec = InputSpec(View on GitHub (pinned to 7a34a03db6)
Solutions
- Set kernel_size to positive integers in every spatial dimension.
- Validate all(k > 0 for k in kernel_size) before constructing the layer.
- Check tuple arity matches the layer rank.
Example fix
# before keras.layers.DepthwiseConv2D(kernel_size=(3, 0)) # after keras.layers.DepthwiseConv2D(kernel_size=(3, 3))
Defensive patterns
Strategy: validation
Validate before calling
ks = kernel_size if isinstance(kernel_size, (tuple, list)) else (kernel_size,) assert all(k > 0 for k in ks)
Type guard
def valid_kernel_size(kernel_size, rank) -> bool:
ks = kernel_size if isinstance(kernel_size, (tuple, list)) else (kernel_size,)
return len(ks) == rank and all(k > 0 for k in ks) Prevention
- Validate kernel sizes in config loaders
- Match tuple arity to conv rank
When it happens
Trigger: Constructing DepthwiseConv1D/2D/3D with kernel_size containing a 0, typically a computed or config-driven kernel size.
Common situations: Kernel size derived from data shapes or config parsing that yields 0; rank/tuple arity mismatch producing a stray 0.
Related errors
- Invalid value for argument `depth_multiplier`. Expected a st
- The argument `strides` cannot contains 0. Received strides={
- The argument `kernel_size` cannot contain 0. Received: kerne
- `strides > 1` not supported in conjunction with `dilation_ra
- Invalid value for argument `depth_multiplier`. Expected a st
AI-assisted analysis of keras-team/keras@7a34a03db6 (2026-08-25).
Data as JSON: /api/errors/b644d0dc86a79598.
Report an issue: GitHub.