Concrete implementation of `tf.data.experimental.Optional`. NOTE(mrry): This implementation is kept private, to avoid defining `Optional.__init__()` in the public API.
| 122 | |
| 123 | |
| 124 | class _OptionalImpl(Optional): |
| 125 | """Concrete implementation of `tf.data.experimental.Optional`. |
| 126 | |
| 127 | NOTE(mrry): This implementation is kept private, to avoid defining |
| 128 | `Optional.__init__()` in the public API. |
| 129 | """ |
| 130 | |
| 131 | def __init__(self, variant_tensor, value_structure): |
| 132 | self._variant_tensor = variant_tensor |
| 133 | self._value_structure = value_structure |
| 134 | |
| 135 | def has_value(self, name=None): |
| 136 | return gen_dataset_ops.optional_has_value(self._variant_tensor, name=name) |
| 137 | |
| 138 | def get_value(self, name=None): |
| 139 | # TODO(b/110122868): Consolidate the restructuring logic with similar logic |
| 140 | # in `Iterator.get_next()` and `StructuredFunctionWrapper`. |
| 141 | with ops.name_scope(name, "OptionalGetValue", |
| 142 | [self._variant_tensor]) as scope: |
| 143 | return structure.from_tensor_list( |
| 144 | self._value_structure, |
| 145 | gen_dataset_ops.optional_get_value( |
| 146 | self._variant_tensor, |
| 147 | name=scope, |
| 148 | output_types=structure.get_flat_tensor_types( |
| 149 | self._value_structure), |
| 150 | output_shapes=structure.get_flat_tensor_shapes( |
| 151 | self._value_structure))) |
| 152 | |
| 153 | @property |
| 154 | def value_structure(self): |
| 155 | return self._value_structure |
| 156 | |
| 157 | @property |
| 158 | def _type_spec(self): |
| 159 | return OptionalSpec.from_value(self) |
| 160 | |
| 161 | |
| 162 | @tf_export( |
no outgoing calls
no test coverage detected