關(guān)于Tensorflow中的tf.train.batch函數(shù)的使用
這兩天一直在看tensorflow中的讀取數(shù)據(jù)的隊(duì)列,說實(shí)話,真的是很難懂。也可能我之前沒這方面的經(jīng)驗(yàn)吧,最早我都使用的theano,什么都是自己寫。經(jīng)過這兩天的文檔以及相關(guān)資料,并且請教了國內(nèi)的師弟。今天算是有點(diǎn)小感受了。簡單的說,就是計(jì)算圖是從一個管道中讀取數(shù)據(jù)的,錄入管道是用的現(xiàn)成的方法,讀取也是。為了保證多線程的時(shí)候從一個管道讀取數(shù)據(jù)不會亂吧,所以這種時(shí)候 讀取的時(shí)候需要線程管理的相關(guān)操作。今天我實(shí)驗(yàn)室了一個簡單的操作,就是給一個有序的數(shù)據(jù),看看讀出來是不是有序的,結(jié)果發(fā)現(xiàn)是有序的,所以直接給代碼:
import tensorflow as tf import numpy as np def generate_data(): num = 25 label = np.asarray(range(0, num)) images = np.random.random([num, 5, 5, 3]) print('label size :{}, image size {}'.format(label.shape, images.shape)) return label, images def get_batch_data(): label, images = generate_data() images = tf.cast(images, tf.float32) label = tf.cast(label, tf.int32) input_queue = tf.train.slice_input_producer([images, label], shuffle=False) image_batch, label_batch = tf.train.batch(input_queue, batch_size=10, num_threads=1, capacity=64) return image_batch, label_batch image_batch, label_batch = get_batch_data() with tf.Session() as sess: coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess, coord) i = 0 try: while not coord.should_stop(): image_batch_v, label_batch_v = sess.run([image_batch, label_batch]) i += 1 for j in range(10): print(image_batch_v.shape, label_batch_v[j]) except tf.errors.OutOfRangeError: print("done") finally: coord.request_stop() coord.join(threads)
記得那個slice_input_producer方法,默認(rèn)是要shuffle的哈。
Besides, I would like to comment this code.
1: there is a parameter ‘num_epochs' in slice_input_producer, which controls how many epochs the slice_input_producer method would work. when this method runs the specified epochs, it would report the OutOfRangeRrror. I think it would be useful for our control the training epochs.
2: the output of this method is one single image, we could operate this single image with tensorflow API, such as normalization, crops, and so on, then this single image is feed to batch method, a batch of images for training or testing wouldbe received.
tf.train.batch和tf.train.shuffle_batch的區(qū)別用法
tf.train.batch([example, label], batch_size=batch_size, capacity=capacity):[example, label]表示樣本和樣本標(biāo)簽,這個可以是一個樣本和一個樣本標(biāo)簽,batch_size是返回的一個batch樣本集的樣本個數(shù)。capacity是隊(duì)列中的容量。這主要是按順序組合成一個batch
tf.train.shuffle_batch([example, label], batch_size=batch_size, capacity=capacity, min_after_dequeue)。這里面的參數(shù)和上面的一樣的意思。不一樣的是這個參數(shù)min_after_dequeue,一定要保證這參數(shù)小于capacity參數(shù)的值,否則會出錯。這個代表隊(duì)列中的元素大于它的時(shí)候就輸出亂的順序的batch。也就是說這個函數(shù)的輸出結(jié)果是一個亂序的樣本排列的batch,不是按照順序排列的。
上面的函數(shù)返回值都是一個batch的樣本和樣本標(biāo)簽,只是一個是按照順序,另外一個是隨機(jī)的
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python輕量級ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法詳解
這篇文章主要介紹了Python輕量級ORM框架Peewee訪問sqlite數(shù)據(jù)庫的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了ORM框架的概念、功能及peewee的安裝、使用及操作sqlite數(shù)據(jù)庫的方法,需要的朋友可以參考下2017-07-07Python pandas 計(jì)算每行的增長率與累計(jì)增長率
這篇文章主要介紹了Python pandas 計(jì)算每行的增長率與累計(jì)增長率,文章舉例詳細(xì)說明。需要的小伙伴可以參考一下2022-03-03深入學(xué)習(xí)Python可變與不可變對象操作實(shí)例
Python中的數(shù)據(jù)類型可以分為可變對象和不可變對象,了解它們之間的區(qū)別對于編寫高效的Python代碼至關(guān)重要,本文將詳細(xì)介紹可變對象和不可變對象的概念,以及如何正確地使用它們來提高代碼的性能和可讀性2023-12-12親手教你用Python打造一款摸魚倒計(jì)時(shí)界面
前段時(shí)間在微博看到一段摸魚人的倒計(jì)時(shí)模板,感覺很有意思,于是我用了一個小時(shí)的時(shí)間寫一個頁面出來,下面小編把實(shí)現(xiàn)過程分享給大家,對Python摸魚倒計(jì)時(shí)界面感興趣的朋友一起看看吧2021-12-12Python使用當(dāng)前時(shí)間、隨機(jī)數(shù)產(chǎn)生一個唯一數(shù)字的方法
這篇文章主要介紹了Python使用當(dāng)前時(shí)間、隨機(jī)數(shù)產(chǎn)生一個唯一數(shù)字的方法,涉及Python時(shí)間與隨機(jī)數(shù)相關(guān)操作技巧,需要的朋友可以參考下2017-09-09解決python中的冪函數(shù)、指數(shù)函數(shù)問題
今天小編就為大家分享一篇解決python中的冪函數(shù)、指數(shù)函數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Pycharm如何設(shè)置默認(rèn)請求頭和切換python環(huán)境
這篇文章主要介紹了Pycharm如何設(shè)置默認(rèn)請求頭和切換python環(huán)境問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-06-06Python雙端隊(duì)列實(shí)現(xiàn)回文檢測
雙端隊(duì)列 Deque 是一種有次序的數(shù)據(jù)集,跟隊(duì)列相似,其兩端可以稱作"首" 和 "尾"端。這篇文章將通過雙端隊(duì)列實(shí)現(xiàn)回文檢測,感興趣的可以學(xué)習(xí)一下2022-01-01