# tf.shape

<br />

|---|---|
| [![](https://www.tensorflow.org/images/tf_logo_32px.png) TensorFlow 1 version](https://www.tensorflow.org/versions/r1.15/api_docs/python/tf/shape) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.3.0/tensorflow/python/ops/array_ops.py#L557-L601) |

Returns the shape of a tensor.

    tf.shape(
        input, out_type=tf.dtypes.int32, name=None
    )

See also [`tf.size`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/size), [`tf.rank`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/rank).

[`tf.shape`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/shape) returns a 1-D integer tensor representing the shape of `input`.

#### For example:

    t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
    tf.shape(t)
    <tf.Tensor: shape=(3,), dtype=int32, numpy=array([2, 2, 3], dtype=int32)>

> [!NOTE]
> **Note:** When using symbolic tensors, such as when using the Keras API, tf.shape() will return the shape of the symbolic tensor.

    a = tf.keras.layers.Input((None, 10))
    tf.shape(a)
    <tf.Tensor ... shape=(3,) dtype=int32>

In these cases, using [`tf.Tensor.shape`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/Tensor#shape) will return more informative results.

    a.shape
    TensorShape([None, None, 10])

(The first `None` represents the as yet unknown batch size.)

[`tf.shape`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/shape) and [`Tensor.shape`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/Tensor#shape) should be identical in eager mode. Within
[`tf.function`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/function) or within a [`compat.v1`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/compat/v1) context, not all dimensions may be
known until execution time. Hence when defining custom layers and models
for graph mode, prefer the dynamic [`tf.shape(x)`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf/shape) over the static `x.shape`.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `input` | A `Tensor` or `SparseTensor`. |
| `out_type` | (Optional) The specified output type of the operation (`int32` or `int64`). Defaults to [`tf.int32`](https://www.tensorflow.org/versions/r2.3/api_docs/python/tf#int32). |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

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

<br />