我安装了最新的TensorFlow(v1.1.0)并尝试运行
tf.contrib.learn Quickstart教程,您可以在其中为IRIS数据集构建分类器.但是,当我尝试时:
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING,
target_dtype=np.int,
features_dtype=np.float32)
我遇到了StopIteration错误.
当我检查API时,我没有找到任何关于load_csv_with_header()的信息.他们是否在最新版本中更改了它而没有更新教程?我怎样才能解决这个问题?
编辑:
我使用Python3.6,如果这有任何区别.
最佳答案 这是因为Python 2和Python 3之间存在差异.下面我的代码适用于Python 3.5:
if not os.path.exists(IRIS_TRAINING):
raw = urllib.request.urlopen(IRIS_TRAINING_URL).read().decode()
with open(IRIS_TRAINING, 'w') as f:
f.write(raw)
if not os.path.exists(IRIS_TEST):
raw = urllib.request.urlopen(IRIS_TEST_URL).read().decode()
with open(IRIS_TEST, 'w') as f:
f.write(raw)
可能发生的是您的代码在IRIS_TRAINING之后创建了一个文件名.但文件是空的.因此提出了StopIteration.如果你看一下load_csv_with_header的实现:
with gfile.Open(filename) as csv_file:
data_file = csv.reader(csv_file)
header = next(data_file)
当next没有检测到任何其他要读取的项目时,会引发StopIteration,如文档https://docs.python.org/3.5/library/exceptions.html#StopIteration所示
请注意我的代码与Tensorflow教程中显示的Python 2版本相比的变化:
> urllib.request.urlopen而不是urllib.urlopen
> read()在read()之后执行