第四课 Tensorflow并发读取数据

来源:互联网 发布:龙虎榜数据真实吗 编辑:程序博客网 时间:2024/06/06 01:35

分享朋友的机器学习应用案例:使用机器学习实现财富自由www.abuquant.com

并发读取数据能够大大加速数据的读取速度。

import tensorflow as tfimport numpy as npfrom IPython.display import display, HTMLimport matplotlib.pyplot as pltplt.rcParams["figure.figsize"] = (20,10)
filenames = tf.train.match_filenames_once('data/*.csv')print filenameskeep_prob = tf.Variable(1.0 , name='keep_prob')# 产生文件名字的队列 epchs 是循环三轮,当前/data 下面有3个文件,那么总共就会有9个文件。# 这个num_epochs可以和训练时候的epoch数量一致filename_queue = tf.train.string_input_producer(filenames, shuffle=False, num_epochs=3)
<tf.Variable 'matching_filenames:0' shape=<unknown> dtype=string_ref>
# 定义readerreader = tf.TextLineReader()# 从前面的queue中读取, key就是文件名,value就是读取的数据key, value = reader.read(filename_queue)# 将读取的csv内容进行解码example, label = tf.decode_csv(value, record_defaults=[['null'], ['null']])
# 运行上面构建的图with tf.Session() as session:    session.run(tf.local_variables_initializer()) # 注意这里是local initliazer.    session.run(tf.global_variables_initializer())    #print session.run([filenames])    print(session.run([keep_prob]))    print(session.run([filenames]))    coord = tf.train.Coordinator() # 创建线程管理器 类似线程池    threads = tf.train.start_queue_runners(coord=coord) # 队列运行的线程池    for i in xrange(20):        e, l = session.run([example, label])        print e, l    coord.request_stop()    coord.join(threads)
[1.0][array(['data/A.csv', 'data/B.csv', 'data/C.csv'], dtype=object)]A1 a1A2 a2A3 a3B1 b1B2 b2B3 b3C1 c1C2 c2c3 c3A1 a1A2 a2A3 a3B1 b1B2 b2B3 b3C1 c1C2 c2c3 c3A1 a1A2 a2