I’m writing a custom convolution autograd function and I want to compare the results of backpropagation to the official convolution implementation. How can I call backward for torch.nn.functional.conv2d and store the output?
For example, I’d like to compare the weight gradient, the input gradient, and the bias gradient computed by conv2d backward to my computation of the same values.
What I’ve tried
import torch

# Toy inputs, targets, and weights
input = torch.randn((5, 3, 7, 15))
weight = torch.randn((2,3,3,3), requires_grad=True)
target = torch.randint(0, 10, (5,))

# These two layers are so that I can have a loss gradient for backpropagation
linear = torch.nn.Linear(2*5*13, 10) #Get things into the right shape for the loss function
loss = torch.nn.CrossEntropyLoss()

# Forwards pass
x_conv = torch.nn.functional.conv2d(input, weight)  # This is the convolution that I want to know the gradients of 
x_linear = linear(x_conv.view(-1, 2*5*13)) # The following two layers are simply for getting a loss 
x_loss = loss(x_linear, target)

#Backwards
x_grad = x_loss.backward() # I thought that this would call the backward method of loss, linear, and coonv2d and return the input, weight, and bias gradients, but x_grad is empty