Class TensorShape
Represents the shape of a Tensor
.
Aliases:
- Class
tf.compat.v1.TensorShape
- Class
tf.compat.v2.TensorShape
Used in the guide:
M
ig
ra
te
y
ou
rT
e
ns
or
Fl
ow
1
c
od
et
o
T
en
so
rF
lo
w2``
R
ec
ur
re
nt
N
eu
ra
lN
e
tw
or
ks
(
RN
N)
w
it
hK
e
ra
s``
Used in the tutorials:
T
ex
tg
e
ne
ra
ti
on
w
it
ha
n
R
NN
ATensorShape
represents a possibly-partial shape specification for aTensor
. It may be one of the following:- Fully-known shape: has a known number of dimensions and a known size for each dimension. e.g. TensorShape([16, 256])
- Partially-known shape: has a known number of dimensions, and an unknown size for one or more dimension. e.g. TensorShape([None, 256])
- Unknown shape: has an unknown number of dimensions, and an unknown size in all dimensions. e.g. TensorShape(None) Shape functionsIf a tensor is produced by an operation of type "Foo", its shape may be inferred if there is a registered shape function for "Foo". See for details of shape functions and how to register them. Alternatively, the shape may be set explicitly using tf.Tensor.set_shape.
init
__init__(dims)
Creates a new TensorShape with the given dimensions.
Args:
dims
: A list of Dimensions, or None if the shape is unspecified.
Raises:
TypeError
: If dims cannot be converted to a list of dimensions.
Properties
dims
Returns a list of Dimensions, or None if the shape is unspecified.
ndims
Deprecated accessor for rank
.
rank
Returns the rank of this shape, or None if it is unspecified.
Methods
add
__add__(other)
bool
__bool__()
Returns True if this shape contains non-zero information.
concat
__concat__(other)
eq
__eq__(other)
Returns True if self
is equivalent to other
.
getitem
__getitem__(key)
Returns the value of a dimension or a shape, depending on the key.
Args:
key
: Ifkey
is an integer, returns the dimension at that index; otherwise ifkey
is a slice, returns a TensorShape whose dimensions are those selected by the slice fromself
.
Returns:
An integer if key
is an integer, or a TensorShape
if key
is a slice.
Raises:
ValueError
: Ifkey
is a slice andself
is completely unknown and the step is set.
iter
__iter__()
Returns self.dims
if the rank is known, otherwise raises ValueError.
len
__len__()
Returns the rank of this shape, or raises ValueError if unspecified.
ne
__ne__(other)
Returns True if self
is known to be different from other
.
nonzero
__nonzero__()
Returns True if this shape contains non-zero information.
radd
__radd__(other)
as_list
as_list()
Returns a list of integers or None
for each dimension.
Returns:
A list of integers or None
for each dimension.
Raises:
ValueError
: Ifself
is an unknown shape with an unknown rank.
as_proto
as_proto()
Returns this shape as a TensorShapeProto
.
assert_has_rank
assert_has_rank(rank)
Raises an exception if self
is not compatible with the given rank
.
Args:
rank
: An integer.
Raises:
ValueError
: Ifself
does not represent a shape with the givenrank
.
assert_is_compatible_with
assert_is_compatible_with(other)
Raises exception if self
and other
do not represent the same shape.
This method can be used to assert that there exists a shape that both self
and other
represent.
Args:
other
: Another
TensorShape.
Raises:
ValueError
: Ifself
andother
do not represent the same shape.
assert_is_fully_defined
assert_is_fully_defined()
Raises an exception if self
is not fully defined in every dimension.
Raises:
ValueError
: Ifself
does not have a known value for every dimension.
assert_same_rank
assert_same_rank(other)
Raises an exception if self
and other
do not have compatible ranks.
Args:
other
: Another
TensorShape
.
Raises:
ValueError
: Ifself
andother
do not represent shapes with the same rank.
concatenate
concatenate(other)
Returns the concatenation of the dimension in self
and other
.
N.B. If either self
or other
is completely unknown, concatenation will discard information about the other
shape. In future, we might support concatenation that preserves this information for use with slicing.
Args:
other
: Another
TensorShape
.
Returns:
A TensorShape
whose dimensions are the concatenation of the dimensions in self
and other
.
is_compatible_with
is_compatible_with(other)
Returns True iff self
is compatible with other
.
Two possibly-partially-defined shapes are compatible if there exists a fully-defined shape that both shapes can represent. Thus, compatibility allows the shape inference code to reason about partially-defined shapes. For example:
T
en
so
rS
ha
pe
(N
on
e)
i
sc
o
mp
at
ib
le
w
it
ha
l
ls
h
ap
es
.``T
en
so
rS
ha
pe
([
No
ne
,N
o
ne
])
i
sc
o
mp
at
ib
le
w
it
ha
l
lt
w
o-
di
me
ns
io
na
ls
h
ap
es
,s
u
ch
a
sT
e
ns
or
Sh
ap
e(
[3
2,
7
84
])
,a
n
da
l
so
T
en
so
rS
ha
pe
(N
on
e)
.I
t
i
sn
o
tc
o
mp
at
ib
le
w
it
h,
f
or
e
xa
mp
le
,T
e
ns
or
Sh
ap
e(
[N
on
e]
)o
r
T
en
so
rS
ha
pe
([
No
ne
,N
o
ne
,N
o
ne
])
.``T
en
so
rS
ha
pe
([
32
,N
o
ne
])
i
sc
o
mp
at
ib
le
w
it
ha
l
lt
w
o-
di
me
ns
io
na
ls
h
ap
es
w
it
hs
i
ze
3
2i
n
t
he
0
th
d
im
en
si
on
,a
n
da
l
so
T
en
so
rS
ha
pe
([
No
ne
,N
o
ne
])
a
nd
T
en
so
rS
ha
pe
(N
on
e)
.I
t
i
sn
o
tc
o
mp
at
ib
le
w
it
h,
f
or
e
xa
mp
le
,T
e
ns
or
Sh
ap
e(
[3
2]
),
T
en
so
rS
ha
pe
([
32
,N
o
ne
,1
]
)o
r
T
en
so
rS
ha
pe
([
64
,N
o
ne
])
.``T
en
so
rS
ha
pe
([
32
,7
8
4]
)i
s
c
om
pa
ti
bl
ew
i
th
i
ts
el
f,
a
nd
a
ls
oT
e
ns
or
Sh
ap
e(
[3
2,
N
on
e]
),
T
en
so
rS
ha
pe
([
No
ne
,7
8
4]
),
T
en
so
rS
ha
pe
([
No
ne
,N
o
ne
])
a
nd
T
en
so
rS
ha
pe
(N
on
e)
.I
t
i
sn
o
tc
o
mp
at
ib
le
w
it
h,
f
or
e
xa
mp
le
,T
e
ns
or
Sh
ap
e(
[3
2,
1
,7
8
4]
)o
r
T
en
so
rS
ha
pe
([
No
ne
])
.`` The compatibility relation is reflexive and symmetric, but not transitive. For example, TensorShape([32, 784]) is compatible with TensorShape(None), and TensorShape(None) is compatible with TensorShape([4, 4]), but TensorShape([32, 784]) is not compatible with TensorShape([4, 4]).
Args:
other
: Another
TensorShape.
Returns:
True iff self
is compatible with other
.
is_fully_defined
is_fully_defined()
Returns True iff self
is fully defined in every dimension.
merge_with
merge_with(other)
Returns a TensorShape
combining the information in self
and other
.
The dimensions in self
and other
are merged elementwise, according to the rules defined for Dimension.merge_with
().
Args:
other
: Another
TensorShape
.
Returns:
A TensorShape
containing the combined information of self
and other
.
Raises:
ValueError
: Ifself
andother
are not compatible.
most_specific_compatible_shape
most_specific_compatible_shape(other)
Returns the most specific TensorShape compatible with self
and other
.
T
en
so
rS
ha
pe
([
No
ne
,1
]
)i
s
t
he
m
os
ts
p
ec
if
ic
T
en
so
rS
ha
pe
c
om
pa
ti
bl
ew
i
th
b
ot
hT
e
ns
or
Sh
ap
e(
[2
,1
]
)a
n
dT
e
ns
or
Sh
ap
e(
[5
,1
]
).
N
ot
et
h
at
T
en
so
rS
ha
pe
(N
on
e)
i
sa
l
so
c
om
pa
ti
bl
ew
i
th
a
bo
ve
m
en
ti
on
ed
T
en
so
rS
ha
pe
s.
T
en
so
rS
ha
pe
([
1,
2
,3
]
)i
s
t
he
m
os
ts
p
ec
if
ic
T
en
so
rS
ha
pe
c
om
pa
ti
bl
ew
i
th
b
ot
hT
e
ns
or
Sh
ap
e(
[1
,2
,
3
])
a
nd
T
en
so
rS
ha
pe
([
1,
2
,3
]
).
T
he
re
a
re
m
or
el
e
ss
s
pe
ci
fi
cT
e
ns
or
Sh
ap
es
c
om
pa
ti
bl
ew
i
th
a
bo
ve
m
en
ti
on
ed
T
en
so
rS
ha
pe
s,
e
.g
.T
e
ns
or
Sh
ap
e(
[1
,2
,
N
on
e]
),
T
en
so
rS
ha
pe
(N
on
e)
.``
Args:
other
: Another
TensorShape
.
Returns:
A TensorShape
which is the most specific compatible shape of self
and other
.
num_elements
num_elements()
Returns the total number of elements, or none for incomplete shapes.
with_rank
with_rank(rank)
Returns a shape based on self
with the given rank.
This method promotes a completely unknown shape to one with a known rank.
Args:
rank
: An integer.
Returns:
A shape that is at least as specific as self
with the given rank.
Raises:
ValueError
: Ifself
does not represent a shape with the givenrank
.
with_rank_at_least
with_rank_at_least(rank)
Returns a shape based on self
with at least the given rank.
Args:
rank
: An integer.
Returns:
A shape that is at least as specific as self
with at least the given rank.
Raises:
ValueError
: Ifself
does not represent a shape with at least the givenrank
.
with_rank_at_most
with_rank_at_most(rank)
Returns a shape based on self
with at most the given rank.
Args:
rank
: An integer.
Returns:
A shape that is at least as specific as self
with at most the given rank.
Raises:
ValueError
: Ifself
does not represent a shape with at most the givenrank
.