# 神经网络基本组成

## MLP (Multi-Layer Perceptron) 多层感知器

### 单层MLP

MLP为多层感知器，我们首先来看只有一个隐含层的MLP，如下图。

### sklearn中使用MLP

ann_model = MLPClassifier(hidden_layer_sizes=[unit,], activation=’logistic’, solver=’lbfgs’, random_state=0)

ann_model.fit(X_train, y_train)

• hidden_layer_sizes： 隐藏层个数，为一个列表，列表的长度为隐藏层的个数，列表中第i个位置上的数为第i个隐藏层的神经元个数
• activation: 为激活函数，relu, logistic, tanh

①ANN建模

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.linear_model import LogisticRegression

# 加载数据集

X = fruits_df[['width', 'height']]
y = fruits_df['fruit_label'].copy()

# 将不是apple的标签设为0
y[y != 1] = 0
# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=1/4, random_state=0)

②单层ANN(MLP)

from sklearn.neural_network import MLPClassifier
from ml_visualization import plot_class_regions_for_classifier

# 神经元个数
units = [1, 10, 100]
for unit in units:
# 激活函数：relu, logistic, tanh
#初始化模型
ann_model = MLPClassifier(hidden_layer_sizes=[unit], activation='logistic', solver='lbfgs', random_state=0)
#训练模型
ann_model.fit(X_train, y_train)
print('神经元个数={}，准确率：{:.3f}'.format(unit, ann_model.score(X_test, y_test)))
#用自己写的函数画出下图
plot_class_regions_for_classifier(ann_model, X_test.values, y_test.values, title='Unit={}'.format(unit))

③ 多层ANN

ann_model = MLPClassifier(hidden_layer_sizes=[10, 10], activation='relu', solver='lbfgs', random_state=0)
ann_model.fit(X_train, y_train)
print('准确率：{:.3f}'.format(ann_model.score(X_test, y_test)))
plot_class_regions_for_classifier(ann_model, X_test.values, y_test.values)


## ANN过拟合

sklearn中 alpha为正则化强度

# alpha
aplhas = [0.001, 0.01, 0.1, 1.0]
for alpha in aplhas:
ann_model = MLPClassifier(hidden_layer_sizes=[100, 100], activation='tanh', solver='lbfgs', random_state=0,
alpha=alpha)
ann_model.fit(X_train, y_train)
print('alpha={}，准确率：{:.3f}'.format(alpha, ann_model.score(X_test, y_test)))
plot_class_regions_for_classifier(ann_model, X_test.values, y_test.values, title='Alpha={}'.format(alpha))