<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 基于 Keras 的深度學習模型中的dropout正則化 > 原文: [https://machinelearningmastery.com/dropout-regularization-deep-learning-models-keras/](https://machinelearningmastery.com/dropout-regularization-deep-learning-models-keras/) 神經網絡和深度學習模型的簡單而強大的正則化技術是dropout。 在這篇文章中,您將了解 dropout 正則化技術以及如何將其應用于使用 Keras 用 Python 編寫的模型中。 閱讀這篇文章后你會知道: * dropout正則化技術原理。 * 如何在輸入層上使用 dropout。 * 如何在隱藏層上使用 dropout。 * 如何針對具體問題對dropout調優 讓我們開始吧。 * **2016 年 10 月更新**:更新了 Keras 1.1.0,TensorFlow 0.10.0 和 scikit-learn v0.18 的示例。 * **2017 年 3 月更新**:更新了 Keras 2.0.2,TensorFlow 1.0.1 和 Theano 0.9.0 的示例。 ![Dropout Regularization in Deep Learning Models With Keras](https://img.kancloud.cn/e9/88/e988126f1939832e7452d936c60de064_640x425.png) 使用 Keras 的深度學習模型中的dropout正規化 照片由 [Trekking Rinjani](https://www.flickr.com/photos/trekkingrinjani/4930552641/) ,保留一些權利。 ## 神經網絡的 dropout 正則化 Dropout 是 Srivastava 等人提出的神經網絡模型的正則化技術。在他們的 2014 年論文[dropout:一種防止神經網絡過度擬合的簡單方法](http://jmlr.org/papers/v15/srivastava14a.html)([下載 PDF](http://jmlr.org/papers/volume15/srivastava14a/srivastava14a.pdf) )。 dropout是一種在訓練過程中忽略隨機選擇的神經元的技術。這些神經元被隨機“dropout“,這意味著它們對激活下一層神經元的貢獻在正向傳遞時暫時消除,并且在反向傳遞時任何權重更新也不會應用于這些神經元。 當神經網絡學習時,網絡中的神經元的權重將進行調整重置。神經元的權重針對某些特征進行調優,具有一些特殊化。周圍的神經元則會依賴于這種特殊化,如果過于特殊化,模型會因為對訓練數據過擬合而變得脆弱不堪。神經元在訓練過程中的這種依賴于上下文的現象被稱為復雜的協同適應(complex co-adaptations)。 你可以想象到如果神經元在訓練過程中被隨機丟棄,那么其他神經元因缺失神經元不得不介入并替代缺失神經元的那部分表征,為預測結果提供信息。人們認為這樣網絡模型可以學到多種相互獨立的內部表征。 其結果是網絡對神經元的特定權重變得不那么敏感。這反過來使得網絡能夠更好地泛化,減少了過度擬合訓練數據的可能性。 ## Keras的dropout規范化 通過以每輪權重更新時的給定概率(例如 20%)隨機選擇要丟棄的節點、。這就是在Keras實施 Dropout 的方式。 Dropout 僅在模型訓練期間使用,在評估模型的性能時不使用。 接下來,我們將探討在 Keras 中使用 Dropout 的幾種不同方法。 這些示例將使用 [Sonar 數據集](http://archive.ics.uci.edu/ml/datasets/Connectionist+Bench+(Sonar,+Mines+vs.+Rocks))。這是一個二元分類問題,其目標是利用聲納回聲正確識別巖石和模擬地雷。它是神經網絡的一個很好的測試數據集,因為所有輸入值都是數字的并且具有相同的量綱。 數據集可以是從 UCI 機器學習庫下載的[。您可以將聲納數據集放在當前工作目錄中,文件名為 sonar.csv。](http://archive.ics.uci.edu/ml/machine-learning-databases/undocumented/connectionist-bench/sonar/sonar.all-data) 我們將使用帶有 10 折交叉驗證的 scikit-learn 來評估模型的質量,以便更好地梳理結果中的差異。 有 60 個輸入值和一個輸出值,輸入值在用于網絡之前已歸一化。基準神經網絡模型具有兩個隱藏層,第一個具有 60 個單元,第二個具有 30 個。隨機梯度下降用于訓練具有相對低的學習率和沖量的模型。 下面列出了完整的基線模型。 ```py # Baseline Model on the Sonar Dataset import numpy from pandas import read_csv from keras.models import Sequential from keras.layers import Dense from keras.layers import Dropout from keras.wrappers.scikit_learn import KerasClassifier from keras.constraints import maxnorm from keras.optimizers import SGD from sklearn.model_selection import cross_val_score from sklearn.preprocessing import LabelEncoder from sklearn.model_selection import StratifiedKFold from sklearn.preprocessing import StandardScaler from sklearn.pipeline import Pipeline # fix random seed for reproducibility seed = 7 numpy.random.seed(seed) # load dataset dataframe = read_csv("sonar.csv", header=None) dataset = dataframe.values # split into input (X) and output (Y) variables X = dataset[:,0:60].astype(float) Y = dataset[:,60] # encode class values as integers encoder = LabelEncoder() encoder.fit(Y) encoded_Y = encoder.transform(Y) # baseline def create_baseline(): # create model model = Sequential() model.add(Dense(60, input_dim=60, kernel_initializer='normal', activation='relu')) model.add(Dense(30, kernel_initializer='normal', activation='relu')) model.add(Dense(1, kernel_initializer='normal', activation='sigmoid')) # Compile model sgd = SGD(lr=0.01, momentum=0.8, decay=0.0, nesterov=False) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) return model numpy.random.seed(seed) estimators = [] estimators.append(('standardize', StandardScaler())) estimators.append(('mlp', KerasClassifier(build_fn=create_baseline, epochs=300, batch_size=16, verbose=0))) pipeline = Pipeline(estimators) kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed) results = cross_val_score(pipeline, X, encoded_Y, cv=kfold) print("Baseline: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100)) ``` 運行該示例可生成 分類準確度約為86%。 ```py Baseline: 86.04% (4.58%) ``` ## 在可見層上使用 Dropout Dropout 可以應用于稱為可見層的輸入神經元。 在下面的示例中,我們在輸入(或可見層)和第一個隱藏層之間添加一個新的 Dropout 層。dropout率設置為 20%,這意味著每個更新周期中將隨機丟棄五分之一輸入。 此外,正如 Dropout 那篇論文中所建議的那樣,對每個隱藏層的權重施加約束,確保權重的最大范數不超過值 3.這可以通過在構造模型層時設置 kernel_constraint 參數來完成。 學習率提高了一個數量級,沖量增加到 0.9。 這也是Dropout 論文中推薦的做法。 繼續上面的基準示例,下面的代碼使用輸入層dropout的網絡模型。 ```py # dropout in the input layer with weight constraint def create_model(): # create model model = Sequential() model.add(Dropout(0.2, input_shape=(60,))) model.add(Dense(60, kernel_initializer='normal', activation='relu', kernel_constraint=maxnorm(3))) model.add(Dense(30, kernel_initializer='normal', activation='relu', kernel_constraint=maxnorm(3))) model.add(Dense(1, kernel_initializer='normal', activation='sigmoid')) # Compile model sgd = SGD(lr=0.1, momentum=0.9, decay=0.0, nesterov=False) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) return model numpy.random.seed(seed) estimators = [] estimators.append(('standardize', StandardScaler())) estimators.append(('mlp', KerasClassifier(build_fn=create_model, epochs=300, batch_size=16, verbose=0))) pipeline = Pipeline(estimators) kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed) results = cross_val_score(pipeline, X, encoded_Y, cv=kfold) print("Visible: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100)) ``` 運行該示例在單次測試運行中分類精度小幅下降。 ```py Visible: 83.52% (7.68%) ``` ## 在隱藏層上使用 Dropout Dropout 可以應用于網絡模型內的隱藏層節點。 在下面的示例中,Dropout 應用于兩個隱藏層之間以及最后一個隱藏層和輸出層之間。再次使用 20%的dropout率,并且對這些層進行權重約束。 ```py # dropout in hidden layers with weight constraint def create_model(): # create model model = Sequential() model.add(Dense(60, input_dim=60, kernel_initializer='normal', activation='relu', kernel_constraint=maxnorm(3))) model.add(Dropout(0.2)) model.add(Dense(30, kernel_initializer='normal', activation='relu', kernel_constraint=maxnorm(3))) model.add(Dropout(0.2)) model.add(Dense(1, kernel_initializer='normal', activation='sigmoid')) # Compile model sgd = SGD(lr=0.1, momentum=0.9, decay=0.0, nesterov=False) model.compile(loss='binary_crossentropy', optimizer=sgd, metrics=['accuracy']) return model numpy.random.seed(seed) estimators = [] estimators.append(('standardize', StandardScaler())) estimators.append(('mlp', KerasClassifier(build_fn=create_model, epochs=300, batch_size=16, verbose=0))) pipeline = Pipeline(estimators) kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=seed) results = cross_val_score(pipeline, X, encoded_Y, cv=kfold) print("Hidden: %.2f%% (%.2f%%)" % (results.mean()*100, results.std()*100)) ``` 我們可以看到,針對此問題以及所選模型配置參數,在隱藏層中使用 dropout 并未提升模型效果。事實上,表現比基準差。 可能需要更多的訓練迭代次數或者需要進一步調整學習率。 ```py Hidden: 83.59% (7.31%) ``` ## 使用 Dropout 的技巧 關于 Dropout 的原始論文提供了一套標準機器學習問題的實踐性結論。因此在運用dropout時,會帶來很多幫助。 * 通常,使用 20%-50%神經元的小dropout值,20%可作為良好的起點。比例太低具有最小的影響比列太高會導致模型的欠學習。 * 使用更大的網絡。當在較大的網絡上使用 dropout 時,模型可能會獲得更好的表現,模型有更多的機會學習到多種獨立的表征。 * 在輸入層(可見層)和隱藏層都使用 dropout。在網絡的每一層應用 dropout 已被證明具有良好的結果。 * 增加學習率和沖量。將學習率提高 10 到 100 倍,并使用 0.9 或 0.99 的高沖量值。 * 限制網絡模型權重的大小。較大的學習率可能導致非常大的權重值。對網絡的權重值做最大范數正則化等方法,例如大小為 4 或 5 的最大范數正則化已被證明可以改善結果。 ## 關于dropout的更多資源 以下是一些資源,您可以用它們來了解有關神經網絡和深度學習模型中的 dropout 的更多信息。 * [dropout:一種防止神經網絡過度擬合的簡單方法](http://jmlr.org/papers/v15/srivastava14a.html)(原始論文)。 * [通過阻止特征檢測器的共同適應來改善神經網絡](http://arxiv.org/abs/1207.0580)。 * [dropout方法如何在深度學習中發揮作用? Quora 上的](https://www.quora.com/How-does-the-dropout-method-work-in-deep-learning)。 ## 總結 在這篇文章中,您了解了深度學習模型的 dropout 正則化技術。你了解到: * dropout含義和原理。 * 如何在自己的深度學習模型中使用 dropout。 * 使用dropout的技巧。 您對dropout或這篇文章有任何疑問嗎?在評論中提出您的問題,我會盡力回答。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看