python -tf.contrib.learn load_csv_with_header在TensorFlow 1.
发布时间:2020-12-20 12:00:02 所属栏目:Python 来源:网络整理
导读:我安装了最新的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,
我安装了最新的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()的信息.他们是否在最新版本中更改了它而没有更新教程?我怎样才能解决这个问题? 编辑: 解决方法
这是因为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()之后执行 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |