tensorflow/models · error · ValueError

Expected the input_matrix tensor (input_h, input_w) has rank

Error message

Expected the input_matrix tensor (input_h, input_w) has rank == 2, was: %s

What it means

Error "Expected the input_matrix tensor (input_h, input_w) has rank == 2, was: %s" thrown in tensorflow/models.

Source

Thrown at official/vision/ops/spatial_transform_ops.py:578

                             row_indices: tf.Tensor) -> tf.Tensor:
  """Gather rows from the input matrix (2-D tensor).

  This operation is equivalent to tf.gather(input_matrix, row_indices), but is
  implemented in sparse matrix multiplication.

  Args:
    input_matrix: A 2-D tensor in shape (input_h, input_w) from which to gather
      values. The shape must be 2-D, since sparse matrix multiplication is
      currently only supported on 2-D matrices.
    row_indices: A 1-D int tensor in shape (output_h) which stored the row
      indices of the input.

  Returns:
    A tensor in shape (output_h, input_w) which stores the gathered rows.
  """
  input_matrix_shape = input_matrix.get_shape().as_list()
  if len(input_matrix_shape) != 2:
    raise ValueError(
        'Expected the input_matrix tensor (input_h, input_w) has rank == 2, '
        'was: %s' % input_matrix_shape)
  row_indices_shape = row_indices.get_shape().as_list()
  if len(row_indices_shape) != 1:
    raise ValueError(
        'Expected the row_indices tensor (output_h) has rank == 1, was: %s' %
        row_indices_shape)

  # (output_h, input_h)
  indices_one_hot = tf.one_hot(
      row_indices, depth=input_matrix_shape[0], dtype=input_matrix.dtype)
  # Matrix multiplication: (output_h, input_h) x (input_h, input_w)
  # (output_h, input_w)
  return tf.linalg.matmul(indices_one_hot, input_matrix, a_is_sparse=True)


def bilinear_resize_to_bbox(
    images: tf.Tensor, bbox: tf.Tensor, output_size: tf.Tensor

View on GitHub (pinned to e006f5f0d5)

Solutions

  1. Pass input_matrix as a rank-2 tensor of shape (input_h, input_w).
  2. Reshape or index the matrix so it is exactly 2-D.

When it happens

Trigger: Thrown at official/vision/ops/spatial_transform_ops.py:578 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/f3f0695bb78ab3a4. Report an issue: GitHub.