tf.contrib.学习加载不在TensorFlow 1.1中工作的_csv_

2024-05-23 16:58:17 发布

您现在位置: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,
    features_dtype=np.float32)

我得到了一个StopIteration错误。在

当我检查API时,我没有找到关于load_csv_with_header()的任何信息。他们是否在没有更新教程的情况下更改了最新版本?我该怎么解决这个问题?在

编辑: 如果这有什么不同的话,我就用Python3.6。在


Tags: csv数据iris分类器tftensorflowwithnp
2条回答

或者您可以将csv文件写为二进制文件,而不是添加decode()

if not os.path.exists(IRIS_TRAINING):
    raw = urllib.request.urlopen(IRIS_TRAINING_URL).read()
    with open(IRIS_TRAINING, 'wb') as f:
        f.write(raw)

这是因为Python2和Python3之间的区别。下面是我在Python3.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 is raised。如果您查看load_csv_with_header的实现:

^{2}$

next没有检测到任何要读取的附加项时,StopIteration被引发https://docs.python.org/3.5/library/exceptions.html#StopIteration

请注意我的代码与Python 2版本相比的变化,如Tensorflow教程所示:

  1. urllib.request.urlopen而不是{}
  2. decode()read()之后执行

相关问题 更多 >