也许我不正确,但我有不同的看法。
后向函数被定义并被前向函数调用。
例如:
#!/usr/bin/env python
# encoding: utf-8
###############################################################
# Parametrized example
# --------------------
#
# This implements a layer with learnable weights.
#
# It implements the Cross-correlation with a learnable kernel.
#
# In deep learning literature, it’s confusingly referred to as
# Convolution.
#
# The backward computes the gradients wrt the input and gradients wrt the
# filter.
#
# **Implementation:**
#
# *Please Note that the implementation serves as an illustration, and we
# did not verify it’s correctness*
import torch
from torch.autograd import Function
from torch.autograd import Variable
from scipy.signal import convolve2d, correlate2d
from torch.nn.modules.module import Module
from torch.nn.parameter import Parameter
class ScipyConv2dFunction(Function):
@staticmethod
def forward(ctx, input, filter):
result = correlate2d(input.numpy(), filter.numpy(), mode='valid')
ctx.save_for_backward(input, filter)
return input.new(result)
@staticmethod
def backward(ctx, grad_output):
input, filter = ctx.saved_tensors
grad_output = grad_output.data
grad_input = convolve2d(grad_output.numpy(), filter.t().numpy(), mode='full')
grad_filter = convolve2d(input.numpy(), grad_output.numpy(), mode='valid')
return Variable(grad_output.new(grad_input)), \
Variable(grad_output.new(grad_filter))
class ScipyConv2d(Module):
def __init__(self, kh, kw):
super(ScipyConv2d, self).__init__()
self.filter = Parameter(torch.randn(kh, kw))
def forward(self, input):
return ScipyConv2dFunction.apply(input, self.filter)
###############################################################
# **Example usage:**
module = ScipyConv2d(3, 3)
print(list(module.parameters()))
input = Variable(torch.randn(10, 10), requires_grad=True)
output = module(input)
print(output)
output.backward(torch.randn(8, 8))
print(input.grad)
在本例中,后向函数由 ScipyConv2dFunction 函数定义。
ScipyConv2dFunction 被 forward 函数调用。
我说的对吗?