importtempfilepath=os.path.join(tempfile.gettempdir(),"saved_data")# Save a datasetdataset=tf.data.Dataset.range(2)tf.data.experimental.save(dataset,path)new_dataset=tf.data.experimental.load(path)foreleminnew_dataset:print(elem)tf.Tensor(0,shape=(),dtype=int64)tf.Tensor(1,shape=(),dtype=int64)
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:
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. Note that this
argument is required 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.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tf.data.experimental.load\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/data/experimental/ops/io.py#L108-L166) |\n\nLoads a previously saved dataset. (deprecated) \n\n tf.data.experimental.load(\n path, element_spec=None, compression=None, reader_func=None\n )\n\n| **Deprecated:** THIS FUNCTION IS DEPRECATED. It will be removed in a future version. Instructions for updating: Use [`tf.data.Dataset.load(...)`](../../../tf/data/Dataset#load) instead.\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\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. Note that this argument is required 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"]]