tensorflow/models · error · ValueError

For the tensor `%s`, the actual tensor rank `%d` (shape = %s

Error message

For the tensor `%s`, the actual tensor rank `%d` (shape = %s) is not equal to the expected tensor rank `%s`

What it means

Error "For the tensor `%s`, the actual tensor rank `%d` (shape = %s) is not equal to the expected tensor rank `%s`" thrown in tensorflow/models.

Source

Thrown at official/modeling/tf_utils.py:196

  Args:
    tensor: A tf.Tensor to check the rank of.
    expected_rank: Python integer or list of integers, expected rank.
    name: Optional name of the tensor for the error message.

  Raises:
    ValueError: If the expected shape doesn't match the actual shape.
  """
  expected_rank_dict = {}
  if isinstance(expected_rank, six.integer_types):
    expected_rank_dict[expected_rank] = True
  else:
    for x in expected_rank:
      expected_rank_dict[x] = True

  actual_rank = tensor.shape.ndims
  if actual_rank not in expected_rank_dict:
    raise ValueError(
        "For the tensor `%s`, the actual tensor rank `%d` (shape = %s) is not "
        "equal to the expected tensor rank `%s`" %
        (name, actual_rank, str(tensor.shape), str(expected_rank)))


def safe_mean(losses):
  """Computes a safe mean of the losses.

  Args:
    losses: `Tensor` whose elements contain individual loss measurements.

  Returns:
    A scalar representing the mean of `losses`. If `num_present` is zero,
      then zero is returned.
  """
  total = tf.reduce_sum(losses)
  num_elements = tf.cast(tf.size(losses), dtype=losses.dtype)
  return tf.math.divide_no_nan(total, num_elements)

View on GitHub (pinned to e006f5f0d5)

When it happens

Trigger: Thrown at official/modeling/tf_utils.py:196 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/d0d40e20e75f6ebd. Report an issue: GitHub.