一、向前传播函数
forward_propagate
输入:X theta
根据上图公式进行计算
输出:a1、z2、a2、z3、h(a3)
# 前向传播函数
def forward_propagate(X, theta1, theta2):
m = X.shape[0]
# X:5000×400
a1 = np.insert(X, 0, values=np.ones(m), axis=1)# a1:5000×401
z2 = a1 * theta1.T
a2 = np.insert(sigmoid(z2), 0, values=np.ones(m), axis=1)# a2:5000×26
z3 = a2 * theta2.T
h = sigmoid(z3)
return a1, z2, a2, z3, h
def sigmoid(z):
return 1 / (1 + np.exp(-z))
二、正则化代价函数
costReg
输入:X y theta learning_rate
遍历m个样本的K个输出维度求和,正则化项中对theta(除第一列)的平方求和。
输出:J
def costReg(theta1, theta2, input_size, hidden_size, num_labels, X, y, learning_rate):
m = X.shape[0]
X = np.matrix(X)
y = np.matrix(y)
# run the feed-forward pass
a1, z2, a2, z3, h = forward_propagate(X, theta1, theta2)
# compute the cost
J = 0
for i in range(m):
first_term = np.multiply(-y[i,:], np.log(h[i,:]))
second_term = np.multiply((1 - y[i,:]), np.log(1 - h[i,:]))
J += np.sum(first_term - second_term)
J = J / m
# add the cost regularization term
J += (float(learning_rate) / (2 * m)) * (np.sum(np.power(theta1[:,1:], 2)) + np.sum(np.power(theta2[:,1:], 2)))
return J
三、正则化反向传播函数
反向传播的步骤是,给定训练集
(
x
(
t
,
y
(
t
)
)
(x^{(t},y^{(t)})
(x(t,y(t)),先计算正向传播
h
Θ
(
x
)
h_\Theta(x)
hΘ(x),再对于
l
l
l层的每个节点
j
j
j,计算误差项
δ
j
(
l
)
\delta_j^{(l)}
δj(l),这个数据衡量这个节点对最后输出的误差“贡献”了多少。
对于每个输出节点,我们可以直接计算输出值与目标值的差值,定义为
δ
j
(
3
)
\delta_j^{(3)}
δj(3)。对于每个隐藏节点,需要基于现有权重及
(
l
+
1
)
(l+1)
(l+1)层的误差,计算
δ
j
(
l
)
\delta_j^{(l)}
δj(l)
有点难,细节理解还不到位,先放代码
def backpropReg(params, input_size, hidden_size, num_labels, X, y, learning_rate):
m = X.shape[0]
X = np.matrix(X)
y = np.matrix(y)
# reshape the parameter array into parameter matrices for each layer
theta1 = np.matrix(np.reshape(params[:hidden_size * (input_size + 1)], (hidden_size, (input_size + 1))))
theta2 = np.matrix(np.reshape(params[hidden_size * (input_size + 1):], (num_labels, (hidden_size + 1))))
# run the feed-forward pass
a1, z2, a2, z3, h = forward_propagate(X, theta1, theta2)
# initializations
J = 0
delta1 = np.zeros(theta1.shape) # (25, 401)
delta2 = np.zeros(theta2.shape) # (10, 26)
# compute the cost
for i in range(m):
first_term = np.multiply(-y[i,:], np.log(h[i,:]))
second_term = np.multiply((1 - y[i,:]), np.log(1 - h[i,:]))
J += np.sum(first_term - second_term)
J = J / m
# add the cost regularization term
J += (float(learning_rate) / (2 * m)) * (np.sum(np.power(theta1[:,1:], 2)) + np.sum(np.power(theta2[:,1:], 2)))
# perform backpropagation
for t in range(m):
a1t = a1[t,:] # (1, 401)
z2t = z2[t,:] # (1, 25)
a2t = a2[t,:] # (1, 26)
ht = h[t,:] # (1, 10)
yt = y[t,:] # (1, 10)
d3t = ht - yt # (1, 10)
z2t = np.insert(z2t, 0, values=np.ones(1)) # (1, 26)
d2t = np.multiply((theta2.T * d3t.T).T, sigmoid_gradient(z2t)) # (1, 26)
delta1 = delta1 + (d2t[:,1:]).T * a1t
delta2 = delta2 + d3t.T * a2t
delta1 = delta1 / m
delta2 = delta2 / m
# add the gradient regularization term
delta1[:,1:] = delta1[:,1:] + (theta1[:,1:] * learning_rate) / m
delta2[:,1:] = delta2[:,1:] + (theta2[:,1:] * learning_rate) / m
# unravel the gradient matrices into a single array
grad = np.concatenate((np.ravel(delta1), np.ravel(delta2)))
return J, grad
四、数据处理和初始化
需要初始化的参数有:
数据处理后分为X y(其中如果是多元分类则改变y的编码方式,从列向量转化为矩阵)
权重theta随机初始化为接近0的值
输入层单元input_size = 400
隐藏层单元hidden_size = 25
输出单元num_labels = 10
正则化项的学习率learning_rate=1
五、利用Python工具库求解
- 利用fmin得出theta
from scipy.optimize import minimize
# minimize the objective function
fmin = minimize(fun=backpropReg, x0=(params), args=(input_size, hidden_size, num_labels, X, y_onehot, learning_rate),
method='TNC', jac=True, options={'maxiter': 250})
thetafinal1 = np.matrix(np.reshape(fmin.x[:hidden_size * (input_size + 1)], (hidden_size, (input_size + 1))))
thetafinal2 = np.matrix(np.reshape(fmin.x[hidden_size * (input_size + 1):], (num_labels, (hidden_size + 1))))
- 预测时使用得出的theta,调用前向传播函数forward_propagate进行预测
- 评价预测结果:
# 预测值与实际值比较
from sklearn.metrics import classification_report#这个包是评价报告
print(classification_report(y, y_pred))