Re Dream
Re Dream

Reputation: 91

module 'tensorflow_core._api.v2.data' has no attribute 'Iterator'

Can't figure out what to use instead of Iterator

I tried tf.compat.v1.data.Iterator instead but got another error - AttributeError: 'PrefetchDataset' object has no attribute 'output_types'

code:

train_ds = prepare_for_train(labeled_ds)

val_ds = tf.data.Dataset.from_tensor_slices(test_data)

#create a iterator with shape and type

iter = tf.data.Iterator.from_structure(train_ds.output_types, train_ds.output_shapes)

"""iter= tf.compat.v1.data.Iterator.from_structure(train_ds.output_types, train_ds.output_shapes)"""
print(iter)


*AttributeError: module 'tensorflow_core._api.v2.data' has no attribute 'Iterator'*

My TF version 2.2.0-dev20200212

Thank you!

Upvotes: 3

Views: 7398

Answers (2)

SOURAV DE
SOURAV DE

Reputation: 11

tf.compat.v1.disable_eager_execution()
train_ds = prepare_for_train(labeled_ds)

val_ds = tf.data.Dataset.from_tensor_slices(test_data)

#create a iterator with shape and type

iter = tf.data.Iterator.from_structure(train_ds.output_types, train_ds.output_shapes)

"""iter= tf.compat.v1.data.Iterator.from_structure(train_ds.output_types, train_ds.output_shapes)"""
print(iter)

Using this should solve the issue

*AttributeError: module 'tensorflow_core._api.v2.data' has no attribute 'Iterator'*

Upvotes: 1

user11530462
user11530462

Reputation:

I was able to reproduce your error. Here is how you can fix it in Tensorflow Version 2.x.

You need to define iter as below -

iter = tf.compat.v1.data.Iterator.from_structure(tf.compat.v1.data.get_output_types(train_dataset),
                                           tf.compat.v1.data.get_output_shapes(train_dataset))

Below is an example -

Code -

%tensorflow_version 2.x
import tensorflow as tf
print(tf.__version__)
import numpy as np

# Reinitializable iterator to switch between Datasets
EPOCHS = 10

# making fake data using numpy
train_data = (np.random.sample((100,2)), np.random.sample((100,1)))

# create two datasets, one for training and one for test
train_dataset = tf.data.Dataset.from_tensor_slices(train_data)

# create a iterator of the correct shape and type
iter = tf.compat.v1.data.Iterator.from_structure(tf.compat.v1.data.get_output_types(train_dataset),
                                           tf.compat.v1.data.get_output_shapes(train_dataset))

# create the initialisation operations
train_init_op = iter.make_initializer(train_dataset)

features, labels = iter.get_next()

for _ in range(EPOCHS):
  print([features, labels])

Output -

2.1.0
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/iterator_ops.py:347: Iterator.output_types (from tensorflow.python.data.ops.iterator_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.compat.v1.data.get_output_types(iterator)`.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/iterator_ops.py:348: Iterator.output_shapes (from tensorflow.python.data.ops.iterator_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.compat.v1.data.get_output_shapes(iterator)`.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/data/ops/iterator_ops.py:350: Iterator.output_classes (from tensorflow.python.data.ops.iterator_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use `tf.compat.v1.data.get_output_classes(iterator)`.
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]
[<tf.Tensor: shape=(2,), dtype=float64, numpy=array([0.35431711, 0.07564416])>, <tf.Tensor: shape=(1,), dtype=float64, numpy=array([0.38728039])>]

Hope this answers your question. Happy Learning.

Upvotes: 3

Related Questions