# 机器学习系列：（十）从感知器到人工神经网络

### 非线性决策边界

A

B

A AND B

A NAND B

A OR B

A XOR B

0

0

0

1

0

0

0

1

0

1

1

1

1

0

0

1

1

1

1

1

1

0

1

0

A

B

A OR B

A NAND B

(A OR B) AND (A NAND B)

0

0

0

1

0

0

1

1

1

1

1

0

1

1

1

1

1

1

0

0

f(x)=tanh(x)

f(x)=11+e−x

### 用多层感知器近似XOR函数

```# __init__.pyfrom .rbm import BernoulliRBMfrom .multilayer_perceptron import MultilayerPerceptronClassifierfrom .multilayer_perceptron import MultilayerPerceptronRegressor__all__ = ["BernoulliRBM",
"MultilayerPerceptronClassifier",
"MultilayerPerceptronRegressor"]```

In [1]:

`from sklearn.cross_validation import train_test_splitfrom sklearn.neural_network import MultilayerPerceptronClassifierimport numpy as npy = [0, 1, 1, 0] * 1000X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]] * 1000)X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=3)`

In [3]:

```clf = MultilayerPerceptronClassifier(hidden_layer_sizes=[2],
activation='logistic',
algorithm='sgd',
random_state=3)clf.fit(X_train, y_train)```

Out[3]:

```MultilayerPerceptronClassifier(activation='logistic', algorithm='sgd',
alpha=1e-05, batch_size=200, hidden_layer_sizes=[2],
learning_rate='constant', learning_rate_init=0.5,
max_iter=200, power_t=0.5, random_state=3, shuffle=False,
tol=1e-05, verbose=False, warm_start=False)```

In [5]:

```print('层数：%s，输出单元数量：%s' % (clf.n_layers_, clf.n_outputs_))predictions = clf.predict(X_test)print('准确率：%s' % clf.score(X_test, y_test))for i, p in enumerate(predictions[:10]):
print('真实值：%s，预测值：%s' % (y_test[i], p))```
```层数：3，输出单元数量：1

### 手写数字识别

In [6]:

`from sklearn.datasets import load_digitsfrom sklearn.cross_validation import train_test_split, cross_val_scorefrom sklearn.pipeline import Pipelinefrom sklearn.preprocessing import StandardScalerfrom sklearn.neural_network.multilayer_perceptron import MultilayerPerceptronClassifier`

In [7]:

```if __name__ == '__main__':
X = digits.data
y = digits.target```

In [8]:

```pipeline = Pipeline([
('ss', StandardScaler()),
('mlp', MultilayerPerceptronClassifier(hidden_layer_sizes=[150, 100], alpha=0.1))
])print('准确率： %s' % cross_val_score(pipeline, X, y, n_jobs=-1))```
`准确率： [ 0.8654485   0.87646077  0.87248322]`

2122 篇文章98 人订阅

0 条评论

## 相关文章

4056

2275

### 二值人脸对齐--Binarized Convolutional Landmark Localizers for Human Pose Estimation and Face Alignment

Binarized Convolutional Landmark Localizers for Human Pose Estimation and Face A...

2945

3555

3776

1482

1093

2560

873

4066