<br />

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

Returns a tensor containing the shape of the input tensor.

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

### Used in the notebooks

| Used in the guide | Used in the tutorials |
|---|---|
| - [Effective Tensorflow 2](https://www.tensorflow.org/guide/effective_tf2) - [Ragged tensors](https://www.tensorflow.org/guide/ragged_tensor) - [Introduction to Tensors](https://www.tensorflow.org/guide/tensor) - [Customizing what happens in \`fit()\` with TensorFlow](https://www.tensorflow.org/guide/keras/custom_train_step_in_tensorflow) - [Customizing what happens in \`fit()\`](https://www.tensorflow.org/guide/keras/customizing_what_happens_in_fit) | - [DeepDream](https://www.tensorflow.org/tutorials/generative/deepdream) - [Neural style transfer](https://www.tensorflow.org/tutorials/generative/style_transfer) - [Transfer learning with YAMNet for environmental sound classification](https://www.tensorflow.org/tutorials/audio/transfer_learning_audio) - [pix2pix: Image-to-image translation with a conditional GAN](https://www.tensorflow.org/tutorials/generative/pix2pix) - [Playing CartPole with the Actor-Critic method](https://www.tensorflow.org/tutorials/reinforcement_learning/actor_critic) |

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

[`tf.shape`](https://www.tensorflow.org/api_docs/python/tf/shape) returns a 1-D integer tensor representing the shape of `input`.
For a scalar input, the tensor returned has a shape of (0,) and its value is
the empty vector (i.e. \[\]).

#### For example:

    tf.shape(1.)
    <tf.Tensor: shape=(0,), dtype=int32, numpy=array([], dtype=int32)>

    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)
    <... shape=(3,) dtype=int32...>

In these cases, using [`tf.Tensor.shape`](https://www.tensorflow.org/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/api_docs/python/tf/shape) and [`Tensor.shape`](https://www.tensorflow.org/api_docs/python/tf/Tensor#shape) should be identical in eager mode. Within
[`tf.function`](https://www.tensorflow.org/api_docs/python/tf/function) or within a [`compat.v1`](https://www.tensorflow.org/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/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/api_docs/python/tf#int32). (Note: there is an experimental flag, `tf_shape_default_int64` that changes the default to [`tf.int64`](https://www.tensorflow.org/api_docs/python/tf#int64). This is an unsupported, experimental setting that causes known breakages.) |
| `name` | A name for the operation (optional). |

<br />

<br />

<br />

<br />

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

<br />