溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么在Python項(xiàng)目中生成一個(gè)batch數(shù)據(jù)

發(fā)布時(shí)間:2021-03-11 15:00:40 來(lái)源:億速云 閱讀:399 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)怎么在Python項(xiàng)目中生成一個(gè)batch數(shù)據(jù),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

產(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)打亂順序。

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可以分開輸入,也可以同時(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))

關(guān)于怎么在Python項(xiàng)目中生成一個(gè)batch數(shù)據(jù)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI