View source on GitHub |
Describes the type of a tf.Tensor.
Inherits From: TypeSpec
, TraceType
tf.TensorSpec(
shape,
dtype=tf.dtypes.float32
,
name=None
)
t = tf.constant([[1,2,3],[4,5,6]])
tf.TensorSpec.from_tensor(t)
TensorSpec(shape=(2, 3), dtype=tf.int32, name=None)
Contains metadata for describing the the nature of tf.Tensor
objects
accepted or returned by some TensorFlow APIs.
For example, it can be used to constrain the type of inputs accepted by a tf.function:
@tf.function(input_signature=[tf.TensorSpec([1, None])])
def constrained_foo(t):
print("tracing...")
return t
Now the tf.function
is able to assume that t
is always of the type
tf.TensorSpec([1, None])
which will avoid retracing as well as enforce the
type restriction on inputs.
As a result, the following call with tensor of type tf.TensorSpec([1, 2])
triggers a trace and succeeds:
>>> constrained_foo(tf.constant([[1., 2]])).numpy()
tracing...
array([[1., 2.]], dtype=float32)
The following subsequent call with tensor of type tf.TensorSpec([1, 4])
does not trigger a trace and succeeds:
>>> constrained_foo(tf.constant([[1., 2, 3, 4]])).numpy()
array([[1., 2., 3., 4.], dtype=float32)
But the following call with tensor of type tf.TensorSpec([2, 2])
fails:
>>> constrained_foo(tf.constant([[1., 2], [3, 4]])).numpy()
Traceback (most recent call last):
...
ValueError: Python inputs incompatible with input_signature
Args | |
---|---|
shape
|
Value convertible to tf.TensorShape . The shape of the tensor.
|
dtype
|
Value convertible to tf.DType . The type of the tensor values.
|
name
|
Optional name for the Tensor. |
Raises | |
---|---|
TypeError
|
If shape is not convertible to a tf.TensorShape , or dtype is
not convertible to a tf.DType .
|
Methods
experimental_as_proto
experimental_as_proto() -> struct_pb2.TensorSpecProto
Returns a proto representation of the TensorSpec instance.
experimental_from_proto
@classmethod
experimental_from_proto( proto: struct_pb2.TensorSpecProto ) -> 'TensorSpec'
Returns a TensorSpec instance based on the serialized proto.
experimental_type_proto
@classmethod
experimental_type_proto() -> Type[struct_pb2.TensorSpecProto]
Returns the type of proto associated with TensorSpec serialization.
from_spec
@classmethod
from_spec( spec, name=None )
Returns a TensorSpec
with the same shape and dtype as spec
.
spec = tf.TensorSpec(shape=[8, 3], dtype=tf.int32, name="OriginalName")
tf.TensorSpec.from_spec(spec, "NewName")
TensorSpec(shape=(8, 3), dtype=tf.int32, name='NewName')
Args | |
---|---|
spec
|
The TypeSpec used to create the new TensorSpec .
|
name
|
The name for the new TensorSpec . Defaults to spec.name .
|
from_tensor
@classmethod
from_tensor( tensor, name=None )
Returns a TensorSpec
that describes tensor
.
tf.TensorSpec.from_tensor(tf.constant([1, 2, 3]))
TensorSpec(shape=(3,), dtype=tf.int32, name=None)
Args | |
---|---|
tensor
|
The tf.Tensor that should be described.
|
name
|
A name for the TensorSpec . Defaults to tensor.op.name .
|
Returns | |
---|---|
A TensorSpec that describes tensor .
|
is_compatible_with
is_compatible_with(
spec_or_tensor
)
Returns True if spec_or_tensor is compatible with this TensorSpec.
Two tensors are considered compatible if they have the same dtype
and their shapes are compatible (see tf.TensorShape.is_compatible_with
).
Args | |
---|---|
spec_or_tensor
|
A tf.TensorSpec or a tf.Tensor |
Returns | |
---|---|
True if spec_or_tensor is compatible with self. |
is_subtype_of
is_subtype_of(
other: tf.types.experimental.TraceType
) -> bool
Returns True if self
is a subtype of other
.
Implements the tf.types.experimental.func.TraceType interface.
If not overridden by a subclass, the default behavior is to assume the TypeSpec is covariant upon attributes that implement TraceType and invariant upon rest of the attributes as well as the structure and type of the TypeSpec.
Args | |
---|---|
other
|
A TraceType object. |
most_specific_common_supertype
most_specific_common_supertype(
others: Sequence[tf.types.experimental.TraceType
]
) -> Optional['TypeSpec']
Returns the most specific supertype TypeSpec of self
and others
.
Implements the tf.types.experimental.func.TraceType interface.
If not overridden by a subclass, the default behavior is to assume the TypeSpec is covariant upon attributes that implement TraceType and invariant upon rest of the attributes as well as the structure and type of the TypeSpec.
Args | |
---|---|
others
|
A sequence of TraceTypes. |
most_specific_compatible_type
most_specific_compatible_type(
other: 'TypeSpec'
) -> 'TypeSpec'
Returns the most specific TypeSpec compatible with self
and other
. (deprecated)
Deprecated. Please use most_specific_common_supertype
instead.
Do not override this function.
Args | |
---|---|
other
|
A TypeSpec .
|
Raises | |
---|---|
ValueError
|
If there is no TypeSpec that is compatible with both self
and other .
|
__eq__
__eq__(
other
)
Return self==value.
__ne__
__ne__(
other
)
Return self!=value.