1. 程式人生 > >tensorflow-讀寫資料最佳程式碼組合方式

tensorflow-讀寫資料最佳程式碼組合方式

最佳組合程式碼模式為:

# Create the graph, etc.
init_op = tf.global_variables_initializer()

# Create a session for running operations in the Graph.
sess = tf.Session()

# Initialize the variables (like the epoch counter).
sess.run(init_op)

# Start input enqueue threads.
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess=sess, coord=coord)

try:
    while not coord.should_stop():
        # Run training steps or whatever
        sess.run(train_op)

except tf.errors.OutOfRangeError:
    print('Done training -- epoch limit reached')
finally:
    # When done, ask the threads to stop.
    coord.request_stop()

# Wait for threads to finish.
coord.join(threads)
sess.close()
#!/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

g=tf.Graph()
with g.as_default():
    #生成檔名佇列
    fileName=os.getcwd()+"/1.csv"
    print fileName
    fileNameQueue=tf.train.string_input_producer([fileName])
    #生成記錄鍵值對
    reader=tf.TextLineReader(skip_header_lines=1)
    key,value=reader.read(fileNameQueue)
    recordDefaults=[[""],[1],[1]]
    decoded=tf.decode_csv(value,record_defaults=recordDefaults)
    name,age,source=tf.train.shuffle_batch(decoded,batch_size=2,capacity=2,min_after_dequeue=1)    
    features=tf.transpose(tf.stack([age,source]))

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

    coord.request_stop()
    coord.join(threads)

[[32 99]
[36 75]]

#!/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

g=tf.Graph()
with g.as_default():
    #生成檔名佇列
    fileName=os.getcwd()+"/1.csv"
    fileNameQueue=tf.train.string_input_producer([fileName])
    #生成記錄鍵值對
    reader=tf.TextLineReader(skip_header_lines=1)
    key,value=reader.read(fileNameQueue)
    recordDefaults=[[""],[1],[1]]
    decoded=tf.decode_csv(value,record_defaults=recordDefaults)
    name,age,source=tf.train.shuffle_batch(decoded,batch_size=2,capacity=2,min_after_dequeue=1)    
    features=tf.stack([age,source])#此處不轉置

with tf.Session(graph=g) as sess:
    # 開始產生檔名佇列
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    print sess.run(features)
    print sess.run(key)#檔名
    print sess.run(value)#讀取一行的內容

    coord.request_stop()
    coord.join(threads)

[[32 36]
[99 75]]
/Users/xxxxx/Documents/AIstudy/tf/1.csv:3
lisi,36,75
$ cat 1.csv

name,age,source

zhanghua,32,99

liuzhi,29,69

lisi,36,75