# tf.rank

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/ops/array_ops.py#L844-L877) |

Returns the rank of a tensor.

#### View aliases


**Compat aliases for migration**

See
[Migration guide](https://www.tensorflow.org/guide/migrate) for
more details.

[`tf.compat.v1.rank`](https://www.tensorflow.org/api_docs/python/tf/rank)

<br />

    tf.rank(
        input, name=None
    )

See also [`tf.shape`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/shape).

Returns a 0-D `int32` `Tensor` representing the rank of `input`.

#### For example:

    # shape of tensor 't' is [2, 2, 3]
    t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
    tf.rank(t)  # 3

> [!NOTE]
> **Note:** The rank of a tensor is not the same as the rank of a matrix. The rank of a tensor is the number of indices required to uniquely select each element of the tensor. Rank is also known as "order", "degree", or "ndims."

<br />

<br />

<br />

| ## Args ||
|---|---|
| `input` | A `Tensor` or `SparseTensor`. |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `Tensor` of type `int32`. ||

<br />

<br />

## numpy compatibility

<br />

Equivalent to np.ndim

<br />

<br />