神经网络反向传播神经网络反向传播求解w代码验证求解w,用python代码验证
时间: 2024-09-10 15:07:25 浏览: 41
神经网络的反向传播算法是一种用于训练深层模型的关键技术,它通过计算梯度来更新权重(w),使得损失函数逐渐减小。这个过程可以分为前向传播(预测)和反向传播(误差反传)。这里以简单的线性回归为例,展示如何用Python和NumPy实现一个一隐藏层的神经网络,并包含反向传播的部分:
```python
import numpy as np
# 假设我们有输入数据 X (形状(m, n)) 和对应的标签 y (形状(m, 1))
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def forward(X, w1, b1, w2, b2):
# 前向传播
z1 = np.dot(X, w1) + b1
a1 = sigmoid(z1)
z2 = np.dot(a1, w2) + b2
y_pred = sigmoid(z2)
return y_pred, a1, z2
def cost_function(y_true, y_pred):
m = len(y_true)
J = -(1/m) * np.sum(y_true * np.log(y_pred) + (1 - y_true) * np.log(1 - y_pred))
return J
# 假设已经初始化了权重 w1、b1、w2、b2
# 每次迭代我们要做的就是:
def backward(X, y_true, y_pred, a1, z2, hessian=False):
m = X.shape[0]
dz2 = y_pred - y_true
dw2 = (1/m) * np.dot(a1.T, dz2)
db2 = (1/m) * np.sum(dz2, axis=0, keepdims=True)
if hessian:
da1 = np.dot(dz2, w2)
da1[a1 <= 0.5] *= -1
dz1 = np.dot(da1, w2.T)
dw1 = (1/m) * np.dot(X.T, dz1)
db1 = (1/m) * np.sum(dz1, axis=0, keepdims=True)
return dw1, db1, dw2, db2
else:
dz1 = np.dot(da1, dw2)
dz1[a1 <= 0.5] *= -1
dw1 = (1/m) * np.dot(X.T, dz1)
db1 = (1/m) * np.sum(dz1, axis=0, keepdims=True)
return dw1, db1
# 训练循环
learning_rate = 0.01
epochs = 1000
for epoch in range(epochs):
y_pred, a1, z2 = forward(X, w1, b1, w2, b2)
dw1, db1, dw2, db2 = backward(X, y_true, y_pred, a1, z2)
# 更新权重
w1 -= learning_rate * dw1
b1 -= learning_rate * db1
w2 -= learning_rate * dw2
b2 -= learning_rate * db2
# 打印一些进度信息
if epoch % 100 == 0:
print(f"Epoch {epoch+1}/{epochs}, Loss: {cost_function(y_true, y_pred)}")
阅读全文