使用Python实现简反向传播算法,能够推导相关公式
时间: 2024-12-06 22:20:29 浏览: 16
在Python中实现简单的反向传播算法通常用于神经网络的学习过程,特别是对于梯度下降优化。反向传播的核心是计算损失函数关于每个权重的偏导数,即误差的梯度,然后更新权重来最小化损失。
这里是一个简化版的反向传播算法示例,假设我们有一个两层的前馈神经网络,有输入层、隐藏层和输出层:
```python
import numpy as np
# 假设输入数据 x, 预期输出 y, 权重 w1 和 w2
x = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([[0], [1], [1], [0]])
# 初始化随机权重
w1 = np.random.randn(2, 3)
w2 = np.random.randn(3, 1)
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def forward_propagation(x, w1, w2):
z1 = x @ w1
a1 = sigmoid(z1) # 隐藏层激活值
z2 = a1 @ w2
a2 = sigmoid(z2) # 输出层激活值
return a1, a2
def loss_function(a2, y):
m = len(y)
return -np.sum(np.log(a2[np.arange(m), y]))
def backward_propagation(x, a1, a2, y, w1, w2, learning_rate):
dz2 = a2 - y # 损失函数对a2的导数
dw2 = a1.T @ dz2 / x.shape[0] # 按照梯度下降更新w2
da1 = dz2 @ w2.T * sigmoid_derivative(a1) # 激活函数的导数
dz1 = da1 * sigmoid_derivative(z1) # 向前传播的导数
dw1 = x.T @ dz1 / x.shape[0] # 更新w1
return dw1, dw2
def update_weights(w1, w2, learning_rate, dw1, dw2):
w1 -= learning_rate * dw1
w2 -= learning_rate * dw2
return w1, w2
learning_rate = 0.1
epochs = 10000
for epoch in range(epochs):
a1, a2 = forward_propagation(x, w1, w2)
loss = loss_function(a2, y)
dw1, dw2 = backward_propagation(x, a1, a2, y, w1, w2, learning_rate)
w1, w2 = update_weights(w1, w2, learning_rate, dw1, dw2)
if epoch % 1000 == 0:
print(f"Epoch {epoch}, Loss: {loss}")
sigmoid_derivative = lambda x: x * (1 - x) # sigmoid函数的导数
阅读全文