Skip to content

Commit 6d44da4

Browse files
authored
Extended description of tfds.as_numpy
- Added simple code example for tfds.as_numpy - Extended description of how it works
1 parent ef0966d commit 6d44da4

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

tensorflow_datasets/core/dataset_utils.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,23 @@ def as_numpy(dataset, graph=None):
190190
are left as-is for the user to deal with them (e.g. using `to_list()`).
191191
In TF 1 (i.e. graph mode), `tf.RaggedTensor`s are returned as
192192
`tf.ragged.RaggedTensorValue`s.
193-
193+
194+
Example:
195+
A numpy generator produced from a loaded dataset with `as_supervised=False`
196+
will yield dictionaries with the keys and the features as listed in the dataset.
197+
```
198+
mnist = tfds.load(name="mnist",split="train")
199+
numpy_gen = tfds.as_numpy(mnist)
200+
```
201+
`numpy_gen` will yield samples in the format:
202+
`{"image":...,"labels":...}`
203+
The shape of the numpy arrays depend on the `batch_size` argument of `tfds.load`.
204+
If `batch_size` is set to any number, the shape of the arrays will include the batch
205+
index(shape: (1,28,28,1) instead of (28,28,1)).
206+
207+
If `as_supervised=True` the generator will yield tuples in the same order as the
208+
normal dataset would. The shape convention described above still applies.
209+
194210
Args:
195211
dataset: a possibly nested structure of `tf.data.Dataset`s and/or
196212
`tf.Tensor`s.

0 commit comments

Comments
 (0)