导入相关包 1
2
3
4
5
6
7
8
9
10
11
import matplotlib.pyplot as plt
import numpy as np
from numpy import arange
import sklearn
import sklearn.datasets
import sklearn.linear_model
import matplotlib
matplotlib.rcParams['figure.figsize' ] = (10.0 , 8.0 )
生成数据集 1
2
3
4
5
np.random.seed(0 )
X, y = sklearn.datasets.make_moons(200 , noise=0.20 )
plt.scatter(X[:,0 ], X[:,1 ], s=40 , c=y, cmap=plt.cm.Spectral)
绘制决策边界 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def plot_decision_boundary (pred_func) :
x_min, x_max = X[:, 0 ].min() - .5 , X[:, 0 ].max() + .5
y_min, y_max = X[:, 1 ].min() - .5 , X[:, 1 ].max() + .5
h = 0.01
xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
Z = pred_func(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral)
plt.scatter(X[:, 0 ], X[:, 1 ], c=y, cmap=plt.cm.Spectral)
logistic rgeression classifier 无法解决问题 模型训练1
2
3
clf = sklearn.linear_model.LogisticRegressionCV()
clf.fit(X, y)
查看结果1
2
3
4
plot_decision_boundary(lambda x: clf.predict(x))
plt.title("Logistic Regression" )
plt.show()
神经网络的初始化参数 1
2
3
4
5
6
7
num_examples = len(X)
nn_input_dim = 2
nn_output_dim = 2
epsilon = 0.01
reg_lambda = 0.01
计算损失函数 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def calculate_loss (model) :
W1, b1, W2, b2 = model['W1' ], model['b1' ], model['W2' ], model['b2' ]
z1 = X.dot(W1) + b1
a1 = np.tanh(z1)
z2 = a1.dot(W2) + b2
exp_scores = np.exp(z2)
probs = exp_scores / np.sum(exp_scores, axis=1 , keepdims=True )
corect_logprobs = -np.log(probs[range(num_examples), y])
data_loss = np.sum(corect_logprobs)
data_loss += reg_lambda/2 * (np.sum(np.square(W1)) + np.sum(np.square(W2)))
return 1. /num_examples * data_loss
预测 1
2
3
4
5
6
7
8
9
10
def predict (model, x) :
W1, b1, W2, b2 = model['W1' ], model['b1' ], model['W2' ], model['b2' ]
z1 = x.dot(W1) + b1
a1 = np.tanh(z1)
z2 = a1.dot(W2) + b2
exp_scores = np.exp(z2)
probs = exp_scores / np.sum(exp_scores, axis=1 , keepdims=True )
return np.argmax(probs, axis=1 )
模型训练 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def build_model (nn_hdim, num_passes=20000 , print_loss=False) :
np.random.seed(0 )
W1 = np.random.randn(nn_input_dim, nn_hdim) / np.sqrt(nn_input_dim)
b1 = np.zeros((1 , nn_hdim))
W2 = np.random.randn(nn_hdim, nn_output_dim) / np.sqrt(nn_hdim)
b2 = np.zeros((1 , nn_output_dim))
model = {}
for i in xrange(0 , num_passes):
z1 = X.dot(W1) + b1
a1 = np.tanh(z1)
z2 = a1.dot(W2) + b2
exp_scores = np.exp(z2)
probs = exp_scores / np.sum(exp_scores, axis=1 , keepdims=True )
delta3 = probs
delta3[range(num_examples), y] -= 1
dW2 = (a1.T).dot(delta3)
db2 = np.sum(delta3, axis=0 , keepdims=True )
delta2 = delta3.dot(W2.T) * (1 - np.power(a1, 2 ))
dW1 = np.dot(X.T, delta2)
db1 = np.sum(delta2, axis=0 )
dW2 += reg_lambda * W2
dW1 += reg_lambda * W1
W1 += -epsilon * dW1
b1 += -epsilon * db1
W2 += -epsilon * dW2
b2 += -epsilon * db2
model = { 'W1' : W1, 'b1' : b1, 'W2' : W2, 'b2' : b2}
if print_loss and i % 1000 == 0 :
print "Loss after iteration %i: %f" %(i, calculate_loss(model))
return model
模型实例 1
2
3
4
5
6
7
model = build_model(3 , print_loss=True )
plot_decision_boundary(lambda x: predict(model, x))
plt.title("Decision Boundary for hidden layer size 3" )
plt.show()
不同的隐含层规模 1
2
3
4
5
6
7
8
plt.figure(figsize=(16 , 32 ))
hidden_layer_dimensions = [1 , 2 , 3 , 4 , 5 , 20 , 50 ]
for i, nn_hdim in enumerate(hidden_layer_dimensions):
plt.subplot(5 , 2 , i+1 )
plt.title('Hidden Layer size %d' % nn_hdim)
model = build_model(nn_hdim)
plot_decision_boundary(lambda x: predict(model, x))
plt.show()
其他值得关注的点 查看tanh函数 1
2
3
4
5
6
7
X =arange(-10,10,0.05)
print X.shape
y =np.tanh(X)
plt.scatter(X, y, c ='r' )
plt.show()
softmax函数 http://blog.csdn.net/kevinew/article/details/9407367
关于隐含层的规模
But higher dimensionality comes at a cost. First, more computation is required to make predictions and learn the network parameters.A bigger number of parameters also means we become more prone to overfitting our data.
过大的隐含层规模有两个缺点,首先是建完的模型有更多的参数需要运算;另外隐含层过大非常容易导致数据过拟合。
关于合适的隐含层规模(hidden layer size),《Matlab在数学建模中的应用》有一小节简单做了讨论,提供了两个经验公式,当然这里的结论只是仅供参考而已。
$$ l=\sqrt{m+n}+a $$
$$ l=\sqrt{0.43mn+0.12n^{2}+2.54m+0.77n+0.35+0.51} $$
其中m,n分别为输入层规模与输出层规模,a为1~10之间的常数,l为需要的隐含层规模。
关于反向传播的细节
I won’t go into detail how backpropagation works, but there are many excellent explanations (here or here) floating around the web.
http://colah.github.io/posts/2015-08-Backprop/
http://cs231n.github.io/optimization-2/
变学习速率
So if you are serious you’ll want to use one of these, and ideally you would also decay the learning rate over time.
http://cs231n.github.io/neural-networks-3/#anneal
参考资料 [1] 从头开始实现神经网络:入门 [2] Implementing a Neural Network from Scratch in Python – An Introduction [3] nn-from-scratch (github) [4] sklearn的多层感知器