tfds.as_numpy
Stay organized with collections
Save and categorize content based on your preferences.
Converts a tf.data.Dataset
to an iterable of NumPy arrays.
tfds.as_numpy(
dataset: Tree[TensorflowElem]
) -> Tree[NumpyElem]
Used in the notebooks
as_numpy
converts a possibly nested structure of tf.data.Dataset
s
and tf.Tensor
s to iterables of NumPy arrays and NumPy arrays, respectively.
Note that because TensorFlow has support for ragged tensors and NumPy has
no equivalent representation,
tf.RaggedTensor
s
are left as-is for the user to deal with them (e.g. using to_list()
).
In TF 1 (i.e. graph mode), tf.RaggedTensor
s are returned as
tf.ragged.RaggedTensorValue
s.
Example:
ds = tfds.load(name="mnist", split="train")
ds_numpy = tfds.as_numpy(ds) # Convert `tf.data.Dataset` to Python generator
for ex in ds_numpy:
# `{'image': np.array(shape=(28, 28, 1)), 'labels': np.array(shape=())}`
print(ex)
Returns |
A structure matching dataset where tf.data.Dataset s are converted to
generators of NumPy arrays and tf.Tensor s are converted to NumPy arrays.
|
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.
Last updated 2024-04-26 UTC.
[null,null,["Last updated 2024-04-26 UTC."],[],[],null,["# tfds.as_numpy\n\n\u003cbr /\u003e\n\n|---------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/datasets/blob/v4.9.3/tensorflow_datasets/core/dataset_utils.py#L166-L202) |\n\nConverts a [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) to an iterable of NumPy arrays. \n\n tfds.as_numpy(\n dataset: Tree[TensorflowElem]\n ) -\u003e Tree[NumpyElem]\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [TensorFlow Datasets](https://www.tensorflow.org/datasets/overview) - [Text Classification with Movie Reviews](https://www.tensorflow.org/hub/tutorials/tf2_text_classification) - [Assess privacy risks with the TensorFlow Privacy Report](https://www.tensorflow.org/responsible_ai/privacy/tutorials/privacy_report) - [Distributed Inference with JAX](https://www.tensorflow.org/probability/examples/Distributed_Inference_with_JAX) - [Generating Images with BigBiGAN](https://www.tensorflow.org/hub/tutorials/bigbigan_with_tf_hub) |\n\n`as_numpy` converts a possibly nested structure of [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset)s\nand [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor)s to iterables of NumPy arrays and NumPy arrays, respectively.\n\nNote that because TensorFlow has support for ragged tensors and NumPy has\nno equivalent representation,\n[`tf.RaggedTensor`s](https://www.tensorflow.org/api_docs/python/tf/RaggedTensor)\nare left as-is for the user to deal with them (e.g. using `to_list()`).\nIn TF 1 (i.e. graph mode), [`tf.RaggedTensor`](https://www.tensorflow.org/api_docs/python/tf/RaggedTensor)s are returned as\n`tf.ragged.RaggedTensorValue`s.\n\n#### Example:\n\n ds = tfds.load(name=\"mnist\", split=\"train\")\n ds_numpy = tfds.as_numpy(ds) # Convert `tf.data.Dataset` to Python generator\n for ex in ds_numpy:\n # `{'image': np.array(shape=(28, 28, 1)), 'labels': np.array(shape=())}`\n print(ex)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|-----------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `dataset` | a possibly nested structure of [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset)s and/or [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor)s. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| A structure matching `dataset` where [`tf.data.Dataset`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset)s are converted to generators of NumPy arrays and [`tf.Tensor`](https://www.tensorflow.org/api_docs/python/tf/Tensor)s are converted to NumPy arrays. ||\n\n\u003cbr /\u003e"]]