# tf.ensure_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/ensure_shape) | [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/ops/check_ops.py#L2105-L2150) |

Updates the shape of a tensor and checks at runtime that the shape holds.

#### View aliases


**Compat aliases for migration**

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

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

<br />

    tf.ensure_shape(
        x, shape, name=None
    )

#### For example:

    x = tf.compat.v1.placeholder(tf.int32)
    print(x.shape)
    ==> TensorShape(None)
    y = x * 2
    print(y.shape)
    ==> TensorShape(None)

    y = tf.ensure_shape(y, (None, 3, 3))
    print(y.shape)
    ==> TensorShape([Dimension(None), Dimension(3), Dimension(3)])

    with tf.compat.v1.Session() as sess:
      # Raises tf.errors.InvalidArgumentError, because the shape (3,) is not
      # compatible with the shape (None, 3, 3)
      sess.run(y, feed_dict={x: [1, 2, 3]})

> [!NOTE]
> **Note:** This differs from [`Tensor.set_shape`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/Tensor#set_shape) in that it sets the static shape of the resulting tensor and enforces it at runtime, raising an error if the tensor's runtime shape is incompatible with the specified shape. [`Tensor.set_shape`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/Tensor#set_shape) sets the static shape of the tensor without enforcing it at runtime, which may result in inconsistencies between the statically-known shape of tensors and the runtime value of tensors.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `x` | A `Tensor`. |
| `shape` | A `TensorShape` representing the shape of this tensor, a `TensorShapeProto`, a list, a tuple, or None. |
| `name` | A name for this operation (optional). Defaults to "EnsureShape". |

<br />

<br />

<br />

<br />

| ## Returns ||
|---|---|
| A `Tensor`. Has the same type and contents as `x`. At runtime, raises a [`tf.errors.InvalidArgumentError`](https://www.tensorflow.org/versions/r2.1/api_docs/python/tf/errors/InvalidArgumentError) if `shape` is incompatible with the shape of `x`. ||

<br />