模块'tensorflow_core._api.v2.data'没有属性'Iterator'

时间:2020-03-13 06:19:51

标签: tensorflow2.0 tensorflow-datasets

无法弄清楚使用什么代替Iterator

我改用了tf.compat.v1.data.Iterator,但遇到了另一个错误-AttributeError: 'PrefetchDataset' object has no attribute 'output_types'

代码:

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'*

我的TF版本2.2.0-dev20200212

谢谢!

2 个答案:

答案 0 :(得分:2)

我能够重现您的错误。您可以通过以下方法在Tensorflow Version 2.x中进行修复。

您需要如下定义iter-

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))

下面是一个示例-

代码-

%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])

输出-

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])>]

希望这能回答您的问题。学习愉快。

答案 1 :(得分:1)

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)

使用它可以解决问题

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