keras-team/keras · error · ValueError
Input spatial dimensions must be statically known for…
Error message
Input spatial dimensions must be statically known for adaptive pooling
What it means
Raised by the static-shape check in _adaptive_average_pool2d (keras/src/backend/tensorflow/nn.py): adaptive pooling needs the input height and width at trace time to compute window boundaries, but at least one spatial dim of the converted tensor has a None (dynamic) static shape, so the boundaries cannot be derived.
Solutions
- Provide inputs with statically known H and W, e.g. tf.ensure_shape(x, (None, H, W, C))
- Set element_shape/spec when feeding from tf.data so shapes are not None
- Fall back to AveragePooling2D with fixed pool size for dynamic spatial dims
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at keras/src/backend/tensorflow/nn.py:428 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/87201a053e28e3d0.
Report an issue: GitHub.
Appendix: source
Thrown at keras/src/backend/tensorflow/nn.py:428
if data_format == "channels_first":
pooled_l = tf.transpose(pooled_l, (0, 2, 1))
return pooled_l
def _adaptive_average_pool2d(inputs, output_size, data_format="channels_first"):
if isinstance(output_size, int):
output_size = (output_size, output_size)
if data_format == "channels_first":
inputs = tf.transpose(inputs, (0, 2, 3, 1))
static_shape = inputs.shape.as_list()
h_static = static_shape[1]
w_static = static_shape[2]
out_h, out_w = output_size
if h_static is None or w_static is None:
raise ValueError(
"Input spatial dimensions must be "
"statically known for adaptive pooling"
)
small_h, big_h = compute_adaptive_pooling_window_sizes(h_static, out_h)
small_w, big_w = compute_adaptive_pooling_window_sizes(w_static, out_w)
gather_h = _compute_static_gather_indices(h_static, out_h, small_h, big_h)
gather_w = _compute_static_gather_indices(w_static, out_w, small_w, big_w)
small_pool_h = tf.nn.pool(
inputs,
window_shape=(small_h, 1),
pooling_type="AVG",
strides=(1, 1),
padding="VALID",
data_format="NHWC",
)View on GitHub (pinned to 7a34a03db6)