1. 程式人生 > >tensorflow-讀取多個檔案

tensorflow-讀取多個檔案

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 15 10:54:53 2018

@author: myhaspl
@email:[email protected]
讀取多個檔案

"""
import tensorflow as tf
import os

validateCount=10
sampleCount=10
testCount=10

g=tf.Graph()

with g.as_default():

    def inputFromFile(fileName,skipLines=1):
        #生成檔名佇列
        fileNameQueue=tf.train.string_input_producer(fileName)
        #生成記錄鍵值對
        reader=tf.TextLineReader(skip_header_lines=skipLines)
        key,value=reader.read(fileNameQueue)
        return key,value

    with tf.name_scope("inputSample"): 
        mykey,mysamples=inputFromFile([os.getcwd()+"/1-1.csv",os.getcwd()+"/1-2.csv"],1)

with tf.Session(graph=g) as sess:
    # 開始產生檔名佇列
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)

    num_examples = 0
    try:
        while True:
            key,value = sess.run([mykey, mysamples])
            print(key,value)
        num_examples += 1
    except tf.errors.OutOfRangeError:
        print ("There are", num_examples, "examples")

    coord.request_stop()
    coord.join(threads)

1-1.csv
x1,x2,y
6.148,72,35.88
0,33.6,0.627
11,50,1
1.89,66,23
94,28.1,0.167
22,21,0.9

1-2.csv
x1,x2,y
11,21,10
1,41,39
10,32,4
2.14,91.2,0.92
109.3,19.03,81.02
...
('/Volumes/MYHASPL/AI_BOOK/src/1-1.csv:4', '11,50,1')
('/Volumes/MYHASPL/AI_BOOK/src/1-1.csv:5', '1.89,66,23')
('/Volumes/MYHASPL/AI_BOOK/src/1-1.csv:6', '94,28.1,0.167')
('/Volumes/MYHASPL/AI_BOOK/src/1-1.csv:7', '22,21,0.9')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:2', '11,21,10')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:3', '1,41,39')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:4', '10,32,4')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:5', '2.14,91.2,0.92')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:6', '109.3,19.03,81.02')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:2', '11,21,10')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:3', '1,41,39')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:4', '10,32,4')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:5', '2.14,91.2,0.92')
('/Volumes/MYHASPL/AI_BOOK/src/1-2.csv:6', '109.3,19.03,81.02')