Is there any way to select features or labels from a tensorflow Dataset without using numpy conversion methods or iterate through?

The simplest example I found is:

import tensorflow_datasets as tfds

ds = tfds.load('mnist', split='train', shuffle_files=True)
>> <_OptionsDataset shapes: {image: (28, 28, 1), label: ()}, types: {image: tf.uint8, label: tf.int64}>

In this case, _OptionsDataset holds data in a dict, but the collection can be a tuple, list or even a np.ndarray.

If there is not a way to select data directly from a tensorflow Dataset, what's the most commonly used alternative?


1 Answer 1


You can use the ds.map() function to create dataset conatains only images or labels:

ds_images = ds.map(lambda d:d['images')

the original purpose of the map function is manipulating the data without converting to numpy, for example:

ds_images = ds.map(lambda d:d['images']/255)

hope I helped you.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.