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)
y.data = torch.cat((y.data , x1.data, x2.data), 0)
#y.data = torch.cat((y.data , x1.data, x2.data), 0).contiguous()
y.data.resize_(5)
z = y * y *3
out = z.mean()
out.backward()
print(x, x.grad, x1.grad)
Output :
Variable containing:
2
[torch.FloatTensor of size 1]
Variable containing:
4.8000e+00
3.6000e+00
4.8000e+00
5.5023e-41
5.3249e-44
[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 torch.cat