Tf.contrib.learn load_csv_with_header not working in TensorFlow 1.1
I installed the latest version of TensorFlow (v1.1.0) and I tried to run the tf.contrib.learn Quickstart where you intend to generate a classifier for the IRIS dataset. 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 an error StopIteration
.
When I checked the API, I didn't find anything about load_csv_with_header()
. Did they change it in the latest version without updating the tutorial? How can I fix this?
EDIT : I am using Python3.6 if it matters.
source to share
This is due to the difference between Python 2 and Python 3. Here's my code below 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 the filename after IRIS_TRAINING
. But the file is empty. Thus StopIteration is raised
. If you look at the implementation load_csv_with_header
:
with gfile.Open(filename) as csv_file:
data_file = csv.reader(csv_file)
header = next(data_file)
StopIteration
thrown when it next
doesn't find any additional items to read as documented https://docs.python.org/3.5/library/exceptions.html#StopIteration
Please note the change in my code from Python 2 version as shown in the Tensorflow tutorial:
-
urllib.request.urlopen
insteadurllib.urlopen
-
decode()
performed afterread()
source to share