Python產(chǎn)生batch數(shù)據(jù)的操作
產(chǎn)生batch數(shù)據(jù)
輸入data中每個(gè)樣本可以有多個(gè)特征,和一個(gè)標(biāo)簽,最好都是numpy.array格式。
datas = [data1, data2, …, dataN ], labels = [label1, label2, …, labelN],
其中data[i] = [feature1, feature2,…featureM], 表示每個(gè)樣本數(shù)據(jù)有M個(gè)特征。
輸入我們方法的數(shù)據(jù),all_data = [datas, labels] 。
代碼實(shí)現(xiàn)
通過(guò)索引值來(lái)產(chǎn)生batch大小的數(shù)據(jù),同時(shí)提供是否打亂順序的選擇,根據(jù)隨機(jī)產(chǎn)生數(shù)據(jù)量范圍類(lèi)的索引值來(lái)打亂順序。
import numpy as np def batch_generator(all_data , batch_size, shuffle=True): """ :param all_data : all_data整個(gè)數(shù)據(jù)集,包含輸入和輸出標(biāo)簽 :param batch_size: batch_size表示每個(gè)batch的大小 :param shuffle: 是否打亂順序 :return: """ # 輸入all_datas的每一項(xiàng)必須是numpy數(shù)組,保證后面能按p所示取值 all_data = [np.array(d) for d in all_data] # 獲取樣本大小 data_size = all_data[0].shape[0] print("data_size: ", data_size) if shuffle: # 隨機(jī)生成打亂的索引 p = np.random.permutation(data_size) # 重新組織數(shù)據(jù) all_data = [d[p] for d in all_data] batch_count = 0 while True: # 數(shù)據(jù)一輪循環(huán)(epoch)完成,打亂一次順序 if batch_count * batch_size + batch_size > data_size: batch_count = 0 if shuffle: p = np.random.permutation(data_size) all_data = [d[p] for d in all_data] start = batch_count * batch_size end = start + batch_size batch_count += 1 yield [d[start: end] for d in all_data]
測(cè)試數(shù)據(jù)
樣本數(shù)據(jù)x和標(biāo)簽y可以分開(kāi)輸入,也可以同時(shí)輸入。
# 輸入x表示有23個(gè)樣本,每個(gè)樣本有兩個(gè)特征 # 輸出y表示有23個(gè)標(biāo)簽,每個(gè)標(biāo)簽取值為0或1 x = np.random.random(size=[23, 2]) y = np.random.randint(2, size=[23,1]) count = x.shape[0] batch_size = 5 epochs = 20 batch_num = count // batch_size batch_gen = batch_generator([x, y], batch_size) for i in range(epochs): print("##### epoch %s ##### " % i) for j in range(batch_num): batch_x, batch_y = next(batch_gen) print("-----epoch=%s, batch=%s-----" % (i, j)) print(batch_x, batch_y)
補(bǔ)充:使用tensorflow.data.Dataset構(gòu)造batch數(shù)據(jù)集
import tensorflow as tf import numpy as np def _parse_function(x): num_list = np.arange(10) return num_list def _from_tensor_slice(x): return tf.data.Dataset.from_tensor_slices(x) softmax_data = tf.data.Dataset.range(1000) # 構(gòu)造一個(gè)隊(duì)列 softmax_data = softmax_data.map(lambda x:tf.py_func(_parse_function, [x], [tf.int32]))# 將數(shù)據(jù)進(jìn)行傳入 softmax_data = softmax_data.flat_map(_from_tensor_slice) #將數(shù)據(jù)進(jìn)行平鋪, 將其變?yōu)橐痪S的數(shù)據(jù),from_tensor_slice將數(shù)據(jù)可以輸出 softmax_data = softmax_data.batch(1) #構(gòu)造一個(gè)batch的數(shù)量 softmax_iter = softmax_data.make_initializable_iterator() # 構(gòu)造數(shù)據(jù)迭代器 softmax_element = softmax_iter.get_next() # 獲得一個(gè)batch的數(shù)據(jù) sess = tf.Session() sess.run(softmax_iter.initializer) # 數(shù)據(jù)迭代器的初始化操作 print(sess.run(softmax_element)) # 實(shí)際獲得一個(gè)數(shù)據(jù) print(sess.run(softmax_data))
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- 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神經(jīng)網(wǎng)絡(luò)之批量學(xué)習(xí)tf.train.batch函數(shù)示例
- python生成器generator:深度學(xué)習(xí)讀取batch圖片的操作
- python非阻塞式后臺(tái)如何運(yùn)行bat腳本
相關(guān)文章
django celery redis使用具體實(shí)踐
這篇文章主要介紹了django celery redis使用具體實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Python pygame實(shí)現(xiàn)圖像基本變換的示例詳解
pygame的transform中封裝了一些基礎(chǔ)的圖像處理函數(shù),這篇文章主要為大家介紹了pygame實(shí)現(xiàn)圖像的基本變換,例如縮放、旋轉(zhuǎn)、鏡像等,感興趣的小伙伴可以了解一下2023-11-11python+flask實(shí)現(xiàn)API的方法
這篇文章主要為大家詳細(xì)介紹了python+flask實(shí)現(xiàn)API的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11python繼承threading.Thread實(shí)現(xiàn)有返回值的子類(lèi)實(shí)例
這篇文章主要介紹了python繼承threading.Thread實(shí)現(xiàn)有返回值的子類(lèi)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼
今天小編就為大家分享一篇關(guān)于Scrapy框架爬取Boss直聘網(wǎng)Python職位信息的源碼,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Python回調(diào)函數(shù)用法實(shí)例詳解
這篇文章主要介紹了Python回調(diào)函數(shù)用法,以實(shí)例形式較為詳細(xì)的分析了Python回調(diào)函數(shù)的定義、功能及相關(guān)使用技巧,需要的朋友可以參考下2015-07-07