PyTorch学习系列(六)——自动求导

各种深度学习框架大大简化了研究人员的工作量,这主要归功于它们的自动求导功能。如果不借助于这些深度学习框架,我们不仅需要实现网络的前向过程,而且还要推导网络在各层的梯度表达式以实现反向过程。这是非常费时的,而且极易出错。

PyTorch提供了包torch.autograd用于自动求导。在前向过程中,PyTorch会构建计算图,每个节点用Variable表示,边表示由输入节点到输出节点的函数(torch.autograd.Function对象)。Function对象不仅负责执行前向计算,在反向过程中,每个Function对象会调用.backward()函数计算输出对输入的梯度,然后将梯度传递给下一个Function对象。

torch.autograd.Variable

见博客 PyTorch学习系列(三)——Tensor 和 Variable 中关于Variable的介绍。

torch.autograd.Function

Function和Variable一样,是autograd的重要组成部分,每个在Variable上执行的操作都会生成一个Function对象。

属性(成员变量)

  • saved_tensors: 传给forward()的参数,在backward()中会用到。
  • needs_input_grad:长度为 :attr:num_inputs的bool元组,表示输出是否需要梯度。可以用于优化反向过程的缓存。
  • num_inputs: 传给函数 :func:forward的参数的数量。
  • num_outputs: 函数 :func:forward返回的值的数目。
  • requires_grad: 布尔值,表示函数 :func:backward 是否永远不会被调用。

成员函数

forward()

forward()可以有任意多个输入、任意多个输出,但是输入和输出必须是Variable。

backward()

backward()的输入和输出的个数就是forward()函数的输出和输入的个数。其中,backward()输入表示关于forward()输出的梯度,backward()的输出表示关于forward()的输入的梯度。在输入不需要梯度时(通过查看needs_input_grad参数)或者不可导时,可以返回None。

定义新操作

一般情况下我们并不会接触到Function。但是在以下情况下,我们需要定义自己的Function子类:

  • 在关于Variable对象的操作生成的子图比较大时,需要保存的状态以及计算量会增加,这时我们会希望把这些操作封装在一个Function对象中;
  • 需要定义新的操作时。

定义新的操作,意味着定义Function的子类,并且这些子类必须重写以下函数:::forward()和::backward()。初始化函数::__init__()根据实际需求判断是否需要重写。

示例:

# Inherit from Function
class Linear(Function):

    # bias is an optional argument
    def forward(self, input, weight, bias=None):
        self.save_for_backward(input, weight, bias)
        output = input.mm(weight.t())
        if bias is not None:
            output += bias.unsqueeze(0).expand_as(output)
        return output

    # This function has only a single output, so it gets only one gradient
    def backward(self, grad_output):
        # This is a pattern that is very convenient - at the top of backward
        # unpack saved_tensors and initialize all gradients w.r.t. inputs to
        # None. Thanks to the fact that additional trailing Nones are
        # ignored, the return statement is simple even when the function has
        # optional inputs.
        input, weight, bias = self.saved_tensors
        grad_input = grad_weight = grad_bias = None

        # These needs_input_grad checks are optional and there only to
        # improve efficiency. If you want to make your code simpler, you can
        # skip them. Returning gradients for inputs that don't require it is
        # not an error.
        if self.needs_input_grad[0]:
            grad_input = grad_output.mm(weight)
        if self.needs_input_grad[1]:
            grad_weight = grad_output.t().mm(input)
        if bias is not None and self.needs_input_grad[2]:
            grad_bias = grad_output.sum(0).squeeze(0)

        return grad_input, grad_weight, grad_bias
#建议把新操作封装在一个函数中
def linear(input, weight, bias=None):
    # First braces create a Function object. Any arguments given here
    # will be passed to __init__. Second braces will invoke the __call__
    # operator, that will then use forward() to compute the result and
    # return it.
    return Linear()(input, weight, bias)#调用forward()

#检查实现的backward()是否正确
from torch.autograd import gradcheck
# gradchek takes a tuple of tensor as input, check if your gradient
# evaluated with these tensors are close enough to numerical
# approximations and returns True if they all verify this condition.
input = (Variable(torch.randn(20,20).double(), requires_grad=True),)
test = gradcheck(Linear(), input, eps=1e-6, atol=1e-4)
print(test)

你可能感兴趣的:(深度学习框架Torch)