欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

keras處理欠擬合和過擬合的實(shí)例講解

 更新時(shí)間:2020年05月25日 08:46:01   作者:Lzj000lzj  
這篇文章主要介紹了keras處理欠擬合和過擬合的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

baseline

import tensorflow.keras.layers as layers
baseline_model = keras.Sequential(
[
 layers.Dense(16, activation='relu', input_shape=(NUM_WORDS,)),
 layers.Dense(16, activation='relu'),
 layers.Dense(1, activation='sigmoid')
]
)
baseline_model.compile(optimizer='adam',
      loss='binary_crossentropy',
      metrics=['accuracy', 'binary_crossentropy'])
baseline_model.summary()

baseline_history = baseline_model.fit(train_data, train_labels,
          epochs=20, batch_size=512,
          validation_data=(test_data, test_labels),
          verbose=2)

小模型

small_model = keras.Sequential(
[
 layers.Dense(4, activation='relu', input_shape=(NUM_WORDS,)),
 layers.Dense(4, activation='relu'),
 layers.Dense(1, activation='sigmoid')
]
)
small_model.compile(optimizer='adam',
      loss='binary_crossentropy',
      metrics=['accuracy', 'binary_crossentropy'])
small_model.summary()
small_history = small_model.fit(train_data, train_labels,
          epochs=20, batch_size=512,
          validation_data=(test_data, test_labels),
          verbose=2)

大模型

big_model = keras.Sequential(
[
 layers.Dense(512, activation='relu', input_shape=(NUM_WORDS,)),
 layers.Dense(512, activation='relu'),
 layers.Dense(1, activation='sigmoid')
]
)
big_model.compile(optimizer='adam',
      loss='binary_crossentropy',
      metrics=['accuracy', 'binary_crossentropy'])
big_model.summary()
big_history = big_model.fit(train_data, train_labels,
          epochs=20, batch_size=512,
          validation_data=(test_data, test_labels),
          verbose=2)

繪圖比較上述三個(gè)模型

def plot_history(histories, key='binary_crossentropy'):
 plt.figure(figsize=(16,10))
 
 for name, history in histories:
 val = plt.plot(history.epoch, history.history['val_'+key],
     '--', label=name.title()+' Val')
 plt.plot(history.epoch, history.history[key], color=val[0].get_color(),
    label=name.title()+' Train')

 plt.xlabel('Epochs')
 plt.ylabel(key.replace('_',' ').title())
 plt.legend()

 plt.xlim([0,max(history.epoch)])


plot_history([('baseline', baseline_history),
    ('small', small_history),
    ('big', big_history)])

三個(gè)模型在迭代過程中在訓(xùn)練集的表現(xiàn)都會(huì)越來越好,并且都會(huì)出現(xiàn)過擬合的現(xiàn)象

大模型在訓(xùn)練集上表現(xiàn)更好,過擬合的速度更快

l2正則減少過擬合

l2_model = keras.Sequential(
[
 layers.Dense(16, kernel_regularizer=keras.regularizers.l2(0.001), 
     activation='relu', input_shape=(NUM_WORDS,)),
 layers.Dense(16, kernel_regularizer=keras.regularizers.l2(0.001), 
     activation='relu'),
 layers.Dense(1, activation='sigmoid')
]
)
l2_model.compile(optimizer='adam',
      loss='binary_crossentropy',
      metrics=['accuracy', 'binary_crossentropy'])
l2_model.summary()
l2_history = l2_model.fit(train_data, train_labels,
          epochs=20, batch_size=512,
          validation_data=(test_data, test_labels),
          verbose=2)
plot_history([('baseline', baseline_history),
    ('l2', l2_history)])

可以發(fā)現(xiàn)正則化之后的模型在驗(yàn)證集上的過擬合程度減少

添加dropout減少過擬合

dpt_model = keras.Sequential(
[
 layers.Dense(16, activation='relu', input_shape=(NUM_WORDS,)),
 layers.Dropout(0.5),
 layers.Dense(16, activation='relu'),
 layers.Dropout(0.5),
 layers.Dense(1, activation='sigmoid')
]
)
dpt_model.compile(optimizer='adam',
      loss='binary_crossentropy',
      metrics=['accuracy', 'binary_crossentropy'])
dpt_model.summary()
dpt_history = dpt_model.fit(train_data, train_labels,
          epochs=20, batch_size=512,
          validation_data=(test_data, test_labels),
          verbose=2)
plot_history([('baseline', baseline_history),
    ('dropout', dpt_history)])

批正則化

model = keras.Sequential([
 layers.Dense(64, activation='relu', input_shape=(784,)),
 layers.BatchNormalization(),
 layers.Dense(64, activation='relu'),
 layers.BatchNormalization(),
 layers.Dense(64, activation='relu'),
 layers.BatchNormalization(),
 layers.Dense(10, activation='softmax')
])
model.compile(optimizer=keras.optimizers.SGD(),
    loss=keras.losses.SparseCategoricalCrossentropy(),
    metrics=['accuracy'])
model.summary()
history = model.fit(x_train, y_train, batch_size=256, epochs=100, validation_split=0.3, verbose=0)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['training', 'validation'], loc='upper left')
plt.show()

總結(jié)

防止神經(jīng)網(wǎng)絡(luò)中過度擬合的最常用方法:

獲取更多訓(xùn)練數(shù)據(jù)。

減少網(wǎng)絡(luò)容量。

添加權(quán)重正規(guī)化。

添加dropout。

以上這篇keras處理欠擬合和過擬合的實(shí)例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python語法中的模糊語義

    Python語法中的模糊語義

    這篇文章主要介紹的是Python語法中的模糊語義,下面文章具體內(nèi)容包括切片不執(zhí)行越界檢查和報(bào)錯(cuò)、空列表的創(chuàng)建、閉包的延遲綁定,需要的朋友可以參考一下
    2021-11-11
  • Django 創(chuàng)建后臺(tái),配置sqlite3教程

    Django 創(chuàng)建后臺(tái),配置sqlite3教程

    今天小編就為大家分享一篇Django 創(chuàng)建后臺(tái),配置sqlite3教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python中str.format()詳解

    Python中str.format()詳解

    本文主要給大家詳細(xì)介紹的是python編程中str.format()的基本語法和高級(jí)用法,非常的詳細(xì),并附有示例,希望大家能夠喜歡
    2017-03-03
  • python3 自動(dòng)識(shí)別usb連接狀態(tài),即對(duì)usb重連的判斷方法

    python3 自動(dòng)識(shí)別usb連接狀態(tài),即對(duì)usb重連的判斷方法

    今天小編就為大家分享一篇python3 自動(dòng)識(shí)別usb連接狀態(tài),即對(duì)usb重連的判斷方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • python paramiko連接ssh實(shí)現(xiàn)命令

    python paramiko連接ssh實(shí)現(xiàn)命令

    這篇文章主要為大家介紹了python paramiko連接ssh實(shí)現(xiàn)的命令詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • Python3 hashlib密碼散列算法原理詳解

    Python3 hashlib密碼散列算法原理詳解

    這篇文章主要介紹了Python3 hashlib密碼散列算法原理詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python 調(diào)整圖片亮度的示例

    python 調(diào)整圖片亮度的示例

    這篇文章主要介紹了python 調(diào)整圖片亮度的示例代碼,幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下
    2020-12-12
  • Python常見異常的處理方式淺析

    Python常見異常的處理方式淺析

    異常指當(dāng)程序出現(xiàn)錯(cuò)誤后程序的處理方法,異常機(jī)制提供了程序正常退出的安全通道.當(dāng)出現(xiàn)錯(cuò)誤后,程序執(zhí)行的流程發(fā)生改變,程序的控制權(quán)轉(zhuǎn)移到異常處理器,如序列的下標(biāo)越界、打開不存在的文件、空引用異常等
    2023-02-02
  • Python端口掃描簡單程序

    Python端口掃描簡單程序

    這篇文章主要為大家詳細(xì)介紹了Python端口掃描簡單程序的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • 教你使用Python寫一個(gè)簡單的JSONParser

    教你使用Python寫一個(gè)簡單的JSONParser

    這篇文章主要介紹了教你使用Python寫一個(gè)簡單的JSONParser,它的整個(gè)效果,有點(diǎn)類似于 python 標(biāo)準(zhǔn)庫 json 的 json.load() 方法,需要的朋友可以參考下
    2023-04-04

最新評(píng)論