Search code examples
pythontensorflowtensorflow-datasets

Can't convert a tf.data.Dataset object to a numpy iterator


I am using Tensorflow 1.14.0 and tensorflow_datasets 1.2.0

When trying to run the following code

import tensorflow as tf
import tensorflow_datasets as tfds

smallnorb = tfds.load("smallnorb")
smallnorb_train, smallnorb_test = smallnorb["train"], smallnorb["test"]
assert isinstance(smallnorb_train, tf.data.Dataset)
smallnorb_train = smallnorb_train.as_numpy_iterator()

I get the following error

AttributeError: 'DatasetV1Adapter' object has no attribute 'as_numpy_iterator'

According to the tensorflow_datasets docs this should work.

Why won't it? And why am I getting a DatasetV1Adapter object in the first place?


Solution

  • You are using wrong tensorflow and tensorflow_datasets versions.

    Please use 2.x unless you need 1.x for some very specific reasons.

    This code works if you use tensorflow 2.1.0 and tensorflow_datasets 2.0.0. Proper documentation for 1.x of tf.data.Dataset can be found here and it has no such method indeed.