Glrs
Glrs

Reputation: 1070

tf.contrib.learn load_csv_with_header not working in TensorFlow 1.1

I installed the latest TensorFlow (v1.1.0) and I tried to run the tf.contrib.learn Quickstart tutorial, where you suppose to build a classifier for the IRIS data set. However, when I tried:

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

I got a StopIteration error.

When I checked the API, I didn't find anything about the load_csv_with_header(). Have they changed it in the latest version without updating the tutorial? How can I fix this?

EDIT: I use Python3.6 if this makes any difference.

Upvotes: 3

Views: 3236

Answers (4)

Junhang Chen
Junhang Chen

Reputation: 1

if the answer above doesn't work, you may specify your path of the iris_training.csv and iris_test.csv file in the urlopen() method.

Upvotes: -1

cheng yang
cheng yang

Reputation: 1742

or you can write the csv file as binary instead of adding 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)

Upvotes: 0

Quan Vuong
Quan Vuong

Reputation: 1999

This is because of the difference between Python 2 and Python 3. Here's my code below that works for 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)

What probably happened is that your code created a file name after IRIS_TRAINING. But the file is empty. Thus StopIteration is raised. If you look into the implementation of load_csv_with_header:

with gfile.Open(filename) as csv_file:
    data_file = csv.reader(csv_file)
    header = next(data_file)

StopIteration is raised when next does not detect any additional items to read as documented https://docs.python.org/3.5/library/exceptions.html#StopIteration

Note the change in my code compared to the Python 2 version as shown in Tensorflow tutorial:

  1. urllib.request.urlopen instead of urllib.urlopen
  2. decode() is performed after read()

Upvotes: 6

Alexandre Passos
Alexandre Passos

Reputation: 5206

StopIteration should only happen there if the csv file is empty. Did you check that that path (IRIS_TRAINING) resolves to something you have permission to open?

Upvotes: 2

Related Questions