<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>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Python 中的 Keras 深度學習庫的回歸教程 > 原文: [https://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/](https://machinelearningmastery.com/regression-tutorial-keras-deep-learning-library-python/) Keras 是一個深度學習庫,包含高效的數字庫 Theano 和 TensorFlow。 在這篇文章中,您將了解如何使用 Keras 開發和評估神經網絡模型以獲得回歸問題。 完成本分步教程后,您將了解: * 如何加載 CSV 數據集并使其可供 Keras 使用。 * 如何使用 Keras 創建一個回歸問題的神經網絡模型。 * 如何使用 scras-learn 與 Keras 一起使用交叉驗證來評估模型。 * 如何進行數據準備以提高 Keras 模型的技能。 * 如何使用 Keras 調整模型的網絡拓撲。 讓我們開始吧。 * **2017 年 3 月更新**:更新了 Keras 2.0.2,TensorFlow 1.0.1 和 Theano 0.9.0 的示例。 * **更新 Mar / 2018** :添加了備用鏈接以下載數據集,因為原始圖像已被刪除。 * **Update Apr / 2018** :將 nb_epoch 參數更改為 epochs。 ![Regression Tutorial with Keras Deep Learning Library in Python](https://img.kancloud.cn/7b/d5/7bd5cce73761837b99b39877e5e0f2ae_640x425.png) 使用 Python 中的 Keras 深度學習庫的回歸教程 [Salim Fadhley](https://www.flickr.com/photos/salimfadhley/130295135/) 的照片,保留一些權利。 ## 1.問題描述 我們將在本教程中看到的問題是[波士頓房價數據集](https://archive.ics.uci.edu/ml/datasets/Housing)。 您可以下載此數據集并將其保存到當前工作中,直接使用文件名 _housing.csv_ (更新:[從此處下載數據](https://raw.githubusercontent.com/jbrownlee/Datasets/master/housing.data))。 該數據集描述了波士頓郊區房屋的 13 個數字屬性,并涉及以數千美元模擬這些郊區房屋的價格。因此,這是回歸預測建模問題。輸入屬性包括犯罪率,非經營業務占地比例,化學品濃度等。 這是機器學習中經過深入研究的問題。使用起來很方便,因為所有輸入和輸出屬性都是數字的,并且有 506 個實例可供使用。 使用均方誤差(MSE)評估的模型的合理表現約為 20 平方千美元(如果取平方根則為 4,500 美元)。這是一個很好的目標,旨在與我們的神經網絡模型。 ## 2.開發基線神經網絡模型 在本節中,我們將為回歸問題創建基線神經網絡模型。 讓我們從包含本教程所需的所有函數和對象開始。 ```py import numpy import pandas from keras.models import Sequential from keras.layers import Dense from keras.wrappers.scikit_learn import KerasRegressor from sklearn.model_selection import cross_val_score from sklearn.model_selection import KFold from sklearn.preprocessing import StandardScaler from sklearn.pipeline import Pipeline ``` 我們現在可以從本地目錄中的文件加載數據集。 事實上,數據集在 UCI 機器學習庫中不是 CSV 格式,而是用空格分隔屬性。我們可以使用 pandas 庫輕松加載它。然后我們可以分割輸入(X)和輸出(Y)屬性,以便使用 Keras 和 scikit-learn 更容易建模。 ```py # load dataset dataframe = pandas.read_csv("housing.csv", delim_whitespace=True, header=None) dataset = dataframe.values # split into input (X) and output (Y) variables X = dataset[:,0:13] Y = dataset[:,13] ``` 我們可以使用 Keras 庫提供的方便的包裝器對象創建 Keras 模型并使用 scikit-learn 來評估它們。這是可取的,因為 scikit-learn 在評估模型方面表現優異,并且允許我們使用強大的數據準備和模型評估方案,只需很少的代碼。 Keras 包裝器需要一個函數作為參數。我們必須定義的這個函數負責創建要評估的神經網絡模型。 下面我們定義用于創建要評估的基線模型的函數。它是一個簡單的模型,它有一個完全連接的隱藏層,與輸入屬性具有相同數量的神經元(13)。網絡使用良好的實踐,例如隱藏層的整流器激活功能。沒有激活函數用于輸出層,因為它是回歸問題,我們有興趣直接預測數值而不進行變換。 使用有效的 ADAM 優化算法并且優化均方誤差損失函數。這將與我們用于評估模型表現的指標相同。這是一個理想的指標,因為通過取平方根給出了一個誤差值,我們可以在問題的背景下直接理解(數千美元)。 ```py # define base model def baseline_model(): # create model model = Sequential() model.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu')) model.add(Dense(1, kernel_initializer='normal')) # Compile model model.compile(loss='mean_squared_error', optimizer='adam') return model ``` 用于 scikit-learn 作為回歸估計器的 Keras 包裝器對象稱為 KerasRegressor。我們創建一個實例并將其傳遞給函數的名稱以創建神經網絡模型以及一些參數以便稍后傳遞給模型的 fit()函數,例如時期數和批量大小。這兩個都設置為合理的默認值。 我們還使用常量隨機種子初始化隨機數生成器,我們將為本教程中評估的每個模型重復該過程。這是為了確保我們一致地比較模型。 ```py # fix random seed for reproducibility seed = 7 numpy.random.seed(seed) # evaluate model with standardized dataset estimator = KerasRegressor(build_fn=baseline_model, epochs=100, batch_size=5, verbose=0) ``` 最后一步是評估此基線模型。我們將使用 10 倍交叉驗證來評估模型。 ```py kfold = KFold(n_splits=10, random_state=seed) results = cross_val_score(estimator, X, Y, cv=kfold) print("Results: %.2f (%.2f) MSE" % (results.mean(), results.std())) ``` 運行此代碼可以估算出模型在看不見的數據問題上的表現。結果報告了均方誤差,包括交叉驗證評估的所有 10 倍的平均值和標準偏差(平均方差)。 ```py Baseline: 31.64 (26.82) MSE ``` ## 3.建模標準化數據集 波士頓房價數據集的一個重要問題是輸入屬性的尺度各不相同,因為它們測量的數量不同。 在使用神經網絡模型對數據進行建模之前準備數據幾乎總是好的做法。 繼續上述基線模型,我們可以使用輸入數據集的標準化版本重新評估相同的模型。 我們可以使用 scikit-learn 的 [Pipeline](http://scikit-learn.org/stable/modules/generated/sklearn.pipeline.Pipeline.html) 框架在模型評估過程中,在交叉驗證的每個折疊內執行標準化。這確保了每個測試集交叉驗證折疊中沒有數據泄漏到訓練數據中。 下面的代碼創建了一個 scikit-learn Pipeline,它首先標準化數據集,然后創建和評估基線神經網絡模型。 ```py # evaluate model with standardized dataset numpy.random.seed(seed) estimators = [] estimators.append(('standardize', StandardScaler())) estimators.append(('mlp', KerasRegressor(build_fn=baseline_model, epochs=50, batch_size=5, verbose=0))) pipeline = Pipeline(estimators) kfold = KFold(n_splits=10, random_state=seed) results = cross_val_score(pipeline, X, Y, cv=kfold) print("Standardized: %.2f (%.2f) MSE" % (results.mean(), results.std())) ``` 運行該示例可提供比基線模型更高的表現,而無需標準化數據,從而丟棄錯誤。 ```py Standardized: 29.54 (27.87) MSE ``` 此部分的進一步擴展將類似地對輸出變量應用重新縮放,例如將其歸一化到 0-1 的范圍,并在輸出層上使用 Sigmoid 或類似的激活函數,以將輸出預測縮小到相同的范圍。 ## 4.調整神經網絡拓撲 有許多問題可以針對神經網絡模型進行優化。 也許最大的杠桿點是網絡本身的結構,包括層數和每層神經元的數量。 在本節中,我們將評估另外兩種網絡拓撲,以進一步提高模型的表現。我們將研究更深入和更廣泛的網絡拓撲。 ### 4.1。評估更深入的網絡拓撲 提高神經網絡表現的一種方法是添加更多層。這可能允許模型提取并重新組合數據中嵌入的高階特征。 在本節中,我們將評估向模型添加一個隱藏層的效果。這就像定義一個新函數一樣簡單,這個函數將創建這個更深層次的模型,從上面的基線模型中復制出來。然后我們可以在第一個隱藏層之后插入一個新行。在這種情況下,神經元的數量約為一半。 ```py # define the model def larger_model(): # create model model = Sequential() model.add(Dense(13, input_dim=13, kernel_initializer='normal', activation='relu')) model.add(Dense(6, kernel_initializer='normal', activation='relu')) model.add(Dense(1, kernel_initializer='normal')) # Compile model model.compile(loss='mean_squared_error', optimizer='adam') return model ``` 我們的網絡拓撲現在看起來像: ```py 13 inputs -> [13 -> 6] -> 1 output ``` 我們可以采用與上述相同的方式評估此網絡拓撲,同時還使用上面顯示的數據集的標準化來提高表現。 ```py numpy.random.seed(seed) estimators = [] estimators.append(('standardize', StandardScaler())) estimators.append(('mlp', KerasRegressor(build_fn=larger_model, epochs=50, batch_size=5, verbose=0))) pipeline = Pipeline(estimators) kfold = KFold(n_splits=10, random_state=seed) results = cross_val_score(pipeline, X, Y, cv=kfold) print("Larger: %.2f (%.2f) MSE" % (results.mean(), results.std())) ``` 運行此模型確實表明表現從 28 降至 24,000 平方美元進一步改善。 ```py Larger: 22.83 (25.33) MSE ``` ### 4.2。評估更廣泛的網絡拓撲 增加模型的表示能力的另一種方法是創建更廣泛的網絡。 在本節中,我們將評估保持淺層網絡架構的效果,并使一個隱藏層中的神經元數量幾乎翻倍。 同樣,我們需要做的就是定義一個創建神經網絡模型的新函數。在這里,與 13 到 20 的基線模型相比,我們增加了隱藏層中神經元的數量。 ```py # define wider model def wider_model(): # create model model = Sequential() model.add(Dense(20, input_dim=13, kernel_initializer='normal', activation='relu')) model.add(Dense(1, kernel_initializer='normal')) # Compile model model.compile(loss='mean_squared_error', optimizer='adam') return model ``` 我們的網絡拓撲現在看起來像: ```py 13 inputs -> [20] -> 1 output ``` 我們可以使用與上面相同的方案評估更廣泛的網絡拓撲: ```py numpy.random.seed(seed) estimators = [] estimators.append(('standardize', StandardScaler())) estimators.append(('mlp', KerasRegressor(build_fn=wider_model, epochs=100, batch_size=5, verbose=0))) pipeline = Pipeline(estimators) kfold = KFold(n_splits=10, random_state=seed) results = cross_val_score(pipeline, X, Y, cv=kfold) print("Wider: %.2f (%.2f) MSE" % (results.mean(), results.std())) ``` 建立模型的確看到誤差進一步下降到大約 2.1 萬平方美元。對于這個問題,這不是一個糟糕的結果。 ```py Wider: 21.64 (23.75) MSE ``` 很難想象更廣泛的網絡在這個問題上會勝過更深層次的網絡。結果證明了在開發神經網絡模型時經驗測試的重要性。 ## 摘要 在這篇文章中,您發現了用于建模回歸問題的 Keras 深度學習庫。 通過本教程,您學習了如何開發和評估神經網絡模型,包括: * 如何加載數據和開發基線模型。 * 如何使用標準化等數據準備技術提升表現。 * 如何針對問題設計和評估具有不同變化拓撲的網絡。 您對 Keras 深度學習庫或這篇文章有任何疑問嗎?在評論中提出您的問題,我會盡力回答。
                  <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>

                              哎呀哎呀视频在线观看