MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / from_generator

Method from_generator

tensorflow/python/data/ops/dataset_ops.py:527–729  ·  view source on GitHub ↗

Creates a `Dataset` whose elements are generated by `generator`. The `generator` argument must be a callable object that returns an object that supports the `iter()` protocol (e.g. a generator function). The elements generated by `generator` must be compatible with the given `output

(generator, output_types, output_shapes=None, args=None)

Source from the content-addressed store, hash-verified

525
526 @staticmethod
527 def from_generator(generator, output_types, output_shapes=None, args=None):
528 """Creates a `Dataset` whose elements are generated by `generator`.
529
530 The `generator` argument must be a callable object that returns
531 an object that supports the `iter()` protocol (e.g. a generator function).
532 The elements generated by `generator` must be compatible with the given
533 `output_types` and (optional) `output_shapes` arguments.
534
535 For example:
536
537 ```python
538 import itertools
539 tf.compat.v1.enable_eager_execution()
540
541 def gen():
542 for i in itertools.count(1):
543 yield (i, [1] * i)
544
545 ds = tf.data.Dataset.from_generator(
546 gen, (tf.int64, tf.int64), (tf.TensorShape([]), tf.TensorShape([None])))
547
548 for value in ds.take(2):
549 print value
550 # (1, array([1]))
551 # (2, array([1, 1]))
552 ```
553
554 NOTE: The current implementation of `Dataset.from_generator()` uses
555 `tf.numpy_function` and inherits the same constraints. In particular, it
556 requires the `Dataset`- and `Iterator`-related operations to be placed
557 on a device in the same process as the Python program that called
558 `Dataset.from_generator()`. The body of `generator` will not be
559 serialized in a `GraphDef`, and you should not use this method if you
560 need to serialize your model and restore it in a different environment.
561
562 NOTE: If `generator` depends on mutable global variables or other external
563 state, be aware that the runtime may invoke `generator` multiple times
564 (in order to support repeating the `Dataset`) and at any time
565 between the call to `Dataset.from_generator()` and the production of the
566 first element from the generator. Mutating global variables or external
567 state can cause undefined behavior, and we recommend that you explicitly
568 cache any external state in `generator` before calling
569 `Dataset.from_generator()`.
570
571 Args:
572 generator: A callable object that returns an object that supports the
573 `iter()` protocol. If `args` is not specified, `generator` must take no
574 arguments; otherwise it must take as many arguments as there are values
575 in `args`.
576 output_types: A nested structure of `tf.DType` objects corresponding to
577 each component of an element yielded by `generator`.
578 output_shapes: (Optional.) A nested structure of `tf.TensorShape` objects
579 corresponding to each component of an element yielded by `generator`.
580 args: (Optional.) A tuple of `tf.Tensor` objects that will be evaluated
581 and passed to `generator` as NumPy-array arguments.
582
583 Returns:
584 Dataset: A `Dataset`.

Calls 4

tupleFunction · 0.85
flattenMethod · 0.45
from_tensorsMethod · 0.45
flat_mapMethod · 0.45