python神經(jīng)網(wǎng)絡(luò)之批量學(xué)習(xí)tf.train.batch函數(shù)示例
學(xué)習(xí)前言
當(dāng)我在快樂(lè)的學(xué)習(xí)SSD訓(xùn)練部分的時(shí)候,我發(fā)現(xiàn)了一個(gè)batch我看不太懂,主要是因?yàn)閠frecords的數(shù)據(jù)讀取方式我不理解,所以好好學(xué)一下batch吧
tf.train.batch函數(shù)
tf.train.batch( tensors, batch_size, num_threads=1, capacity=32, enqueue_many=False, shapes=None, dynamic_pad=False, allow_smaller_final_batch=False, shared_name=None, name=None )
其中:
1、tensors:利用slice_input_producer獲得的數(shù)據(jù)組合。
2、batch_size:設(shè)置每次從隊(duì)列中獲取出隊(duì)數(shù)據(jù)的數(shù)量。
3、num_threads:用來(lái)控制線程的數(shù)量,如果其值不唯一,由于線程執(zhí)行的特性,數(shù)據(jù)獲取可能變成亂序。
4、capacity:一個(gè)整數(shù),用來(lái)設(shè)置隊(duì)列中元素的最大數(shù)量
5、allow_samller_final_batch:當(dāng)其為T(mén)rue時(shí),如果隊(duì)列中的樣本數(shù)量小于batch_size,出隊(duì)的數(shù)量會(huì)以最終遺留下來(lái)的樣本進(jìn)行出隊(duì);當(dāng)其為False時(shí),小于batch_size的樣本不會(huì)做出隊(duì)處理。
6、name:名字
測(cè)試代碼
1、allow_samller_final_batch=True
import pandas as pd import numpy as np import tensorflow as tf # 生成數(shù)據(jù) def generate_data(): num = 18 label = np.arange(num) return label # 獲取數(shù)據(jù) def get_batch_data(): label = generate_data() input_queue = tf.train.slice_input_producer([label], shuffle=False,num_epochs=2) label_batch = tf.train.batch(input_queue, batch_size=5, num_threads=1, capacity=64,allow_smaller_final_batch=True) return label_batch # 數(shù)據(jù)組 label = get_batch_data() sess = tf.Session() # 初始化變量 sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) # 初始化batch訓(xùn)練的參數(shù) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess,coord) try: while not coord.should_stop(): # 自動(dòng)獲取下一組數(shù)據(jù) l = sess.run(label) print(l) except tf.errors.OutOfRangeError: print('Done training') finally: coord.request_stop() coord.join(threads) sess.close()
運(yùn)行結(jié)果為:
[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 0 1]
[2 3 4 5 6]
[ 7 8 9 10 11]
[12 13 14 15 16]
[17]
Done training
2、allow_samller_final_batch=False
相比allow_samller_final_batch=True,輸出結(jié)果少了[17]
import pandas as pd import numpy as np import tensorflow as tf # 生成數(shù)據(jù) def generate_data(): num = 18 label = np.arange(num) return label # 獲取數(shù)據(jù) def get_batch_data(): label = generate_data() input_queue = tf.train.slice_input_producer([label], shuffle=False,num_epochs=2) label_batch = tf.train.batch(input_queue, batch_size=5, num_threads=1, capacity=64,allow_smaller_final_batch=False) return label_batch # 數(shù)據(jù)組 label = get_batch_data() sess = tf.Session() # 初始化變量 sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) # 初始化batch訓(xùn)練的參數(shù) coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(sess,coord) try: while not coord.should_stop(): # 自動(dòng)獲取下一組數(shù)據(jù) l = sess.run(label) print(l) except tf.errors.OutOfRangeError: print('Done training') finally: coord.request_stop() coord.join(threads) sess.close()
運(yùn)行結(jié)果為:
[0 1 2 3 4]
[5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 0 1]
[2 3 4 5 6]
[ 7 8 9 10 11]
[12 13 14 15 16]
Done training
以上就是python神經(jīng)網(wǎng)絡(luò)之批量學(xué)習(xí)tf.train.batch函數(shù)示例的詳細(xì)內(nèi)容,更多關(guān)于python神經(jīng)網(wǎng)絡(luò)tf.train.batch的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- python報(bào)錯(cuò)解決之python運(yùn)行bat文件的各種問(wèn)題處理
- python實(shí)現(xiàn)遠(yuǎn)程運(yùn)行bat文件
- Windows11使用Cpython?編譯文件報(bào)錯(cuò)?error:?Unable?to?find?vcvarsall.bat?完美解決方法
- python神經(jīng)網(wǎng)絡(luò)Batch?Normalization底層原理詳解
- python生成器generator:深度學(xué)習(xí)讀取batch圖片的操作
- Python產(chǎn)生batch數(shù)據(jù)的操作
- python非阻塞式后臺(tái)如何運(yùn)行bat腳本
相關(guān)文章
解決Django提交表單報(bào)錯(cuò):CSRF token missing or incorrect的問(wèn)題
這篇文章主要介紹了解決Django提交表單報(bào)錯(cuò):CSRF token missing or incorrect的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Python中的 No Module named ***問(wèn)題及解決
這篇文章主要介紹了Python中的 No Module named ***問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07Python身份驗(yàn)證第三方庫(kù)(7個(gè))
身份驗(yàn)證在很多項(xiàng)目上都可以用到,本文主要介紹了Python身份驗(yàn)證第三方庫(kù)(7個(gè)),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12探索Python?Slice函數(shù)靈活而強(qiáng)大的序列切片技術(shù)
Python中的Slice函數(shù)是一種強(qiáng)大且靈活的序列切片技術(shù),用于從字符串、列表、元組等序列類(lèi)型中提取子集,本文將深入研究Slice函數(shù)的功能和用法,提供詳細(xì)的示例代碼和解釋,幫助讀者更全面地了解和應(yīng)用這一功能2024-01-01Python 實(shí)現(xiàn)數(shù)據(jù)庫(kù)更新腳本的生成方法
下面小編就為大家?guī)?lái)一篇Python 實(shí)現(xiàn)數(shù)據(jù)庫(kù)更新腳本的生成方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07基于Pytorch實(shí)現(xiàn)的聲音分類(lèi)實(shí)例代碼
聲音分類(lèi)是音頻深度學(xué)習(xí)中應(yīng)用最廣泛的方法之一,下面這篇文章主要給大家介紹了如何基于Pytorch實(shí)現(xiàn)聲音分類(lèi)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-06-06Python中if __name__ == "__main__"詳細(xì)解釋
這篇文章主要介紹了Python中if __name__ == "__main__"詳細(xì)解釋,需要的朋友可以參考下2014-10-10