tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實例
先放關(guān)鍵代碼:
i = tf.train.range_input_producer(NUM_EXPOCHES, num_epochs=1, shuffle=False).dequeue() inputs = tf.slice(array, [i * BATCH_SIZE], [BATCH_SIZE])
原理解析:
第一行會產(chǎn)生一個隊列,隊列包含0到NUM_EXPOCHES-1的元素,如果num_epochs有指定,則每個元素只產(chǎn)生num_epochs次,否則循環(huán)產(chǎn)生。shuffle指定是否打亂順序,這里shuffle=False表示隊列的元素是按0到NUM_EXPOCHES-1的順序存儲。在Graph運行的時候,每個線程從隊列取出元素,假設(shè)值為i,然后按照第二行代碼切出array的一小段數(shù)據(jù)作為一個batch。例如NUM_EXPOCHES=3,如果num_epochs=2,則隊列的內(nèi)容是這樣子;
0,1,2,0,1,2
隊列只有6個元素,這樣在訓(xùn)練的時候只能產(chǎn)生6個batch,迭代6次以后訓(xùn)練就結(jié)束。
如果num_epochs不指定,則隊列內(nèi)容是這樣子:
0,1,2,0,1,2,0,1,2,0,1,2...
隊列可以一直生成元素,訓(xùn)練的時候可以產(chǎn)生無限的batch,需要自己控制什么時候停止訓(xùn)練。
下面是完整的演示代碼。
數(shù)據(jù)文件test.txt內(nèi)容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
main.py內(nèi)容:
import tensorflow as tf import codecs BATCH_SIZE = 6 NUM_EXPOCHES = 5 def input_producer(): array = codecs.open("test.txt").readlines() array = map(lambda line: line.strip(), array) i = tf.train.range_input_producer(NUM_EXPOCHES, num_epochs=1, shuffle=False).dequeue() inputs = tf.slice(array, [i * BATCH_SIZE], [BATCH_SIZE]) return inputs class Inputs(object): def __init__(self): self.inputs = input_producer() def main(*args, **kwargs): inputs = Inputs() init = tf.group(tf.initialize_all_variables(), tf.initialize_local_variables()) sess = tf.Session() coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess=sess, coord=coord) sess.run(init) try: index = 0 while not coord.should_stop() and index<10: datalines = sess.run(inputs.inputs) index += 1 print("step: %d, batch data: %s" % (index, str(datalines))) except tf.errors.OutOfRangeError: print("Done traing:-------Epoch limit reached") except KeyboardInterrupt: print("keyboard interrput detected, stop training") finally: coord.request_stop() coord.join(threads) sess.close() del sess if __name__ == "__main__": main()
輸出:
step: 1, batch data: ['1' '2' '3' '4' '5' '6'] step: 2, batch data: ['7' '8' '9' '10' '11' '12'] step: 3, batch data: ['13' '14' '15' '16' '17' '18'] step: 4, batch data: ['19' '20' '21' '22' '23' '24'] step: 5, batch data: ['25' '26' '27' '28' '29' '30'] Done traing:-------Epoch limit reached
如果range_input_producer去掉參數(shù)num_epochs=1,則輸出:
step: 1, batch data: ['1' '2' '3' '4' '5' '6'] step: 2, batch data: ['7' '8' '9' '10' '11' '12'] step: 3, batch data: ['13' '14' '15' '16' '17' '18'] step: 4, batch data: ['19' '20' '21' '22' '23' '24'] step: 5, batch data: ['25' '26' '27' '28' '29' '30'] step: 6, batch data: ['1' '2' '3' '4' '5' '6'] step: 7, batch data: ['7' '8' '9' '10' '11' '12'] step: 8, batch data: ['13' '14' '15' '16' '17' '18'] step: 9, batch data: ['19' '20' '21' '22' '23' '24'] step: 10, batch data: ['25' '26' '27' '28' '29' '30']
有一點需要注意,文件總共有35條數(shù)據(jù),BATCH_SIZE = 6表示每個batch包含6條數(shù)據(jù),NUM_EXPOCHES = 5表示產(chǎn)生5個batch,如果NUM_EXPOCHES =6,則總共需要36條數(shù)據(jù),就會報如下錯誤:
InvalidArgumentError (see above for traceback): Expected size[0] in [0, 5], but got 6 [[Node: Slice = Slice[Index=DT_INT32, T=DT_STRING, _device="/job:localhost/replica:0/task:0/cpu:0"](Slice/input, Slice/begin/_5, Slice/size)]]
錯誤信息的意思是35/BATCH_SIZE=5,即NUM_EXPOCHES 的取值能只能在0到5之間。
以上這篇tensorflow使用range_input_producer多線程讀取數(shù)據(jù)實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- TensorFlow實現(xiàn)從txt文件讀取數(shù)據(jù)
- TensorFlow 讀取CSV數(shù)據(jù)的實例
- 利用Tensorflow的隊列多線程讀取數(shù)據(jù)方式
- tensorflow入門:TFRecordDataset變長數(shù)據(jù)的batch讀取詳解
- tensorflow tf.train.batch之?dāng)?shù)據(jù)批量讀取方式
- Tensorflow 實現(xiàn)分批量讀取數(shù)據(jù)
- Tensorflow中使用tfrecord方式讀取數(shù)據(jù)的方法
- 用十張圖詳解TensorFlow數(shù)據(jù)讀取機(jī)制(附代碼)
- TensorFlow高效讀取數(shù)據(jù)的方法示例
- 詳解Tensorflow數(shù)據(jù)讀取有三種方式(next_batch)
- Tensorflow分批量讀取數(shù)據(jù)教程
相關(guān)文章
基于Tensorflow讀取MNIST數(shù)據(jù)集時網(wǎng)絡(luò)超時的解決方式
這篇文章主要介紹了基于Tensorflow讀取MNIST數(shù)據(jù)集時網(wǎng)絡(luò)超時的解決方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06Python中執(zhí)行JavaScript實現(xiàn)數(shù)據(jù)抓取的多種方法
JavaScript是一門強大的腳本語言,廣泛應(yīng)用于網(wǎng)頁前端開發(fā)、構(gòu)建交互式用戶界面以及處理各種客戶端端任務(wù),有時可能需要在Python環(huán)境中執(zhí)行JavaScript代碼,本文將介紹多種方法,幫助你在Python中執(zhí)行 JavaScript代碼,并提供詳盡的示例代碼,使你能夠輕松掌握這一技能2023-11-11詳解Python中open()函數(shù)指定文件打開方式的用法
well,我們這里所指的文件打開方式并不是指調(diào)用什么應(yīng)用程序去打開某個文件,而是只讀只寫或者二進(jìn)制等的打開方式,這里我們就來詳解Python中open()函數(shù)指定文件打開方式的用法2016-06-06