python神經(jīng)網(wǎng)絡(luò)之批量學(xué)習(xí)tf.train.batch函數(shù)示例
學(xué)習(xí)前言
當(dāng)我在快樂的學(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:用來控制線程的數(shù)量,如果其值不唯一,由于線程執(zhí)行的特性,數(shù)據(jù)獲取可能變成亂序。
4、capacity:一個(gè)整數(shù),用來設(shè)置隊(duì)列中元素的最大數(shù)量
5、allow_samller_final_batch:當(dāng)其為True時(shí),如果隊(duì)列中的樣本數(shù)量小于batch_size,出隊(duì)的數(shù)量會(huì)以最終遺留下來的樣本進(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文件的各種問題處理
- 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)文章
Anaconda下安裝mysql-python的包實(shí)例
今天小編就為大家分享一篇Anaconda下安裝mysql-python的包實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06
TensorFlow自定義模型保存加載和分布式訓(xùn)練
本篇文章將涵蓋 TensorFlow 的高級(jí)應(yīng)用,包括如何自定義模型的保存和加載過程,以及如何進(jìn)行分布式訓(xùn)練,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
python 地圖經(jīng)緯度轉(zhuǎn)換、糾偏的實(shí)例代碼
這篇文章主要介紹了python 地圖經(jīng)緯度轉(zhuǎn)換、糾偏的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
Python小紅書旋轉(zhuǎn)驗(yàn)證碼識(shí)別實(shí)戰(zhàn)教程
這篇文章主要介紹了Python小紅書旋轉(zhuǎn)驗(yàn)證碼識(shí)別實(shí)戰(zhàn)教程,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-08-08
Windows 7下Python Web環(huán)境搭建圖文教程
這篇文章主要為大家詳細(xì)介紹了Windows 7下Python Web環(huán)境搭建圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03
Python基于百度AI實(shí)現(xiàn)OCR文字識(shí)別
這篇文章主要介紹了Python基于百度AI實(shí)現(xiàn)OCR文字識(shí)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

