讀取數據(Reading data)
TensorFlow輸入數據的方式有四種:
1. tf.data API
關于tf.data.Dataset的更詳盡解釋請看《programmer's guide》。tf.data API能夠從不同的輸入或文件格式中讀取、預處理數據,并且對數據應用一些變換(例如,batching、shuffling、mapping function over the dataset),tf.data API 是舊的 feeding、QueueRunner的升級。
2. Feeding
注意:Feeding是數據輸入效率最低的方式,應該只用于小數據集和調試(debugging)
TensorFlow的Feeding機制允許我們將數據輸入計算圖中的任何一個Tensor。因此可以用Python來處理數據,然后直接將處理好的數據feed到計算圖中 。
在run()
或eval()
中用feed_dict
來將數據輸入計算圖:
with tf.Session(): input = tf.placeholder(tf.float32) classifier = ... print(classifier.eval(feed_dict={input: my_python_preprocessing_fn()}))
雖然你可以用feed data替換任何Tensor的值(包括variables和constants),但最好的使用方法是使用一個tf.placeholder
節點(專門用于feed數據)。它不用初始化,也不包含數據。一個placeholder沒有被feed數據,則會報錯。
使用placeholder和feed_dict的一個實例(數據集使用的是MNIST)見tensorflow/examples/tutorials/mnist/fully_connected_feed.py
3. QueueRunner
注意:這一部分介紹了基于隊列(Queue)API構建輸入通道(pipelines),這一方法完全可以使用 tf.data API來替代。
一個基于queue的從文件中讀取records的通道(pipline)一般有以下幾個步驟:
3.1 Filenames, shuffling, and epoch limits
對于文件名列表,有很多方法:1. 使用一個constant string Tensor(比如:["file0", "file1"]
)或者 [("file%d" %i) for i in range(2)]
;2. 使用 tf.train.match_filenames_once
函數;3. 使用 tf.gfile.Glob(path_pattern)
新聞熱點
疑難解答