午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術(shù)文章
文章詳情頁

Python產(chǎn)生batch數(shù)據(jù)的操作

瀏覽:137日期:2022-06-25 13:16:32
產(chǎn)生batch數(shù)據(jù)

輸入data中每個樣本可以有多個特征,和一個標(biāo)簽,最好都是numpy.array格式。

datas = [data1, data2, …, dataN ], labels = [label1, label2, …, labelN],

其中data[i] = [feature1, feature2,…featureM], 表示每個樣本數(shù)據(jù)有M個特征。

輸入我們方法的數(shù)據(jù),all_data = [datas, labels] 。

代碼實現(xiàn)

通過索引值來產(chǎn)生batch大小的數(shù)據(jù),同時提供是否打亂順序的選擇,根據(jù)隨機產(chǎn)生數(shù)據(jù)量范圍類的索引值來打亂順序。

import numpy as npdef batch_generator(all_data , batch_size, shuffle=True): ''' :param all_data : all_data整個數(shù)據(jù)集,包含輸入和輸出標(biāo)簽 :param batch_size: batch_size表示每個batch的大小 :param shuffle: 是否打亂順序 :return: ''' # 輸入all_datas的每一項必須是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: # 隨機生成打亂的索引 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]測試數(shù)據(jù)

樣本數(shù)據(jù)x和標(biāo)簽y可以分開輸入,也可以同時輸入。

# 輸入x表示有23個樣本,每個樣本有兩個特征# 輸出y表示有23個標(biāo)簽,每個標(biāo)簽取值為0或1x = np.random.random(size=[23, 2])y = np.random.randint(2, size=[23,1])count = x.shape[0]batch_size = 5epochs = 20batch_num = count // batch_sizebatch_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)

補充:使用tensorflow.data.Dataset構(gòu)造batch數(shù)據(jù)集

import tensorflow as tfimport numpy as npdef _parse_function(x): num_list = np.arange(10) return num_listdef _from_tensor_slice(x): return tf.data.Dataset.from_tensor_slices(x)softmax_data = tf.data.Dataset.range(1000) # 構(gòu)造一個隊列softmax_data = softmax_data.map(lambda x:tf.py_func(_parse_function, [x], [tf.int32]))# 將數(shù)據(jù)進行傳入softmax_data = softmax_data.flat_map(_from_tensor_slice) #將數(shù)據(jù)進行平鋪, 將其變?yōu)橐痪S的數(shù)據(jù),from_tensor_slice將數(shù)據(jù)可以輸出softmax_data = softmax_data.batch(1) #構(gòu)造一個batch的數(shù)量softmax_iter = softmax_data.make_initializable_iterator() # 構(gòu)造數(shù)據(jù)迭代器softmax_element = softmax_iter.get_next() # 獲得一個batch的數(shù)據(jù)sess = tf.Session()sess.run(softmax_iter.initializer) # 數(shù)據(jù)迭代器的初始化操作print(sess.run(softmax_element)) # 實際獲得一個數(shù)據(jù)print(sess.run(softmax_data))

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 91黄色片 | 久久不卡免费视频 | 日韩在线亚洲 | 欧美二三区 | 亚洲精品69| 久草视频手机在线 | 久久久久久久久久久久久久av | 亚洲色图欧美视频 | 中国黄色片视频 | 性欧美极品另类 | 亚洲精品三 | 自拍天堂| 精品中文字幕在线 | 国产一级二级三级在线观看 | 91在线免费视频 | 亚洲一区二区精品在线观看 | 91黄色免费版 | 亚洲高清中文字幕 | 欧美大片黄色 | 日韩精品免费视频 | 91大神在线免费观看 | 国产成人精品影院 | 黄色av大片| wwwav | 黄色大片黄色大片 | 国产黄色自拍视频 | 亚洲一区二区视频 | av一二 | 亚洲手机av | 免费欧美一级片 | 97久久国产 | 91亚洲国产成人精品一区二区三 | www.xxxx国产 | 免费日本黄色片 | 国产日韩视频在线 | 亚洲成年人 | 成人女同在线观看 | 91毛片网站 | 日本中文字幕在线免费观看 | 香蕉视频在线网址 | 中文字幕视频一区二区 |