keras-team/keras · error · ValueError

Input length must be statically known for adaptive pooling

Error message

Input length must be statically known for adaptive pooling

What it means

Raised by the static-shape check in _adaptive_average_pool1d (keras/src/backend/tensorflow/nn.py): adaptive pooling must compute window boundaries at trace time, so the temporal input length must be statically known. The length dimension of inputs is None in the graph (dynamic shape), and the computation cannot proceed.

Solutions

  1. Pass inputs with a static length dimension (avoid None-shaped inputs, e.g. from dataset without shape info)
  2. Set fixed shapes: x.set_shape((None, L, C)) or tf.ensure_shape
  3. Use ordinary AveragePooling1D with strides if the length must stay dynamic
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at keras/src/backend/tensorflow/nn.py:340 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/ed7e8fc5a2a8de29. Report an issue: GitHub.

Appendix: source

Thrown at keras/src/backend/tensorflow/nn.py:340

    small_indices = window_starts
    big_indices = window_starts + small_pool_len

    gather_indices = tf.where(is_big_window, big_indices, small_indices)
    return tf.cast(gather_indices, tf.int32)


def _adaptive_average_pool1d(inputs, output_size, data_format="channels_first"):
    if isinstance(output_size, int):
        output_size = (output_size,)
    if data_format == "channels_first":
        inputs = tf.transpose(inputs, (0, 2, 1))

    static_shape = inputs.shape.as_list()
    l_static = static_shape[1]
    out_l = output_size[0]

    if l_static is None:
        raise ValueError(
            "Input length must be statically known for adaptive pooling"
        )

    small_l, big_l = compute_adaptive_pooling_window_sizes(l_static, out_l)
    gather_l = _compute_static_gather_indices(l_static, out_l, small_l, big_l)

    small_pool_l = tf.nn.pool(
        inputs,
        window_shape=(small_l,),
        pooling_type="AVG",
        strides=(1,),
        padding="VALID",
        data_format="NWC",
    )
    big_pool_l = tf.nn.pool(
        inputs,
        window_shape=(big_l,),
        pooling_type="AVG",

View on GitHub (pinned to 7a34a03db6)