gpt4 book ai didi

python - TensorFlow 类型错误 : 'BatchDataset' object is not iterable/TypeError: 'CacheDataset' object is not subscriptable

转载 作者:太空宇宙 更新时间:2023-11-04 00:17:36 25 4
gpt4 key购买 nike

我正在关注 TensorFlow starter guide .它特别指出要在鸢尾花(花)分类的示例项目上启用即时执行。

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.

所以我按照说明启用eager execution,然后继续说明。然而,当我到达讨论如何将数据集准备成 tensorflow 数据集的部分时,我遇到了一个错误。

单元格代码

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

错误

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

我只想继续学习这些例子。我该怎么做才能将 BatchDataset 对象转换为可迭代对象?

最佳答案

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

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

应该返回以下输出:

TensorFlow version: 1.8.0
Eager execution: True

关于python - TensorFlow 类型错误 : 'BatchDataset' object is not iterable/TypeError: 'CacheDataset' object is not subscriptable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50354158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com