x=tf.compat.v1.placeholder(tf.int32)print(x.shape)==> TensorShape(None)y=x*2print(y.shape)==> TensorShape(None)y=tf.ensure_shape(y,(None,3,3))print(y.shape)==> TensorShape([Dimension(None),Dimension(3),Dimension(3)])withtf.compat.v1.Session()assess:# 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]})
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".
Returns
A Tensor. Has the same type and contents as x. At runtime, raises a
tf.errors.InvalidArgumentError if shape is incompatible with the shape
of x.
[null,null,["Last updated 2020-10-01 UTC."],[],[],null,["# tf.ensure_shape\n\n\u003cbr /\u003e\n\n|-------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------|\n| [TensorFlow 1 version](/versions/r1.15/api_docs/python/tf/ensure_shape) | [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.1.0/tensorflow/python/ops/check_ops.py#L2105-L2150) |\n\nUpdates the shape of a tensor and checks at runtime that the shape holds.\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.ensure_shape`](/api_docs/python/tf/ensure_shape)\n\n\u003cbr /\u003e\n\n tf.ensure_shape(\n x, shape, name=None\n )\n\n#### For example:\n\n x = tf.compat.v1.placeholder(tf.int32)\n print(x.shape)\n ==\u003e TensorShape(None)\n y = x * 2\n print(y.shape)\n ==\u003e TensorShape(None)\n\n y = tf.ensure_shape(y, (None, 3, 3))\n print(y.shape)\n ==\u003e TensorShape([Dimension(None), Dimension(3), Dimension(3)])\n\n with tf.compat.v1.Session() as sess:\n # Raises tf.errors.InvalidArgumentError, because the shape (3,) is not\n # compatible with the shape (None, 3, 3)\n sess.run(y, feed_dict={x: [1, 2, 3]})\n\n| **Note:** This differs from [`Tensor.set_shape`](../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`](../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.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|--------------------------------------------------------------------------------------------------------|\n| `x` | A `Tensor`. |\n| `shape` | A `TensorShape` representing the shape of this tensor, a `TensorShapeProto`, a list, a tuple, or None. |\n| `name` | A name for this operation (optional). Defaults to \"EnsureShape\". |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A `Tensor`. Has the same type and contents as `x`. At runtime, raises a [`tf.errors.InvalidArgumentError`](../tf/errors/InvalidArgumentError) if `shape` is incompatible with the shape of `x`. ||\n\n\u003cbr /\u003e"]]