'TensorFlow TypeError: 'BatchDataset' object is not iterable / TypeError: 'CacheDataset' object is not subscriptable
I'm following the TensorFlow starter guide. It specifically said to enable eager execution on the sample project for iris (flower) classification.
Import the required Python modules, including TensorFlow, and enable eager execution for this program. Eager execution makes TensorFlow evaluate operations immediately, returning concrete values instead of creating a computational graph that is executed later. If you are used to a REPL or the python interactive console, you'll feel at home.
So I followed the instructions to enable eager execution, and continued the instructions. However, when I reached the section discussing how the dataset will be prepared into a tensor flow dataset, I encountered an error.
Cell code
train_dataset = tf.data.TextLineDataset(train_dataset_fp)
train_dataset = train_dataset.skip(1) # skip the first header row
train_dataset = train_dataset.map(parse_csv) # parse each row
train_dataset = train_dataset.shuffle(buffer_size=1000) # randomize
train_dataset = train_dataset.batch(32)
# View a single example entry from a batch
features, label = iter(train_dataset).next()
print("example features:", features[0])
print("example label:", label[0])
Error
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-5-61bfe99af85b> in <module>()
7
8 # View a single example entry from a batch
----> 9 features, label = iter(train_dataset).next()
10 print("example features:", features[0])
11 print("example label:", label[0])
TypeError: 'BatchDataset' object is not iterable
I just want to continue following the examples. What can I do to transform the BatchDataset
object into something iterable?
Solution 1:[1]
It turns out that I actually failed to do certain steps in the project that caused this problem.
Upgrade TensorFlow from 1.7 to 1.8:
!pip install --upgrade tensorflow
Checking if your TensorFlow is updated
This code cell:
from __future__ import absolute_import, division, print_function
import os
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow.contrib.eager as tfe
tf.enable_eager_execution()
print("TensorFlow version: {}".format(tf.VERSION))
print("Eager execution: {}".format(tf.executing_eagerly()))
Should return the following output:
TensorFlow version: 1.8.0
Eager execution: True
Solution 2:[2]
Refer here for alternate solution. We can also use as_numpy_iterator()
to get values from tensorflow dataset
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | sakeesh |