tf.TensorSpec
Stay organized with collections
Save and categorize content based on your preferences.
Describes a tf.Tensor.
Inherits From: TypeSpec
tf.TensorSpec(
shape,
dtype=tf.dtypes.float32
,
name=None
)
Metadata for describing the tf.Tensor
objects accepted or returned
by some TensorFlow APIs.
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.
|
Attributes |
dtype
|
Returns the dtype of elements in the tensor.
|
name
|
Returns the (optionally provided) name of the described tensor.
|
shape
|
Returns the TensorShape that represents the shape of the tensor.
|
value_type
|
The Python type for values that are compatible with this TypeSpec.
|
Methods
from_spec
View source
@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
View source
@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
View source
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.
|
most_specific_compatible_type
View source
most_specific_compatible_type(
other
)
Returns the most specific TypeSpec compatible with self
and other
.
Raises |
ValueError
|
If there is no TypeSpec that is compatible with both self
and other .
|
__eq__
View source
__eq__(
other
)
Return self==value.
__ne__
View source
__ne__(
other
)
Return self!=value.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2022-11-04 UTC.
[null,null,["Last updated 2022-11-04 UTC."],[],[],null,["# tf.TensorSpec\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/tensor_spec.py#L117-L231) |\n\nDescribes a tf.Tensor.\n\nInherits From: [`TypeSpec`](../tf/TypeSpec)\n\n#### View aliases\n\n\n**Compat aliases for migration**\n\nSee\n[Migration guide](https://www.tensorflow.org/guide/migrate) for\nmore details.\n\n[`tf.compat.v1.TensorSpec`](https://www.tensorflow.org/api_docs/python/tf/TensorSpec)\n\n\u003cbr /\u003e\n\n tf.TensorSpec(\n shape,\n dtype=../tf/dtypes#float32,\n name=None\n )\n\nMetadata for describing the [`tf.Tensor`](../tf/Tensor) objects accepted or returned\nby some TensorFlow APIs.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|---------|---------------------------------------------------------------------------------------|\n| `shape` | Value convertible to [`tf.TensorShape`](../tf/TensorShape). The shape of the tensor. |\n| `dtype` | Value convertible to [`tf.DType`](../tf/dtypes/DType). The type of the tensor values. |\n| `name` | Optional name for the Tensor. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|-------------|--------------------------------------------------------------------------------------------------------------------------------------------|\n| `TypeError` | If shape is not convertible to a [`tf.TensorShape`](../tf/TensorShape), or dtype is not convertible to a [`tf.DType`](../tf/dtypes/DType). |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|--------------|--------------------------------------------------------------------|\n| `dtype` | Returns the `dtype` of elements in the tensor. |\n| `name` | Returns the (optionally provided) name of the described tensor. |\n| `shape` | Returns the `TensorShape` that represents the shape of the tensor. |\n| `value_type` | The Python type for values that are compatible with this TypeSpec. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `from_spec`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/tensor_spec.py#L142-L154) \n\n @classmethod\n from_spec(\n spec, name=None\n )\n\nReturns a `TensorSpec` with the same shape and dtype as `spec`. \n\n spec = tf.TensorSpec(shape=[8, 3], dtype=tf.int32, name=\"OriginalName\")\n tf.TensorSpec.from_spec(spec, \"NewName\")\n TensorSpec(shape=(8, 3), dtype=tf.int32, name='NewName')\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|--------|-------------------------------------------------------------|\n| `spec` | The `TypeSpec` used to create the new `TensorSpec`. |\n| `name` | The name for the new `TensorSpec`. Defaults to `spec.name`. |\n\n\u003cbr /\u003e\n\n### `from_tensor`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/tensor_spec.py#L156-L176) \n\n @classmethod\n from_tensor(\n tensor, name=None\n )\n\nReturns a `TensorSpec` that describes `tensor`. \n\n tf.TensorSpec.from_tensor(tf.constant([1, 2, 3]))\n TensorSpec(shape=(3,), dtype=tf.int32, name=None)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|----------|------------------------------------------------------------|\n| `tensor` | The [`tf.Tensor`](../tf/Tensor) that should be described. |\n| `name` | A name for the `TensorSpec`. Defaults to `tensor.op.name`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| A `TensorSpec` that describes `tensor`. ||\n\n\u003cbr /\u003e\n\n### `is_compatible_with`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/tensor_spec.py#L128-L140) \n\n is_compatible_with(\n spec_or_tensor\n )\n\nReturns True if spec_or_tensor is compatible with this TensorSpec.\n\nTwo tensors are considered compatible if they have the same dtype\nand their shapes are compatible (see [`tf.TensorShape.is_compatible_with`](../tf/TensorShape#is_compatible_with)).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|------------------|--------------------------------|\n| `spec_or_tensor` | A tf.TensorSpec or a tf.Tensor |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ||\n|---|---|\n| True if spec_or_tensor is compatible with self. ||\n\n\u003cbr /\u003e\n\n### `most_specific_compatible_type`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/tensor_spec.py#L96-L102) \n\n most_specific_compatible_type(\n other\n )\n\nReturns the most specific TypeSpec compatible with `self` and `other`.\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ||\n|---------|---------------|\n| `other` | A `TypeSpec`. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ||\n|--------------|--------------------------------------------------------------------------|\n| `ValueError` | If there is no TypeSpec that is compatible with both `self` and `other`. |\n\n\u003cbr /\u003e\n\n### `__eq__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/tensor_spec.py#L86-L91) \n\n __eq__(\n other\n )\n\nReturn self==value.\n\n### `__ne__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/tensor_spec.py#L93-L94) \n\n __ne__(\n other\n )\n\nReturn self!=value."]]