tf.shape

View source on GitHub

Returns the shape of a tensor.

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

This operation 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)>
>>> tf.shape(t).numpy()
array([2, 2, 3], dtype=int32)

Note: When using symbolic tensors, such as when using the Keras functional 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 will return more informative results.

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

Args:

Returns:

A Tensor of type out_type.