{"record":{"id":"43fe1b5cf0b13c02","repo":"tensorflow/models","slug":"last-dim-out-of-bounds-for-tensor-rank","errorCode":null,"errorMessage":"`last_dim` out of bounds for `tensor` rank.","messagePattern":"`last_dim` out of bounds for `tensor` rank\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/projects/triviaqa/inputs.py","lineNumber":58,"sourceCode":"\n  Returns:\n    Tensor of shape [..., flattened_dim_size, ...] where\n    flattened_dim_size = first_dim_size * ...middle_dims... * last_dim_size.\n  \"\"\"\n  with tf.name_scope(name or 'flatten_dims'):\n    tensor = tf.convert_to_tensor(tensor)\n\n    rank = tensor.shape.rank\n    if rank is None:\n      raise ValueError('Static rank of `tensor` must be known.')\n    if first_dim < 0:  # pytype: disable=unsupported-operands\n      first_dim += rank\n    if first_dim < 0 or first_dim >= rank:  # pytype: disable=unsupported-operands\n      raise ValueError('`first_dim` out of bounds for `tensor` rank.')\n    if last_dim < 0:  # pytype: disable=unsupported-operands\n      last_dim += rank\n    if last_dim < 0 or last_dim >= rank:  # pytype: disable=unsupported-operands\n      raise ValueError('`last_dim` out of bounds for `tensor` rank.')\n    if first_dim > last_dim:  # pytype: disable=unsupported-operands\n      raise ValueError('`first_dim` must not be larger than `last_dim`.')\n\n    # Try to calculate static flattened dim size if all input sizes to flatten\n    # are statically known. Otherwise, just use -1.\n    flat_dims_shape = tensor.shape[first_dim:(last_dim + 1)].as_list()\n    flattened_dim_size = 1\n    for size in flat_dims_shape:\n      if size is None:\n        flattened_dim_size = -1\n        break\n      flattened_dim_size *= size\n\n    old_shape = tf.shape(tensor)\n    output_shape = tf.concat([\n        old_shape[:first_dim], [flattened_dim_size], old_shape[(last_dim + 1):]\n    ], 0)\n    return tf.reshape(tensor, output_shape)","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/projects/triviaqa/inputs.py#L40-L76","documentation":"Error \"`last_dim` out of bounds for `tensor` rank.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/projects/triviaqa/inputs.py:58 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}