Skip to content

Instantly share code, notes, and snippets.

@haoliplus
Last active July 29, 2017 09:15
Show Gist options
  • Save haoliplus/dac634c50811a7437448aec2290c526b to your computer and use it in GitHub Desktop.
Save haoliplus/dac634c50811a7437448aec2290c526b to your computer and use it in GitHub Desktop.
# -*- coding:utf-8 -*-
#! /usr/bin/env python
#################################################################################
# File Name : csv_reader.py
# Created By : Hao Li
# Creation Date : [2017-02-24 20:41]
# Last Modified : [2017-02-25 10:09]
# Description :
#################################################################################
import tensorflow as tf
# filename_queue = tf.train.string_input_producer(["data1.csv", "data2.csv"], shuffle=True)
filename_queue = tf.train.string_input_producer(["data0.csv"], shuffle=True, num_epochs=1)
reader = tf.TextLineReader()
key, value = reader.read(filename_queue)
# Default values, in case of empty columns. Also specifies the type of the
# decoded result.
record_defaults = [[1], [1], [1], [1], [1]]
col1, col2, col3, col4, col5 = tf.decode_csv(
value, record_defaults=record_defaults
)
features = tf.stack([col1, col2, col3, col4])
batch_size = 8
min_after_dequeue = 10
capacity = min_after_dequeue + 2 * batch_size
feature_batch, label_batch = tf.train.shuffle_batch(
[features, col5], batch_size=batch_size, capacity=capacity,
min_after_dequeue=min_after_dequeue, allow_smaller_final_batch=True
)
# tf.initialize_all_variables()
with tf.Session() as sess:
# Start populating the filename queue.
# sess.run(tf.initialize_all_variables())
sess.run(tf.local_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
try:
while not coord.should_stop():
# for _ in range(60):
# Retrieve a single instance:
# example, label = sess.run([features, col5])
examples, labels = sess.run([feature_batch, label_batch])
# print(example, label)
print(examples, labels)
except Exception as e:
coord.request_stop(e)
print("Exception ")
finally:
coord.request_stop()
coord.join(threads)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment