第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何將訓(xùn)練數(shù)據(jù)分成更小的批次來(lái)解決內(nèi)存錯(cuò)誤

如何將訓(xùn)練數(shù)據(jù)分成更小的批次來(lái)解決內(nèi)存錯(cuò)誤

白衣非少年 2023-07-27 10:24:52
我有一個(gè)包含兩個(gè)多維數(shù)組的訓(xùn)練數(shù)據(jù) [prev_sentences, current_sentences],當(dāng)我使用簡(jiǎn)單的 model.fit 方法時(shí),它給了我內(nèi)存錯(cuò)誤。我現(xiàn)在想使用 fit_generator,但我不知道如何將訓(xùn)練數(shù)據(jù)分成批次以輸入 model.fit_generator。訓(xùn)練數(shù)據(jù)的形狀為(111356,126,1024)和(111356,126,1024),y_train形狀為(111356,19)。這是簡(jiǎn)單擬合方法的代碼行。history=model.fit([previous_sentences, current_sentences], y_train,                   epochs=15,batch_size=256,                   shuffle = False, verbose = 1,                   validation_split=0.2,                   class_weight=custom_weight_dict,                   callbacks=[early_stopping_cb])我從未使用過(guò) fit_generator 和數(shù)據(jù)生成器,所以我不知道如何拆分這些訓(xùn)練數(shù)據(jù)以使用 fit_generator。任何人都可以幫助我使用 fit_generator 創(chuàng)建批次嗎?
查看完整描述

2 回答

?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

您只需要撥打:

model.fit_generator(generator,?steps_per_epoch)

其中steps_per_epoch是通常ceil(num_samples / batch_size)并且generator是一個(gè) python 生成器,它迭代數(shù)據(jù)并批量生成數(shù)據(jù)。每次調(diào)用生成器都應(yīng)該產(chǎn)生batch_size許多元素。生成器的示例:

def generate_data(directory, batch_size):

? ? """Replaces Keras' native ImageDataGenerator."""

? ? i = 0

? ? file_list = os.listdir(directory)

? ? while True:

? ? ? ? image_batch = []

? ? ? ? for b in range(batch_size):

? ? ? ? ? ? if i == len(file_list):

? ? ? ? ? ? ? ? i = 0

? ? ? ? ? ? ? ? random.shuffle(file_list)

? ? ? ? ? ? sample = file_list[i]

? ? ? ? ? ? i += 1

? ? ? ? ? ? image = cv2.resize(cv2.imread(sample[0]), INPUT_SHAPE)

? ? ? ? ? ? image_batch.append((image.astype(float) - 128) / 128)


? ? ? ? yield np.array(image_batch)

由于這絕對(duì)是特定于問(wèn)題的,因此您必須編寫(xiě)自己的生成器,盡管使用此模板應(yīng)該很簡(jiǎn)單。


查看完整回答
反對(duì) 回復(fù) 2023-07-27
?
慕虎7371278

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是將訓(xùn)練數(shù)據(jù)分割成小批量的數(shù)據(jù)生成器:


def generate_data(X1,X2,Y,batch_size):

  p_input=[]

  c_input=[]

  target=[]

  batch_count=0

  for i in range(len(X1)):

    p_input.append(X1[i])

    c_input.append(X2[i])

    target.append(Y[i])

    batch_count+=1

    if batch_count>batch_size:

      prev_X=np.array(p_input,dtype=np.int64)

      cur_X=np.array(c_input,dtype=np.int64)

      cur_y=np.array(target,dtype=np.int32)

      print(len(prev_X),len(cur_X))

      yield ([prev_X,cur_X],cur_y ) 

      p_input=[]

      c_input=[]

      target=[]

      batch_count=0

  return

這里是 fit_generator 函數(shù)調(diào)用而不是 model.fit 方法:


batch_size=256

epoch_steps=math.ceil(len(previous_sentences)/ batch_size)

hist = model.fit_generator(generate_data(previous_sentences,current_sentences, y_train, batch_size),

                steps_per_epoch=epoch_steps,

                callbacks = [early_stopping_cb],

                validation_data=generate_data(val_prev, val_curr,y_val,batch_size),

                validation_steps=val_steps,  class_weight=custom_weight_dict,

                 verbose=1)


查看完整回答
反對(duì) 回復(fù) 2023-07-27
  • 2 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)