Tas*_*lrs 3 python tensorflow tflearn
我安装了最新的TensorFlow(v1.1.0),并尝试运行tf.contrib.learn快速入门教程,您可以在其中为IRIS数据集构建分类器.但是,当我尝试时:
training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
filename=IRIS_TRAINING,
target_dtype=np.int,
features_dtype=np.float32)
Run Code Online (Sandbox Code Playgroud)
我收到了一个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)
Run Code Online (Sandbox Code Playgroud)
可能发生的是你的代码之后创建了一个文件名IRIS_TRAINING.但文件是空的.因此StopIteration is raised.如果你看看实现load_csv_with_header:
with gfile.Open(filename) as csv_file:
data_file = csv.reader(csv_file)
header = next(data_file)
Run Code Online (Sandbox Code Playgroud)
StopIteration如果next没有检测到任何其他要读取的项目,则会引发https://docs.python.org/3.5/library/exceptions.html#StopIteration
请注意我的代码与Tensorflow教程中显示的Python 2版本相比的变化:
urllib.request.urlopen 代替 urllib.urlopendecode() 之后进行 read()