Updates the shape of a tensor and checks at runtime that the shape holds.
Aliases:
tf.compat.v1.ensure_shape
tf.compat.v2.ensure_shape
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]})
Tensor.set_shapeNOTE: This differs from 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. 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.
Args:
x
: ATensor
.shape
: ATensor
Shape representing theshape
of this tensor, aTensor
ShapeProto, a list, a tuple, or None.name
: Aname
for this operation (optional). Defaults to "EnsureShape".
Returns:
tf.errors.InvalidArgumentErrorA Tensor. Has the same type and contents as x. At runtime, raises a if shape is incompatible with the shape of x.