Ali_Mirzaeyan:

I added retain_grad() after torch.cat, as explained here https://stackoverflow.com/questions/55942423/pytorch-backpropagating-from-sum-of-matrix-elements-to-leaf-variable,  and apparently it works now.


x = Variable(torch.tensor([1.]), requires_grad=True)
y = Variable(torch.tensor([1.]), requires_grad=True)
target = Variable(torch.tensor([3., 3.]))
z = torch.cat([x,y])
z.retain_grad()
loss = Variable( torch.mean(z - target), requires_grad = True)
loss.backward()