tf.data.experimental.load
Stay organized with collections
Save and categorize content based on your preferences.
Loads a previously saved dataset.
tf.data.experimental.load(
path, element_spec=None, compression=None, reader_func=None
)
Example usage:
import tempfile
path = os.path.join(tempfile.gettempdir(), "saved_data")
# Save a dataset
dataset = tf.data.Dataset.range(2)
tf.data.experimental.save(dataset, path)
new_dataset = tf.data.experimental.load(path)
for elem in new_dataset:
print(elem)
tf.Tensor(0, shape=(), dtype=int64)
tf.Tensor(1, shape=(), dtype=int64)
Note that to load a previously saved dataset, you need to specify
element_spec
-- a type signature of the elements of the saved dataset, which
can be obtained via tf.data.Dataset.element_spec
. This requirement exists so
that shape inference of the loaded dataset does not need to perform I/O.
If the default option of sharding the saved dataset was used, the element
order of the saved dataset will be preserved when loading it.
The reader_func
argument can be used to specify a custom order in which
elements should be loaded from the individual shards. The reader_func
is
expected to take a single argument -- a dataset of datasets, each containing
elements of one of the shards -- and return a dataset of elements. For
example, the order of shards can be shuffled when loading them as follows:
def custom_reader_func(datasets):
datasets = datasets.shuffle(NUM_SHARDS)
return datasets.interleave(lambda x: x, num_parallel_calls=AUTOTUNE)
dataset = tf.data.experimental.load(
path="/path/to/data", ..., reader_func=custom_reader_func)
Args |
path
|
Required. A path pointing to a previously saved dataset.
|
element_spec
|
Optional. A nested structure of tf.TypeSpec objects matching
the structure of an element of the saved dataset and specifying the type
of individual element components. If not provided, the nested structure of
tf.TypeSpec saved with the saved dataset is used. This argument needs to
be provided if the method is executed in graph mode.
|
compression
|
Optional. The algorithm to use to decompress the data when
reading it. Supported options are GZIP and NONE . Defaults to NONE .
|
reader_func
|
Optional. A function to control how to read data from shards.
If present, the function will be traced and executed as graph computation.
|
Raises |
FileNotFoundError
|
If element_spec is not specified and the saved nested
structure of tf.TypeSpec can not be located with the saved dataset.
|
ValueError
|
If element_spec is not specified and the method is executed
in graph mode.
|
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-03-17 UTC.
[null,null,["Last updated 2023-03-17 UTC."],[],[],null,["# tf.data.experimental.load\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.9.3/tensorflow/python/data/experimental/ops/io.py#L247-L314) |\n\nLoads a previously saved dataset. \n\n tf.data.experimental.load(\n path, element_spec=None, compression=None, reader_func=None\n )\n\n#### Example usage:\n\n import tempfile\n path = os.path.join(tempfile.gettempdir(), \"saved_data\")\n # Save a dataset\n dataset = tf.data.Dataset.range(2)\n tf.data.experimental.save(dataset, path)\n new_dataset = tf.data.experimental.load(path)\n for elem in new_dataset:\n print(elem)\n tf.Tensor(0, shape=(), dtype=int64)\n tf.Tensor(1, shape=(), dtype=int64)\n\nNote that to load a previously saved dataset, you need to specify\n`element_spec` -- a type signature of the elements of the saved dataset, which\ncan be obtained via [`tf.data.Dataset.element_spec`](../../../tf/data/Dataset#element_spec). This requirement exists so\nthat shape inference of the loaded dataset does not need to perform I/O.\n\nIf the default option of sharding the saved dataset was used, the element\norder of the saved dataset will be preserved when loading it.\n\nThe `reader_func` argument can be used to specify a custom order in which\nelements should be loaded from the individual shards. The `reader_func` is\nexpected to take a single argument -- a dataset of datasets, each containing\nelements of one of the shards -- and return a dataset of elements. For\nexample, the order of shards can be shuffled when loading them as follows: \n\n def custom_reader_func(datasets):\n datasets = datasets.shuffle(NUM_SHARDS)\n return datasets.interleave(lambda x: x, num_parallel_calls=AUTOTUNE)\n\n dataset = tf.data.experimental.load(\n path=\"/path/to/data\", ..., reader_func=custom_reader_func)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|----------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `path` | Required. A path pointing to a previously saved dataset. |\n| `element_spec` | Optional. A nested structure of [`tf.TypeSpec`](../../../tf/TypeSpec) objects matching the structure of an element of the saved dataset and specifying the type of individual element components. If not provided, the nested structure of [`tf.TypeSpec`](../../../tf/TypeSpec) saved with the saved dataset is used. This argument needs to be provided if the method is executed in graph mode. |\n| `compression` | Optional. The algorithm to use to decompress the data when reading it. Supported options are `GZIP` and `NONE`. Defaults to `NONE`. |\n| `reader_func` | Optional. A function to control how to read data from shards. If present, the function will be traced and executed as graph computation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A [`tf.data.Dataset`](../../../tf/data/Dataset) instance. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `FileNotFoundError` | If `element_spec` is not specified and the saved nested structure of [`tf.TypeSpec`](../../../tf/TypeSpec) can not be located with the saved dataset. |\n| `ValueError` | If `element_spec` is not specified and the method is executed in graph mode. |\n\n\u003cbr /\u003e"]]