Я просматриваю автоградную часть учебников по pytorch. У меня есть два вопроса:
- Зачем нам нужно клонировать
grad_output
и назначать егоgrad_input
, кроме простого назначения во время обратного распространения? - Какова цель
grad_input[input < 0] = 0
? Означает ли это, что мы не обновляем градиент, когда ввод меньше нуля?
Вот код:
class MyReLU(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
"""
In the forward pass we receive a Tensor containing the input and return
a Tensor containing the output. ctx is a context object that can be used
to stash information for backward computation. You can cache arbitrary
objects for use in the backward pass using the ctx.save_for_backward method.
"""
ctx.save_for_backward(input)
return input.clamp(min=0)
@staticmethod
def backward(ctx, grad_output):
"""
In the backward pass we receive a Tensor containing the gradient of the loss
with respect to the output, and we need to compute the gradient of the loss
with respect to the input.
"""
input, = ctx.saved_tensors
grad_input = grad_output.clone()
grad_input[input < 0] = 0
return grad_input
Ссылка здесь: https://pytorch.org/tutorials/beginner/pytorch_with_examples.html#pytorch-defining-new-autograd-functions
Заранее большое спасибо.