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. []).
In these cases, using tf.Tensor.shape will return more informative results.
a.shapeTensorShape([None,None,10])
(The first None represents the as yet unknown batch size.)
tf.shape and Tensor.shape should be identical in eager mode. Within
tf.function or within a 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) over the static x.shape.
Args
input
A Tensor or SparseTensor.
out_type
(Optional) The specified output type of the operation (int32 or
int64). Defaults to tf.int32. (Note: there is an experimental
flag, tf_shape_default_int64 that changes the default to tf.int64.
This is an unsupported, experimental setting that causes known breakages.)
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.shape\n\n\u003cbr /\u003e\n\n|-----------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/array_ops.py#L596-L654) |\n\nReturns a tensor containing the shape of the input tensor. \n\n tf.shape(\n input, out_type=None, name=None\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [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) |\n\nSee also [`tf.size`](../tf/size), [`tf.rank`](../tf/rank).\n\n[`tf.shape`](../tf/shape) returns a 1-D integer tensor representing the shape of `input`.\nFor a scalar input, the tensor returned has a shape of (0,) and its value is\nthe empty vector (i.e. \\[\\]).\n\n#### For example:\n\n tf.shape(1.)\n \u003ctf.Tensor: shape=(0,), dtype=int32, numpy=array([], dtype=int32)\u003e\n\n t = tf.constant([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])\n tf.shape(t)\n \u003ctf.Tensor: shape=(3,), dtype=int32, numpy=array([2, 2, 3], dtype=int32)\u003e\n\n**Note:** When using symbolic tensors, such as when using the Keras API, tf.shape() will return the shape of the symbolic tensor. \n\n a = tf.keras.layers.Input((None, 10))\n tf.shape(a)\n \u003c... shape=(3,) dtype=int32...\u003e\n\nIn these cases, using [`tf.Tensor.shape`](../tf/Tensor#shape) will return more informative results. \n\n a.shape\n TensorShape([None, None, 10])\n\n(The first `None` represents the as yet unknown batch size.)\n\n[`tf.shape`](../tf/shape) and [`Tensor.shape`](../tf/Tensor#shape) should be identical in eager mode. Within\n[`tf.function`](../tf/function) or within a [`compat.v1`](../tf/compat/v1) context, not all dimensions may be\nknown until execution time. Hence, when defining custom layers and models\nfor graph mode, prefer the dynamic [`tf.shape(x)`](../tf/shape) over the static `x.shape`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `input` | A `Tensor` or `SparseTensor`. |\n| `out_type` | (Optional) The specified output type of the operation (`int32` or `int64`). Defaults to [`tf.int32`](../tf#int32). (Note: there is an experimental flag, `tf_shape_default_int64` that changes the default to [`tf.int64`](../tf#int64). This is an unsupported, experimental setting that causes known breakages.) |\n| `name` | A name for the operation (optional). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor` of type `out_type`. ||\n\n\u003cbr /\u003e"]]