误差反向传播算法中的梯度是如何计算的

问题描述

误差反向传播算法是神经网络中常用的训练算法之一,它通过计算梯度来更新神经网络的权重。在该问题中,我们将详细解释误差反向传播算法中梯度的计算过程,并通过使用虚拟数据集展示完整的Python代码和解释代码细节。

算法原理

梯度是指某个函数在每个方向的变化率,计算梯度是为了找到函数的最小值或最大值。误差反向传播算法使用链式法则来计算神经网络各层的梯度,并将梯度反向传播到每一层的神经元。具体来说,算法包括以下几个步骤:

  1. 前向传播:将输入的特征数据通过神经网络的每一层,计算输出的预测值。
  2. 计算损失:通过比较预测值和实际值的差异,计算损失函数的值。
  3. 反向传播:使用链式法则,将损失函数的梯度从输出层一直传递到输入层,计算每个神经元的梯度。
  4. 更新权重:根据梯度和学习率,更新每个连接权重的值。

公式推导

设神经网络输出为 $\hat{y}$,实际值为 $y$,损失函数为 $L(\hat{y}, y)$。我们以二分类问题为例,使用交叉熵损失函数,公式如下:

$$L(\hat{y}, y) = – y \cdot \log(\hat{y}) – (1-y) \cdot \log(1-\hat{y})$$

为了方便计算,我们使用均方误差损失函数的导数,公式如下:

$$\frac{\partial L}{\partial \hat{y}} = \hat{y} – y$$

计算步骤

下面我们来具体说明计算梯度的步骤。

  1. 前向传播:
  2. 将输入特征数据 $X$ 做为神经网络的输入。
  3. 对于每一层,计算该层的加权输入结果 $z$ 和激活函数的输出结果 $a$。具体计算公式如下:
  4. $z^{[l]} = W^{[l]} \cdot a^{[l-1]} + b^{[l]}$
  5. $a^{[l]} = g(z^{[l]})$,其中 $g$ 表示激活函数。
  6. 最后一层的 $a$ 即为神经网络的预测结果 $\hat{y}$。

  7. 计算损失:

  8. 根据预测结果 $\hat{y}$ 和实际值 $y$,计算损失函数的值。

  9. 反向传播:

  10. 计算最后一层的梯度,根据公式 $\frac{\partial L}{\partial \hat{y}}$ 计算:
  11. $da^{[L]} = \frac{\partial L}{\partial \hat{y}}$
  12. 对于每一层,从输出层开始往前计算梯度。
  13. 对于第 $l$ 层,根据下面的公式计算 $dz$ 和 $da$ 的值:
  14. $dz^{[l]} = da^{[l]} \cdot g'(z^{[l]})$,其中 $g’$ 表示激活函数的导数。
  15. $da^{[l-1]} = W^{[l]T} \cdot dz^{[l]}$

  16. 更新权重:

  17. 根据计算得到的梯度和学习率 $\alpha$,使用下面的公式更新权重和偏置:
  18. $W^{[l]} = W^{[l]} – \alpha \cdot dW^{[l]}$
  19. $b^{[l]} = b^{[l]} – \alpha \cdot db^{[l]}$

Python代码示例

下面是一个使用虚拟数据集的示例,展示了如何实现误差反向传播算法,并说明了代码的细节。

import numpy as np

# 生成虚拟数据集
np.random.seed(0)
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 1, 1, 0])

# 定义激活函数和其导数
def sigmoid(x):
 return 1 / (1 + np.exp(-x))

def sigmoid_derivative(x):
 return sigmoid(x) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls (1 - sigmoid(x))

# 初始化权重和偏置
W = [np.random.randn(2, 4), np.random.randn(4, 1)]
b = [np.zeros((1, 4)), np.zeros((1, 1))]

# 定义学习率
learning_rate = 0.1

# 开始训练模型
for epoch in range(1000):
 # 前向传播
 z = [None] artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 2
 a = [X] + [None] artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 2

 for l in range(2):
 z[l] = np.dot(a[l], W[l]) + b[l]
 a[l+1] = sigmoid(z[l])

 # 计算损失和梯度
 loss = - y artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls np.log(a[-1]) - (1 - y) artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls np.log(1 - a[-1])
 da = - (y / a[-1]) + (1 - y) / (1 - a[-1])

 dz = [None] artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 2
 dW = [None] artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 2
 db = [None] artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls 2

 for l in range(1, -1, -1):
 dz[l] = da artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls sigmoid_derivative(z[l])
 dW[l] = np.dot(a[l].T, dz[l])
 db[l] = np.sum(dz[l], axis=0, keepdims=True)
 da = np.dot(dz[l], W[l].T)

 # 更新权重和偏置
 for l in range(2):
 W[l] -= learning_rate artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls dW[l]
 b[l] -= learning_rate artical cgpt2md_gpt.sh cgpt2md_johngo.log cgpt2md_johngo.sh cgpt2md.sh _content1.txt _content.txt current_url.txt history_url history_urls log nohup.out online pic.txt seo test.py topic_gpt.txt topic_johngo.txt topic.txt upload-markdown-to-wordpress.py urls db[l]

# 打印最终的预测结果
print(a[-1])

代码细节解释

  • 代码首先导入需要的库,并定义了激活函数和激活函数的导数。
  • 接着,生成了一个虚拟数据集 X 和对应的标签 y。
  • 初始化了神经网络的权重和偏置 W 和 b。
  • 设置了学习率 learning_rate。
  • 在模型训练的主循环中,进行了前向传播、计算损失和梯度、反向传播和权重更新等步骤。
  • 最后打印出了最终的预测结果。

在代码中,使用了 NumPy 库来进行矩阵运算,以便更高效地实现神经网络的计算。代码中的变量命名和注释都比较清晰,可以帮助理解每个步骤的作用和计算过程。

通过运行这段代码,我们可以看到模型最终的预测结果,这里演示的是一个简单的逻辑回归问题,可以很容易地验证结果的正确性。

希望以上内容对你理解误差反向传播算法中梯度的计算过程有所帮助!

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/823871/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球