TensorFlow TypeError:“BatchDataset”对象不可迭代/TypeError:“CacheDataset”对象不可下标

iwi*_*not 5 python python-3.x tensorflow google-colaboratory

我正在遵循TensorFlow 入门指南。它特别表示要在鸢尾花(花)分类的示例项目上启用急切执行。

导入所需的 Python 模块(包括 TensorFlow),并启用该程序的即时执行。急切执行使 TensorFlow 立即评估操作,返回具体值,而不是创建稍后执行的计算图。如果您习惯了 REPL 或 python 交互式控制台,您将会有宾至如归的感觉。

因此,我按照说明启用急切执行,并继续执行说明。然而,当我到达讨论如何将数据集准备成张量流数据集的部分时,我遇到了错误。

细胞代码

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])
Run Code Online (Sandbox Code Playgroud)

错误

---------------------------------------------------------------------------
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
Run Code Online (Sandbox Code Playgroud)

我只想继续遵循这些示例。我可以做什么来将BatchDataset对象转换为可迭代的对象?

iwi*_*not 4

事实证明,我实际上没有执行项目中的某些步骤,导致了这个问题。

将 TensorFlow 从 1.7 升级到 1.8:

!pip install --upgrade tensorflow

检查您的 TensorFlow 是否已更新

此代码单元格:

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()))
Run Code Online (Sandbox Code Playgroud)

应返回以下输出:

TensorFlow version: 1.8.0
Eager execution: True
Run Code Online (Sandbox Code Playgroud)