tensorflow/models · error · ValueError

Input should have rank

Error message

Input should have rank {}, got {}

What it means

Raised in SpatialAveragePool3D.call when the input tensor rank does not equal the rank recorded at build time (channel-last video frames), i.e. call-time inputs do not conform to the built shape contract.

Solutions

  1. Pass an input tensor whose rank equals the expected rank shown in the error.
  2. Add or remove dimensions (e.g. with tf.expand_dims or tf.squeeze) to match the expected rank.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/vision/modeling/layers/nn_layers.py:819 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24). Data as JSON: /api/errors/27549a60d62d5430. Report an issue: GitHub.

Appendix: source

Thrown at official/vision/modeling/layers/nn_layers.py:819

  def get_config(self):
    """Returns a dictionary containing the config used for initialization."""
    config = {
        'keepdims': self._keepdims,
    }
    base_config = super(SpatialAveragePool3D, self).get_config()
    return dict(list(base_config.items()) + list(config.items()))

  def build(self, input_shape):
    """Builds the layer with the given input shape."""
    if tf_keras.backend.image_data_format() == 'channels_first':
      raise ValueError('"channels_first" mode is unsupported.')

    super(SpatialAveragePool3D, self).build(input_shape)

  def call(self, inputs, states=None, output_states: bool = False):
    """Calls the layer with the given inputs."""
    if inputs.shape.rank != 5:
      raise ValueError(
          'Input should have rank {}, got {}'.format(5, inputs.shape.rank))

    output = tf.reduce_mean(inputs, axis=(2, 3), keepdims=self._keepdims)
    return (output, states) if output_states else output


class CausalConvMixin:
  """Mixin class to implement CausalConv for `tf_keras.layers.Conv` layers."""

  @property
  def use_buffered_input(self) -> bool:
    return self._use_buffered_input

  @use_buffered_input.setter
  def use_buffered_input(self, variable: bool):
    self._use_buffered_input = variable

  def _compute_buffered_causal_padding(self,

View on GitHub (pinned to e006f5f0d5)