Reshapes a tensor.
Aliases:
tf.compat.v1.manip.reshape
tf.compat.v1.reshape
tf.compat.v2.reshape
tf.reshape(
tensor,
shape,
name=None
)
Used in the guide:
T
ra
in
a
nd
e
va
lu
at
ew
i
th
K
er
as
Used in the tutorials:
I
ma
ge
c
ap
ti
on
in
gw
i
th
v
is
ua
la
t
te
nt
io
n``N
eu
ra
lm
a
ch
in
et
r
an
sl
at
io
nw
i
th
a
tt
en
ti
on
T
FR
ec
or
da
n
dt
f
.E
xa
mp
le
T
ra
ns
fo
rm
er
m
od
el
f
or
l
an
gu
ag
eu
n
de
rs
ta
nd
in
g`` Giventensor
, this operation returns atensor
that has the same values astensor
withshape
shape
. If one component ofshape
is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, ashape
of[-1]
flattens into 1-D. At most one component ofshape
can be -1. Ifshape
is 1-D or higher, then the operation returns atensor
withshape
shape
filled with the values oftensor
. In this case, the number of elements implied byshape
must 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 theshape
of the outputtensor
.name
: Aname
for the operation (optional).
Returns:
A Tensor
. Has the same type as tensor
.