tf.experimental.Optional | TensorFlow v2.16.1 (original) (raw)
tf.experimental.Optional
Stay organized with collections Save and categorize content based on your preferences.
Represents a value that may or may not be present.
View aliases
Main aliases
Compat aliases for migration
SeeMigration guide for more details.
tf.compat.v1.data.experimental.Optional, tf.compat.v1.experimental.Optional
A tf.experimental.Optional can represent the result of an operation that may fail as a value, rather than raising an exception and halting execution. For example, tf.data.Iterator.get_next_as_optional() returns atf.experimental.Optional that either contains the next element of an iterator if one exists, or an "empty" value that indicates the end of the sequence has been reached.
tf.experimental.Optional can only be used with values that are convertible to tf.Tensor or tf.CompositeTensor
.
One can create a tf.experimental.Optional from a value using thefrom_value()
method:
optional = tf.experimental.Optional.from_value(42)
print(optional.has_value())
tf.Tensor(True, shape=(), dtype=bool)
print(optional.get_value())
tf.Tensor(42, shape=(), dtype=int32)
or without a value using the empty()
method:
optional = tf.experimental.Optional.empty(
tf.TensorSpec(shape=(), dtype=tf.int32, name=None))
print(optional.has_value())
tf.Tensor(False, shape=(), dtype=bool)
Attributes | |
---|---|
element_spec | The type specification of an element of this optional.optional = tf.experimental.Optional.from_value(42) print(optional.element_spec) tf.TensorSpec(shape=(), dtype=tf.int32, name=None) |
Methods
empty
@staticmethod
empty( element_spec )
Returns an Optional
that has no value.
optional = tf.experimental.Optional.empty(
tf.TensorSpec(shape=(), dtype=tf.int32, name=None))
print(optional.has_value())
tf.Tensor(False, shape=(), dtype=bool)
Args | |
---|---|
element_spec | A (nested) structure of tf.TypeSpec objects matching the structure of an element of this optional. |
Returns |
---|
A tf.experimental.Optional with no value. |
from_value
@staticmethod
from_value( value )
Returns a tf.experimental.Optional that wraps the given value.
optional = tf.experimental.Optional.from_value(42)
print(optional.has_value())
tf.Tensor(True, shape=(), dtype=bool)
print(optional.get_value())
tf.Tensor(42, shape=(), dtype=int32)
Args | |
---|---|
value | A value to wrap. The value must be convertible to tf.Tensor ortf.CompositeTensor. |
Returns |
---|
A tf.experimental.Optional that wraps value. |
get_value
@abc.abstractmethod
get_value( name=None )
Returns the value wrapped by this optional.
If this optional does not have a value (i.e. self.has_value()
evaluates toFalse
), this operation will raise tf.errors.InvalidArgumentError at runtime.
optional = tf.experimental.Optional.from_value(42)
print(optional.get_value())
tf.Tensor(42, shape=(), dtype=int32)
Args | |
---|---|
name | (Optional.) A name for the created operation. |
Returns |
---|
The wrapped value. |
has_value
@abc.abstractmethod
has_value( name=None )
Returns a tensor that evaluates to True
if this optional has a value.
optional = tf.experimental.Optional.from_value(42)
print(optional.has_value())
tf.Tensor(True, shape=(), dtype=bool)
Args | |
---|---|
name | (Optional.) A name for the created operation. |
Returns |
---|
A scalar tf.Tensor of type tf.bool. |