正向模式与反向模式的区别-Pytorch

问题描述 投票:0回答:1

Learning PyTorch with Examples的第一个示例中,作者演示了如何使用numpy创建神经网络。为了方便起见,将其代码粘贴在下面:

# from: https://pytorch.org/tutorials/beginner/pytorch_with_examples.html
# -*- coding: utf-8 -*-
import numpy as np

# N is batch size; D_in is input dimension;
# H is hidden dimension; D_out is output dimension.
N, D_in, H, D_out = 64, 1000, 100, 10

# Create random input and output data
x = np.random.randn(N, D_in)
y = np.random.randn(N, D_out)

# Randomly initialize weights
w1 = np.random.randn(D_in, H)
w2 = np.random.randn(H, D_out)

learning_rate = 1e-6
for t in range(500):
    # Forward pass: compute predicted y
    h = x.dot(w1)
    h_relu = np.maximum(h, 0)
    y_pred = h_relu.dot(w2)

    # Compute and print loss
    loss = np.square(y_pred - y).sum()
    print(t, loss)

    # Backprop to compute gradients of w1 and w2 with respect to loss
    grad_y_pred = 2.0 * (y_pred - y)
    grad_w2 = h_relu.T.dot(grad_y_pred)
    grad_h_relu = grad_y_pred.dot(w2.T)
    grad_h = grad_h_relu.copy()
    grad_h[h < 0] = 0
    grad_w1 = x.T.dot(grad_h)

    # Update weights
    w1 -= learning_rate * grad_w1
    w2 -= learning_rate * grad_w2

让我感到困惑的是为什么计算w1和w2的梯度关于损耗(第二到最后一个代码块)。

通常发生相反的计算:相对于权重计算损耗的梯度,如此处引用:

  • “训练神经网络时,我们将成本(描述神经网络的不良表现的值)作为参数(描述网络行为的数字)的函数。我们要计算成本的导数关于所有参数,用于梯度下降。现在,神经网络中通常有数百万甚至数千万个参数,因此,反向模式微分在神经网络中称为反向传播我们大大加快了速度!” (Colah's blog)。

所以我的问题是:为什么上面的示例中的推导计算与正常的反向传播计算相比是相反的顺序?

numpy neural-network deep-learning pytorch backpropagation
1个回答
0
投票

似乎是评论中的错字。他们实际上是在计算loss w.r.t. w2

让我们快速得出loss w.r.t. w2只是为了确定。通过检查您的代码,我们有

enter image description here

使用微积分的链式规则

enter image description here

每个项可以用矩阵演算的标准规则表示,结果是

enter image description here

enter image description here

将这些术语放回初始等式中,我们得到了表达式

enter image description here

完全匹配表达式

grad_y_pred = 2.0 * (y_pred - y)
grad_w2 = h_relu.T.dot(grad_y_pred)

在您提供的反向传播代码中使用。

© www.soinside.com 2019 - 2024. All rights reserved.