tf.data.experimental.from_list
Stay organized with collections
Save and categorize content based on your preferences.
Creates a Dataset
comprising the given list of elements.
tf.data.experimental.from_list(
elements, name=None
)
The returned dataset will produce the items in the list one by one. The
functionality is identical to Dataset.from_tensor_slices
when elements are
scalars, but different when elements have structure. Consider the following
example.
dataset = tf.data.experimental.from_list([(1, 'a'), (2, 'b'), (3, 'c')])
list(dataset.as_numpy_iterator())
[(1, b'a'), (2, b'b'), (3, b'c')]
To get the same output with from_tensor_slices
, the data needs to be
reorganized:
dataset = tf.data.Dataset.from_tensor_slices(([1, 2, 3], ['a', 'b', 'c']))
list(dataset.as_numpy_iterator())
[(1, b'a'), (2, b'b'), (3, b'c')]
Unlike from_tensor_slices
, from_list
supports non-rectangular input:
dataset = tf.data.experimental.from_list([[1], [2, 3]])
list(dataset.as_numpy_iterator())
[array([1], dtype=int32), array([2, 3], dtype=int32)]
Achieving the same with from_tensor_slices
requires the use of ragged
tensors.
from_list
can be more performant than from_tensor_slices
in some cases,
since it avoids the need for data slicing each epoch. However, it can also be
less performant, because data is stored as many small tensors rather than a
few large tensors as in from_tensor_slices
. The general guidance is to
prefer from_list
from a performance perspective when the number of elements
is small (less than 1000).
Args |
elements
|
A list of elements whose components have the same nested
structure.
|
name
|
(Optional.) A name for the tf.data operation.
|
Returns |
Dataset
|
A Dataset of the elements .
|
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 2023-10-06 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2023-10-06 UTC."],[],[],null,["# tf.data.experimental.from_list\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/data/experimental/ops/from_list.py#L75-L119) |\n\nCreates a `Dataset` comprising the given list of elements.\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.data.experimental.from_list`](https://www.tensorflow.org/api_docs/python/tf/data/experimental/from_list)\n\n\u003cbr /\u003e\n\n tf.data.experimental.from_list(\n elements, name=None\n )\n\nThe returned dataset will produce the items in the list one by one. The\nfunctionality is identical to [`Dataset.from_tensor_slices`](../../../tf/data/Dataset#from_tensor_slices) when elements are\nscalars, but different when elements have structure. Consider the following\nexample. \n\n dataset = tf.data.experimental.from_list([(1, 'a'), (2, 'b'), (3, 'c')])\n list(dataset.as_numpy_iterator())\n [(1, b'a'), (2, b'b'), (3, b'c')]\n\nTo get the same output with `from_tensor_slices`, the data needs to be\nreorganized: \n\n dataset = tf.data.Dataset.from_tensor_slices(([1, 2, 3], ['a', 'b', 'c']))\n list(dataset.as_numpy_iterator())\n [(1, b'a'), (2, b'b'), (3, b'c')]\n\nUnlike `from_tensor_slices`, `from_list` supports non-rectangular input: \n\n dataset = tf.data.experimental.from_list([[1], [2, 3]])\n list(dataset.as_numpy_iterator())\n [array([1], dtype=int32), array([2, 3], dtype=int32)]\n\nAchieving the same with `from_tensor_slices` requires the use of ragged\ntensors.\n\n`from_list` can be more performant than `from_tensor_slices` in some cases,\nsince it avoids the need for data slicing each epoch. However, it can also be\nless performant, because data is stored as many small tensors rather than a\nfew large tensors as in `from_tensor_slices`. The general guidance is to\nprefer `from_list` from a performance perspective when the number of elements\nis small (less than 1000).\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------|---------------------------------------------------------------------|\n| `elements` | A list of elements whose components have the same nested structure. |\n| `name` | (Optional.) A name for the tf.data operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|-----------|--------------------------------|\n| `Dataset` | A `Dataset` of the `elements`. |\n\n\u003cbr /\u003e"]]