tensorflow 网站上的 iris 教程不好用

Posted

技术标签:

【中文标题】tensorflow 网站上的 iris 教程不好用【英文标题】:The iris tutorial in tensorflow's website does not work well 【发布时间】:2017-11-29 22:48:53 【问题描述】:

代码如下,错误信息如下:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import urllib.request

import tensorflow as tf
import numpy as np

IRIS_TRAINING = "iris_training.csv"
IRIS_TRAINING_URL = "http://download.tensorflow.org/data/iris_training.csv"

IRIS_TEST = "iris_test.csv"
IRIS_TEST_RRL = "http://download.tensorflow.org/data/iris_test.csv"

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

if not os.path.exists(IRIS_TEST):
    raw = urllib.request.urlopen(IRIS_TEST_RRL).read()
    with open(IRIS_TEST, 'w') as f:
        f.write(raw)

# load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv_without_header(
    filename=IRIS_TRAINING,
    target_dtype=np.int,
    features_dtype=np.float32)
test_set = tf.contrib.learn.datasets.base.load_csv_without_header(
    filename=IRIS_TEST,
    target_dtype=np.int,
    features_dtype=np.float32
)

# Specify that all features have real_valued data
feature_columns = [tf.contrib.layers.real_valued_column("", dimension=4)]

# Build 3 layers DNN with 10, 20, 10 units respectively.
classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                            hidden_units=[10, 20, 30],
                                            n_class=3,
                                            model_dir="/tem/iris_model")

# Define the training imputs
def get_train_inputs():
    x = tf.constant(training_set.data)
    y = tf.constant(training_set.target)

    return x, y

# Fit model
classifier.fit(input_fn=get_train_inputs(), steps=2000)

# Define the test inputs
def get_test_inputs():
    x = tf.constant(test_set.data)
    y = tf.constant(test_set.target)

    return x, y

# Evaluate accuracy
accuracy_score = classifier.evaluate(input_fn=get_test_inputs(), steps=1)["accuracy"]
print("\nTest Accuracy: 0:f\n".format(accuracy_score))

这将打印以下堆栈跟踪:

Traceback (most recent call last):
  File "/home/skyfacon/PycharmProjects/LinearFitting/IrisClassification.py", line 35, in <module>
    features_dtype=np.float32
  File "/home/skyfacon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 69, in load_csv_without_header
    data.append(np.asarray(row, dtype=features_dtype))
  File "/home/skyfacon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/numpy/core/numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 'setosa'

Process finished with exit code 1

【问题讨论】:

我无法添加图片...心烦 然后将错误信息与代码一起复制粘贴。 感谢您的建议 你可以链接你正在关注的教程吗? The link here 【参考方案1】:

我想知道您使用哪个页面作为教程。因为谷歌搜索的第一页是这样的:

https://www.tensorflow.org/get_started/tflearn

这与您发布的内容之间的区别是tf.contrib.learn.datasets.base.load_csv_without_headertf.contrib.learn.datasets.base.load_csv_with_header

您指定的实际 URL 或虹膜数据包含标题。您正在尝试将其作为没有标题的文件加载。因此,标头中的字符串无法转换为浮点数和错误。

将您的代码更改为:

training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
    filename=IRIS_TRAINING,
    target_dtype=np.int,
    features_dtype=np.float32)
test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
    filename=IRIS_TEST,
    target_dtype=np.int,
    features_dtype=np.float32)

【讨论】:

这里是堆栈跟踪: Traceback(最近一次调用最后一次):文件“/home/skyfacon/PycharmProjects/LinearFitting/IrisClassification.py”,第 31 行,在 features_dtype=np。 float32) 文件“/home/skyfacon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py”,第 47 行,在 load_csv_with_header header = next (data_file) StopIteration Process 以退出代码 1 结束 @skyfacon 您使用的是哪个版本的 tensorflow?在使用它之前尝试升级到最新版本,看看它是否适合您。它对我来说很好。

以上是关于tensorflow 网站上的 iris 教程不好用的主要内容,如果未能解决你的问题,请参考以下文章

TensorFlow和深度学习入门教程(TensorFlow and deep learning without a PhD)

tensorflow 线性回归 iris

Tensorflow Keras 修改 Iris 示例时形状不兼容

ValueError: 标签形状必须为 [batch_size, labels_dimension],得到 (128, 2)

TensorFlow和深度学习入门教程(TensorFlow and deep learning without a PhD)

tensorflow实现svm iris二分类——本质上在使用梯度下降法求解线性回归(loss是定制的而已)