1.创建文件列表path='./data/csvdata/'file_names=os.listdir(path)file_list=[os.path.join(path,file_name)forfile_nameinfile_names]2.读取文件列表到文件队列file_queue=tf.train.string_input_producer(file_list)3.构建文件读取器读取队列文件reader=tf.TextLineReader()key,value=reader.read(file_queue)4.解码recodes=[['None'],['None']]example,label=tf.decoder_csv(value,record_defaults=records)5.批处理tf.train.batch([example,label],batch_size=9,num_threads=2,capacity=100)6.打开tfsessionmultithreadingforprocessingcoord=tf.train.Coordinator()threads=tf.start_queue_runners(sess,coord=coord)coord.request_stop()coord.join(threads)完整代码importtensorflowastfimportos'''1.构建一个文件队列2.读取队列内容,默认读取一个sample1.csv文件,读取一行2.二进制文件,指定读取一个sample的字节数3.图片文件,默认一个一个读取3.解码4.批量读取文件5.主线程takessampledatafortraining'''defcsv_read(file_list):#1.构造文件队列file_queue=tf.train.string_input_producer(file_list)#2.构造reader,读取文件reader=tf.TextLineReader()key,value=reader.read(file_queue)#3.执行文件解码record=[["None"],["None"]]example,label=tf.decode_csv(value,record_defaults=record)#4.批处理batch_example,batch_label=tf.train.batch([example,label],batch_size=9,num_threads=1,capacity=9)returnbatch_example,batch_labelif__name__=='__main__':file_names=os.listdir("./data/csvdata")file_list=[os.path.join('./data/csvdata',file)forfileinfile_names]#print(file_list)示例,label=csv_read(file_list)withtf.Session()assess:coord=tf.train.Coordinator()threads=tf.train.start_queue_runners(sess,coord=coord)print(sess.run([example,标签]))coord.request_stop()coord.join(threads)
