-5

The code is showed below,and the wrong message is also showed below:

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))

This prints the following stack-trace:

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
MBT
  • 21,733
  • 19
  • 84
  • 102
skyfacon
  • 11
  • 5

1 Answers1

0

I would like to know which page you are using as tutorial for this. Because the first page which comes when searching in google is this:

And the difference between this and what you posted is tf.contrib.learn.datasets.base.load_csv_without_header and tf.contrib.learn.datasets.base.load_csv_with_header.

The actual URL or iris data you have specified contains the header. And you are trying to load it as a file without the header. Hence the strings in the header are not able to get converted to float and the error.

Change your code to:

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)
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
  • Here is the stack-trace: Traceback (most recent call last): File"/home/skyfacon/PycharmProjects/LinearFitting/IrisClassification.py", line 31, in features_dtype=np.float32) File "/home/skyfacon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/learn/python/learn/datasets/base.py", line 47, in load_csv_with_header header = next(data_file) StopIteration Process finished with exit code 1 – skyfacon Jun 27 '17 at 09:12
  • @skyfacon Which version of tensorflow are you using? Try upgrading to latest one before using it to see if that works for you. Its working for me fine. – Vivek Kumar Jun 27 '17 at 12:57