CSV 文件
从CSV文件中读取数据, 需要使用TextLineReader
和decode_csv
操作, 如下面的例子所示:
艾伯特(http://www.aibbt.com/)国内第一家人工智能门户
filename_queue = tf.train.string_input_producer(["file0.csv", "file1.csv"])
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.concat(0, [col1, col2, col3, col4])
with tf.Session() as sess:
# Start populating the filename queue.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord=coord)
for i in range(1200):
# Retrieve a single instance:
example, label = sess.run([features, col5])
coord.request_stop()
coord.join(threads)
每次read
的执行都会从文件中读取一行内容, decode_csv
操作会解析这一行内容并将其转为张量列表。如果输入的参数有缺失,record_default
参数可以根据张量的类型来设置默认值。
在调用run
或者eval
去执行read
之前, 你必须调用tf.train.start_queue_runners
来将文件名填充到队列。否则read
操作会被阻塞到文件名队列中有值为止。http://www.aibbt.com/a/16370.html