Creates a new, uninitialized `Iterator` based on the given handle. This method allows you to define a "feedable" iterator where you can choose between concrete iterators by feeding a value in a `tf.Session.run` call. In that case, `string_handle` would be a `tf.compat.v1.placeholder`, a
(string_handle,
output_types,
output_shapes=None,
output_classes=None)
| 221 | |
| 222 | @staticmethod |
| 223 | def from_string_handle(string_handle, |
| 224 | output_types, |
| 225 | output_shapes=None, |
| 226 | output_classes=None): |
| 227 | """Creates a new, uninitialized `Iterator` based on the given handle. |
| 228 | |
| 229 | This method allows you to define a "feedable" iterator where you can choose |
| 230 | between concrete iterators by feeding a value in a `tf.Session.run` call. |
| 231 | In that case, `string_handle` would be a `tf.compat.v1.placeholder`, and you |
| 232 | would |
| 233 | feed it with the value of `tf.data.Iterator.string_handle` in each step. |
| 234 | |
| 235 | For example, if you had two iterators that marked the current position in |
| 236 | a training dataset and a test dataset, you could choose which to use in |
| 237 | each step as follows: |
| 238 | |
| 239 | ```python |
| 240 | train_iterator = tf.data.Dataset(...).make_one_shot_iterator() |
| 241 | train_iterator_handle = sess.run(train_iterator.string_handle()) |
| 242 | |
| 243 | test_iterator = tf.data.Dataset(...).make_one_shot_iterator() |
| 244 | test_iterator_handle = sess.run(test_iterator.string_handle()) |
| 245 | |
| 246 | handle = tf.compat.v1.placeholder(tf.string, shape=[]) |
| 247 | iterator = tf.data.Iterator.from_string_handle( |
| 248 | handle, train_iterator.output_types) |
| 249 | |
| 250 | next_element = iterator.get_next() |
| 251 | loss = f(next_element) |
| 252 | |
| 253 | train_loss = sess.run(loss, feed_dict={handle: train_iterator_handle}) |
| 254 | test_loss = sess.run(loss, feed_dict={handle: test_iterator_handle}) |
| 255 | ``` |
| 256 | |
| 257 | Args: |
| 258 | string_handle: A scalar `tf.Tensor` of type `tf.string` that evaluates to |
| 259 | a handle produced by the `Iterator.string_handle()` method. |
| 260 | output_types: A nested structure of `tf.DType` objects corresponding to |
| 261 | each component of an element of this dataset. |
| 262 | output_shapes: (Optional.) A nested structure of `tf.TensorShape` objects |
| 263 | corresponding to each component of an element of this dataset. If |
| 264 | omitted, each component will have an unconstrainted shape. |
| 265 | output_classes: (Optional.) A nested structure of Python `type` objects |
| 266 | corresponding to each component of an element of this iterator. If |
| 267 | omitted, each component is assumed to be of type `tf.Tensor`. |
| 268 | |
| 269 | Returns: |
| 270 | An `Iterator`. |
| 271 | """ |
| 272 | output_types = nest.map_structure(dtypes.as_dtype, output_types) |
| 273 | if output_shapes is None: |
| 274 | output_shapes = nest.map_structure( |
| 275 | lambda _: tensor_shape.TensorShape(None), output_types) |
| 276 | else: |
| 277 | output_shapes = nest.map_structure_up_to(output_types, |
| 278 | tensor_shape.as_shape, |
| 279 | output_shapes) |
| 280 | if output_classes is None: |