tf.OptionalSpec
Stay organized with collections
Save and categorize content based on your preferences.
Type specification for tf.experimental.Optional
.
Inherits From: TypeSpec
tf.OptionalSpec(
element_spec
)
For instance, tf.OptionalSpec
can be used to define a tf.function that takes
tf.experimental.Optional
as an input argument:
@tf.function(input_signature=[tf.OptionalSpec(
tf.TensorSpec(shape=(), dtype=tf.int32, name=None))])
def maybe_square(optional):
if optional.has_value():
x = optional.get_value()
return x * x
return -1
optional = tf.experimental.Optional.from_value(5)
print(maybe_square(optional))
tf.Tensor(25, shape=(), dtype=int32)
Attributes |
element_spec
|
A (nested) structure of TypeSpec objects that represents the
type specification of the optional element.
|
value_type
|
The Python type for values that are compatible with this TypeSpec.
In particular, all values that are compatible with this TypeSpec must be an
instance of this type.
|
Methods
from_value
View source
@staticmethod
from_value(
value
)
is_compatible_with
View source
is_compatible_with(
spec_or_value
)
Returns true if spec_or_value
is compatible with this TypeSpec.
most_specific_compatible_type
View source
most_specific_compatible_type(
other: 'TypeSpec'
) -> 'TypeSpec'
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
) -> bool
Return self==value.
__ne__
View source
__ne__(
other
) -> bool
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.OptionalSpec\n\n\u003cbr /\u003e\n\n|------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/data/ops/optional_ops.py#L205-L263) |\n\nType specification for [`tf.experimental.Optional`](../tf/experimental/Optional).\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.OptionalSpec`](https://www.tensorflow.org/api_docs/python/tf/OptionalSpec), [`tf.compat.v1.data.experimental.OptionalStructure`](https://www.tensorflow.org/api_docs/python/tf/OptionalSpec)\n\n\u003cbr /\u003e\n\n tf.OptionalSpec(\n element_spec\n )\n\nFor instance, [`tf.OptionalSpec`](../tf/OptionalSpec) can be used to define a tf.function that takes\n[`tf.experimental.Optional`](../tf/experimental/Optional) as an input argument: \n\n @tf.function(input_signature=[tf.OptionalSpec(\n tf.TensorSpec(shape=(), dtype=tf.int32, name=None))])\n def maybe_square(optional):\n if optional.has_value():\n x = optional.get_value()\n return x * x\n return -1\n optional = tf.experimental.Optional.from_value(5)\n print(maybe_square(optional))\n tf.Tensor(25, shape=(), dtype=int32)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Attributes ---------- ||\n|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `element_spec` | A (nested) structure of `TypeSpec` objects that represents the type specification of the optional element. |\n| `value_type` | The Python type for values that are compatible with this TypeSpec. \u003cbr /\u003e In particular, all values that are compatible with this TypeSpec must be an instance of this type. |\n\n\u003cbr /\u003e\n\nMethods\n-------\n\n### `from_value`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/data/ops/optional_ops.py#L252-L254) \n\n @staticmethod\n from_value(\n value\n )\n\n### `is_compatible_with`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/type_spec.py#L103-L117) \n\n is_compatible_with(\n spec_or_value\n )\n\nReturns true if `spec_or_value` is compatible with this TypeSpec.\n\n### `most_specific_compatible_type`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/type_spec.py#L119-L141) \n\n most_specific_compatible_type(\n other: 'TypeSpec'\n ) -\u003e 'TypeSpec'\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/type_spec.py#L321-L324) \n\n __eq__(\n other\n ) -\u003e bool\n\nReturn self==value.\n\n### `__ne__`\n\n[View source](https://github.com/tensorflow/tensorflow/blob/v2.7.4/tensorflow/python/framework/type_spec.py#L326-L327) \n\n __ne__(\n other\n ) -\u003e bool\n\nReturn self!=value."]]