使用 .clamp 而不是 torch.relu 时,Pytorch Autograd 会给出不同的渐变

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

我仍在努力理解 PyTorch autograd 系统。我正在努力解决的一件事是理解为什么

.clamp(min=0)
nn.functional.relu()
似乎有不同的向后传递。

这尤其令人困惑,因为

.clamp
相当于 PyTorch 教程中的
relu
,例如 https://pytorch.org/tutorials/beginner/pytorch_with_examples.html#pytorch-nn

我在分析一个简单的全连接网络的梯度时发现了这一点,该网络具有一个隐藏层和一个 relu 激活(输出层中是线性的)。

据我了解,以下代码的输出应该为零。我希望有人能告诉我我缺少什么。

import torch
dtype = torch.float

x = torch.tensor([[3,2,1],
                  [1,0,2],
                  [4,1,2],
                  [0,0,1]], dtype=dtype)

y = torch.ones(4,4)

w1_a = torch.tensor([[1,2],
                     [0,1],
                     [4,0]], dtype=dtype, requires_grad=True)
w1_b = w1_a.clone().detach()
w1_b.requires_grad = True



w2_a = torch.tensor([[-1, 1],
                     [-2, 3]], dtype=dtype, requires_grad=True)
w2_b = w2_a.clone().detach()
w2_b.requires_grad = True


y_hat_a = torch.nn.functional.relu(x.mm(w1_a)).mm(w2_a)
y_a = torch.ones_like(y_hat_a)
y_hat_b = x.mm(w1_b).clamp(min=0).mm(w2_b)
y_b = torch.ones_like(y_hat_b)

loss_a = (y_hat_a - y_a).pow(2).sum()
loss_b = (y_hat_b - y_b).pow(2).sum()

loss_a.backward()
loss_b.backward()

print(w1_a.grad - w1_b.grad)
print(w2_a.grad - w2_b.grad)

# OUT:
# tensor([[  0.,   0.],
#         [  0.,   0.],
#         [  0., -38.]])
# tensor([[0., 0.],
#         [0., 0.]])
# 
python pytorch backpropagation autograd relu
1个回答
7
投票

原因是

relu
clamp
0
处产生不同的梯度。对于标量张量
x = 0
:

  • (relu(x) - 1.0).pow(2).backward()
    给出
    x.grad == 0
  • (x.clamp(min=0) - 1.0).pow(2).backward()
    给出
    x.grad == -2

这表明:

  • relu
    选择
    x == 0 --> grad = 0
  • clamp
    选择
    x == 0 --> grad = 1
© www.soinside.com 2019 - 2024. All rights reserved.