SushiWaUmai
SushiWaUmai

Reputation: 359

Low-Level Tensorflow, dataset.as_numpy_iterator() returns dicts instead of numpy arrays

When I try to import and batch the dataset using the method with tf.data.Dataset.batch() and use the dataset.as_numpy_iterator(), the iterated objects are dicts, even though I should get multiple numpy arrays.

dataset = tfds.load('mnist', split='train')
dataset.batch(batch_size, drop_remainder=False)
for i in dataset.as_numpy_iterator():
    print(type(i))  # prints <class 'dict'>

Why does it happen?

Upvotes: 0

Views: 7031

Answers (1)

Aniket Bote
Aniket Bote

Reputation: 3574

Use as_supervised = True

import tensorflow_datasets as tfds
dataset = tfds.load('mnist', split='train', as_supervised=True)
dataset.batch(10, drop_remainder=False)
for image, label in tfds.as_numpy(dataset):
    print(type(image), type(label), label)

According to TensorFlow documentation if as_supervised is False you will get dictionary values. Check Docs Here

Upvotes: 3

Related Questions