Backward() on not working as expected

from torch.autograd import Variable
import torch
x = Variable(torch.Tensor([2]), requires_grad = True)
y = x + 2
x1 = Variable(torch.Tensor([3]), requires_grad = True)
x2 = Variable(torch.Tensor([4]), volatile = True) = ,,, 0) = ,,, 0).contiguous()
z = y * y *3
out = z.mean()
print(x, x.grad, x1.grad)

Output :
Variable containing:
[torch.FloatTensor of size 1]
Variable containing:
[torch.FloatTensor of size 5]

Ideally, x.grad (size 5) should be of the same size as x (size 1 ). if x is a non-leaf variable this raises an error (backward() raises an error). Any workaround for using

Firstly, x2 is volatile, this means that when pytorch sees x2 used in any calculation it does not store the computation graph. If any of your model’s inputs is volatile, then pytorch won’t be able to backpropagate.

Secondly, you can use on Variables directly. If you operate on .data then pytorch doesn’t track the operations and can’t backpropagate properly.

y =, x1, x2), 0)

Another potential problem could be the use of .resize_() which you can replace with a simple slice.

y = y[:5]

Normally x and x.grad must contain the same number of elements, but assigning the result of on confuses the autograd mechanism because you are changing the size of the underlying tensor without informing the computation graph of the change. It works fine with the above modifications.

The basic rule of backprop is to never use .data if you want to backpropagate. Don’t use volatile=True either, unless you are running in inference mode.

1 Like