關(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ì)算圖是從一個(gè)管道中讀取數(shù)據(jù)的,錄入管道是用的現(xiàn)成的方法,讀取也是。為了保證多線程的時(shí)候從一個(gè)管道讀取數(shù)據(jù)不會亂吧,所以這種時(shí)候 讀取的時(shí)候需要線程管理的相關(guān)操作。今天我實(shí)驗(yàn)室了一個(gè)簡單的操作,就是給一個(gè)有序的數(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)
記得那個(gè)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)簽,這個(gè)可以是一個(gè)樣本和一個(gè)樣本標(biāo)簽,batch_size是返回的一個(gè)batch樣本集的樣本個(gè)數(shù)。capacity是隊(duì)列中的容量。這主要是按順序組合成一個(gè)batch
tf.train.shuffle_batch([example, label], batch_size=batch_size, capacity=capacity, min_after_dequeue)。這里面的參數(shù)和上面的一樣的意思。不一樣的是這個(gè)參數(shù)min_after_dequeue,一定要保證這參數(shù)小于capacity參數(shù)的值,否則會出錯(cuò)。這個(gè)代表隊(duì)列中的元素大于它的時(shí)候就輸出亂的順序的batch。也就是說這個(gè)函數(shù)的輸出結(jié)果是一個(gè)亂序的樣本排列的batch,不是按照順序排列的。
上面的函數(shù)返回值都是一個(gè)batch的樣本和樣本標(biāo)簽,只是一個(gè)是按照順序,另外一個(gè)是隨機(jī)的
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密)
這篇文章主要為大家介紹了python庫pycryptodom加密技術(shù)探索(公鑰加密私鑰加密),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
利用Python爬蟲實(shí)現(xiàn)搶購某寶秒殺商品
這篇文章主要介紹了利用Python爬蟲實(shí)現(xiàn)搶購某寶秒殺商品,文章基于python的相關(guān)資料展開詳細(xì)的內(nèi)容介紹具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
Python3+selenium配置常見報(bào)錯(cuò)解決方案
這篇文章主要介紹了Python3+selenium配置常見報(bào)錯(cuò)解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
插入排序_Python與PHP的實(shí)現(xiàn)版(推薦)
下面小編就為大家?guī)硪黄迦肱判騙Python與PHP的實(shí)現(xiàn)版(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Python中import的用法陷阱解決盤點(diǎn)小結(jié)
這篇文章主要為大家介紹了Python中import的用法陷阱解決盤點(diǎn)小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10
python網(wǎng)絡(luò)爬蟲實(shí)現(xiàn)個(gè)性化音樂播放器示例解析
這篇文章主要為大家介紹了使用python網(wǎng)絡(luò)爬蟲實(shí)現(xiàn)個(gè)性化音樂播放器的詳細(xì)示例代碼以及內(nèi)容解析,有需要的朋友?可以借鑒參考下希望能夠有所幫助2022-03-03

