Reshapes a tensor.
Aliases:
tf.compat.v1.manip.reshapetf.compat.v1.reshapetf.compat.v2.reshape
tf.reshape(
tensor,
shape,
name=None
)
Used in the guide:
TrainandevaluatewithKeras
Used in the tutorials:
Imagecaptioningwithvisualattention``NeuralmachinetranslationwithattentionTFRecordandtf.ExampleTransformermodelforlanguageunderstanding`` Giventensor, this operation returns atensorthat has the same values astensorwithshapeshape. If one component ofshapeis the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, ashapeof[-1]flattens into 1-D. At most one component ofshapecan be -1. Ifshapeis 1-D or higher, then the operation returns atensorwithshapeshapefilled with the values oftensor. In this case, the number of elements implied byshapemust be the same as the number of elements intensor.
For example:
# tensor 't' is [1, 2, 3, 4, 5, 6, 7, 8, 9]
# tensor 't' has shape [9]
reshape(t, [3, 3]) ==> [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# tensor 't' is [[[1, 1], [2, 2]],
# [[3, 3], [4, 4]]]
# tensor 't' has shape [2, 2, 2]
reshape(t, [2, 4]) ==> [[1, 1, 2, 2],
[3, 3, 4, 4]]
# tensor 't' is [[[1, 1, 1],
# [2, 2, 2]],
# [[3, 3, 3],
# [4, 4, 4]],
# [[5, 5, 5],
# [6, 6, 6]]]
# tensor 't' has shape [3, 2, 3]
# pass '[-1]' to flatten 't'
reshape(t, [-1]) ==> [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6]
# -1 can also be used to infer the shape
# -1 is inferred to be 9:
reshape(t, [2, -1]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 2:
reshape(t, [-1, 9]) ==> [[1, 1, 1, 2, 2, 2, 3, 3, 3],
[4, 4, 4, 5, 5, 5, 6, 6, 6]]
# -1 is inferred to be 3:
reshape(t, [ 2, -1, 3]) ==> [[[1, 1, 1],
[2, 2, 2],
[3, 3, 3]],
[[4, 4, 4],
[5, 5, 5],
[6, 6, 6]]]
# tensor 't' is [7]
# shape `[]` reshapes to a scalar
reshape(t, []) ==> 7
Args:
tensor: ATensor.shape: ATensor. Must be one of the following types:int32,int64. Defines theshapeof the outputtensor.name: Anamefor the operation (optional).
Returns:
A Tensor. Has the same type as tensor.