machine-learning
  • 機器學習:使用Python
    • 簡介Scikit-learn 機器學習
  • 分類法 Classification
    • Ex 1: Recognizing hand-written digits
    • EX 2: Normal and Shrinkage Linear Discriminant Analysis for classification
    • EX 3: Plot classification probability
    • EX 4: Classifier Comparison
    • EX 5: Linear and Quadratic Discriminant Analysis with confidence ellipsoid
  • 特徵選擇 Feature Selection
    • Ex 1: Pipeline Anova SVM
    • Ex 2: Recursive Feature Elimination
    • Ex 3: Recursive Feature Elimination with Cross-Validation
    • Ex 4: Feature Selection using SelectFromModel
    • Ex 5: Test with permutations the significance of a classification score
    • Ex 6: Univariate Feature Selection
    • Ex 7: Comparison of F-test and mutual information
  • 互分解 Cross Decomposition
  • 通用範例 General Examples
    • Ex 1: Plotting Cross-Validated Predictions
    • Ex 2: Concatenating multiple feature extraction methods
    • Ex 3: Isotonic Regression
    • Ex 4: Imputing missing values before building an estimator
    • Ex 5: ROC Curve with Visualization API
    • Ex 7: Face completion with a multi-output estimators
  • 群聚法 Clustering
    • EX 1: Feature_agglomeration.md
    • EX 2: Mean-shift 群聚法.md
    • EX 6: 以群聚法切割錢幣影像.md
    • EX 10:_K-means群聚法
    • EX 12: Spectral clustering for image segmentation
    • Plot Hierarchical Clustering Dendrogram
  • 支持向量機
    • EX 1:Non_linear_SVM.md
    • [EX 4: SVM_with _custom _kernel.md](SVM/EX4_SVM_with _custom _kernel.md)
  • 機器學習資料集 Datasets
    • Ex 1: The digits 手寫數字辨識
    • Ex 2: Plot randomly generated classification dataset 分類數據集
    • Ex 3: The iris 鳶尾花資料集
    • Ex 4: Plot randomly generated multilabel dataset 多標籤數據集
  • 應用範例 Application
    • 用特徵臉及SVM進行人臉辨識實例
    • 維基百科主要的特徵向量
    • 波士頓房地產雲端評估(一)
    • 波士頓房地產雲端評估(二)
  • 類神經網路 Neural_Networks
    • Ex 1: Visualization of MLP weights on MNIST
    • Ex 2: Restricted Boltzmann Machine features for digit classification
    • Ex 3: Compare Stochastic learning strategies for MLPClassifier
    • Ex 4: Varying regularization in Multi-layer Perceptron
  • 決策樹 Decision_trees
    • Ex 1: Decision Tree Regression
    • Ex 2: Multi-output Decision Tree Regression
    • Ex 3: Plot the decision surface of a decision tree on the iris dataset
    • Ex 4: Understanding the decision tree structure
  • 機器學習:使用 NVIDIA JetsonTX2
    • 從零開始
    • 讓 TX2 動起來
    • 安裝OpenCV
    • 安裝TensorFlow
  • 廣義線性模型 Generalized Linear Models
    • Ex 3: SGD: Maximum margin separating hyperplane
  • 模型選擇 Model Selection
    • Ex 3: Plotting Validation Curves
    • Ex 4: Underfitting vs. Overfitting
  • 半監督式分類法 Semi-Supervised Classification
    • Ex 3: Label Propagation digits: Demonstrating performance
    • Ex 4: Label Propagation digits active learning
    • Decision boundary of label propagation versus SVM on the Iris dataset
  • Ensemble_methods
    • IsolationForest example
  • Miscellaneous_examples
    • Multilabel classification
  • Nearest_Neighbors
    • Nearest Neighbors Classification
Powered by GitBook
On this page
  • Multi-layer Perceptron(多層感知器)
  • MLP分類器

類神經網路 Neural_Networks

Previous波士頓房地產雲端評估(二)NextEx 1: Visualization of MLP weights on MNIST

Last updated 6 years ago

+ +MathJax.Hub.Queue(["Typeset",MathJax.Hub]); +

Multi-layer Perceptron(多層感知器)

Multi-layer Perceptron (MLP):MLP為一種監督式學習的演算法,藉由 f(⋅):R^m→R^o ,m是輸入時的維度、o是輸出時的維度,藉由輸入特徵 X=x1,x2,.....,xmX=x_1,x_2,.....,x_mX=x1​,x2​,.....,xm​ 和目標值Y,此算法將可以使用非線性近似將資料分類或進行迴歸運算。MLP可以在輸入層與輸出層中間插入許多非線性層,如圖1所示:,這是有一層隱藏層的網路。

圖1:包含一層隱藏層的MLP

intercepts為模型訓練後,權重矩陣內包含兩個屬性:$coefs\$和$intercepts_$。coefs_ 此矩陣中第i個指標表示第$i$層與$i+1$層的權重,intercepts_為偏權值(bias)矩陣,此矩陣中第i個指標表示要加在$i+1層$的偏權值。

MLP分類器

使用MLP訓練需要使用輸入兩種陣列,一個是特徵X陣列,X陣列包含(樣本數,特徵數),另一個是Y向量包含目標值(分類標籤)下面將會介紹MLP分類器範例。

(一)引入函式庫

from sklearn.neural_network import MLPClassifier:引進MLP分類器

(二)建立模擬資料與設定分類器參數

建立擁有三種特徵的三筆資料 X = [[0., 0.,0.], [1., 1.,1.],[2., 2.,2.]] 將三筆資料的分類標上 y = [0, 1, 2] 設定分類器:最佳化參數的演算法,alpha值,隱藏層的層數與每層神經元數: hidden_layer_sizes=(5,3)表示隱藏層有兩層第一層為五個神經元,第二層為三個神經元 clf = MLPClassifier(solver='lbfgs', alpha=1e-5, hidden_layer_sizes=(5,3), random_state=1)

(三)訓練網路參數與預測

將資料丟進分類器,訓練網路參數 clf.fit(X, y) 將要預測的資料丟進網路預測 clf.predict([[2., 2., 2.], [-1., -2.,0.],[1., 1.,0.]]) 預測結果:array([2, 0, 1]) 結果表示[2., 2., 2.]為第三類,[-1., -2.,0.]為第一類,[1., 1.,0.]為第二類

完整程式碼:

from sklearn.neural_network import MLPClassifier

X = [[0., 0.,0.], [1., 1.,1.],[2., 2.,2.]]

y = [0, 1, 2]

clf = MLPClassifier(solver='lbfgs', alpha=1e-5,
                     hidden_layer_sizes=(5,3), random_state=1)

clf.fit(X, y)    

clf.predict([[2., 2., 2.], [-1., -2.,0.],[1., 1.,0.]])

最左邊那層稱作輸入層,為一個神經元集合 {x_i|x_1,x_2,...,x_m}代表輸入的特徵。每個神經元在隱藏層會根據前一層的輸出的結果,做為此層的輸入w1x1+w2x2+...+wmxmw_1x_1+w_2x_2+...+w_mx_mw1​x1​+w2​x2​+...+wm​xm​在將總和使用非線性的活化函數做 f(⋅):R→R轉換,例如:、,最右邊那層為輸出層,會接收最後的隱藏層的輸出在轉換一次成輸出值。

MLP優點: 1.有能力建立非線性的模型 2.可以使用$partial_fit$建立real-time模型 MLP缺點: 1.因為擁有大於一個區域最小值,使用不同的初始權重,會讓驗證時的準確率浮動 2.MLP模型需要調整每層神經元數、層數、疊代次數 3.MLP對於特徵的預先處理很敏感,建議將特徵X都尺度降至[0,1]或[-1,+1]或讓特徵值降至平均值等於0與變異數等於1的數字區間

凹函數
hyperbolic tan function
Sigmoid function
http://scikit-learn.org/stable/modules/neural_networks_supervised.html