There are a few issues in the code:

You shouldn’t manipulate the .data attribute, as Autograd cannot track these changes, which might result in wrong results. If you want to manipulate the data without tracking the computation, use with torch.no_grad instead
Manipulating x after the gradient were calculated, doesn’t change the gradients anymore
If you would like to scale the gradient, this would work:

x = torch.ones(5,3,32,32)
conv = nn.Conv2d(3,24,kernel_size=3, padding=1)
y = F.conv2d(x, conv.weight)
loss = y.sum() * 2
loss.backward()
print(conv.weight.grad[0,0,0]) 
> tensor([9000., 9000., 9000.])



Variable is deprecated since PyTorch 0.4.0, so you can now use tensors