tensorflow/models · error · ValueError

"channels_first" mode is unsupported.

Error message

"channels_first" mode is unsupported.

What it means

Raised in the positional-encoding layer's build when Keras uses 'channels_first' data format; only 'channels_last' is supported.

Solutions

  1. Use 'channels_last' data format.
  2. Transpose inputs to NHWC instead of using channels_first.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at official/vision/modeling/layers/nn_layers.py:601 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/acc03b1a42af6f93. Report an issue: GitHub.

Appendix: source

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

    """
    frames = input_shape[1]
    channels = input_shape[-1]
    pos_encoding = self._positional_encoding(
        frames, channels, start_position=frame_count, dtype=self.dtype)
    pos_encoding = tf.reshape(pos_encoding, [1, frames, 1, 1, channels])
    return pos_encoding

  def build(self, input_shape):
    """Builds the layer with the given input shape.

    Args:
      input_shape: The input shape.

    Raises:
      ValueError: If using 'channels_first' data format.
    """
    if tf_keras.backend.image_data_format() == 'channels_first':
      raise ValueError('"channels_first" mode is unsupported.')

    if self._cache_encoding:
      self._pos_encoding = self._get_pos_encoding(input_shape)

    super(PositionalEncoding, self).build(input_shape)

  def call(
      self,
      inputs: tf.Tensor,
      states: Optional[States] = None,
      output_states: bool = True,
  ) -> Union[tf.Tensor, Tuple[tf.Tensor, States]]:
    """Calls the layer with the given inputs.

    Args:
      inputs: An input `tf.Tensor`.
      states: A `dict` of states such that, if any of the keys match for this
        layer, will overwrite the contents of the buffer(s). Expected keys

View on GitHub (pinned to e006f5f0d5)