最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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實(shí)現(xiàn)Markdown文檔格式轉(zhuǎn)換詳解

    利用Python實(shí)現(xiàn)Markdown文檔格式轉(zhuǎn)換詳解

    這篇文章主要介紹了如何利用Python中的MarkItDown庫將多種文件高效轉(zhuǎn)換為Markdown文本,以及如何使用Python-Markdown庫將Markdown文本轉(zhuǎn)換為HTML,需要的可以參考下
    2025-03-03
  • Python實(shí)現(xiàn)自定義Jupyter魔法命令

    Python實(shí)現(xiàn)自定義Jupyter魔法命令

    相信大家都用過?jupyter,也用過里面的魔法命令,這些魔法命令都以%或者%%開頭。用法還是比較簡單的,但是我們能不能自定義魔法命令呢?本文就來教大家如何自定義Jupyter魔法命令
    2022-08-08
  • Python裝飾器實(shí)現(xiàn)方法及應(yīng)用場景詳解

    Python裝飾器實(shí)現(xiàn)方法及應(yīng)用場景詳解

    這篇文章主要介紹了Python裝飾器實(shí)現(xiàn)方法及應(yīng)用場景詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Django異步任務(wù)之Celery的基本使用

    Django異步任務(wù)之Celery的基本使用

    這篇文章主要給大家介紹了關(guān)于Django異步任務(wù)之Celery使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Django具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Python WXPY實(shí)現(xiàn)微信監(jiān)控報(bào)警功能的代碼

    Python WXPY實(shí)現(xiàn)微信監(jiān)控報(bào)警功能的代碼

    本篇文章主要介紹了Python WXPY實(shí)現(xiàn)微信監(jiān)控報(bào)警功能的代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-10-10
  • python實(shí)現(xiàn)飛船游戲的縱向移動(dòng)

    python實(shí)現(xiàn)飛船游戲的縱向移動(dòng)

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)飛船游戲的縱向移動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 人工智能學(xué)習(xí)pyTorch自建數(shù)據(jù)集及可視化結(jié)果實(shí)現(xiàn)過程

    人工智能學(xué)習(xí)pyTorch自建數(shù)據(jù)集及可視化結(jié)果實(shí)現(xiàn)過程

    這篇文章主要為大家介紹了人工智能學(xué)習(xí)pyTorch自建數(shù)據(jù)集及可視化結(jié)果的實(shí)現(xiàn)過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • python+matplotlib演示電偶極子實(shí)例代碼

    python+matplotlib演示電偶極子實(shí)例代碼

    這篇文章主要介紹了python+matplotlib演示電偶極子實(shí)例代碼,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Python實(shí)現(xiàn)內(nèi)存泄露排查的示例詳解

    Python實(shí)現(xiàn)內(nèi)存泄露排查的示例詳解

    一般在python代碼塊的調(diào)試過程中會(huì)使用memory-profiler、filprofiler、objgraph等三種方式進(jìn)行輔助分析,今天這里主要介紹使用objgraph對象提供的函數(shù)接口來進(jìn)行內(nèi)存泄露的分析,感興趣的可以了解一下
    2023-01-01
  • Python中的http.server庫用法詳細(xì)介紹

    Python中的http.server庫用法詳細(xì)介紹

    這篇文章主要給大家介紹了關(guān)于Python中http.server庫用法的相關(guān)資料,http.server是Python標(biāo)準(zhǔn)庫中的一個(gè)模塊,用于創(chuàng)建基本的HTTP服務(wù)器,它提供了處理HTTP請求的基本框架和核心類,需要的朋友可以參考下
    2024-11-11

最新評論

武功县| 镇雄县| 田东县| 上犹县| 武威市| 衡阳县| 江门市| 万盛区| 富民县| 屏山县| 桑日县| 新和县| 三江| 台北市| 新巴尔虎右旗| 定日县| 思茅市| 县级市| 甘洛县| 兖州市| 泰兴市| 察哈| 曲麻莱县| 融水| 华池县| 潼关县| 邳州市| 疏勒县| 安阳市| 潼关县| 灵璧县| 班戈县| 合肥市| 吉木萨尔县| 龙口市| 馆陶县| 鄄城县| 澳门| 郧西县| 石屏县| 宝坻区|