Hi, albanD,
I also have a problem with gradient back propagation.
My example is as follows（Correct code）:
w = torch.tensor([[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.],[10.,11.,12.]]], requires_grad=True)
x = torch.tensor([4.], requires_grad=True) 

a = torch.add(w, x)     # a = w + x
b = torch.add(w, 1)     # b = w + 1
y = torch.mul(a, b)     # y = a * b

y.backward(torch.ones_like(w))   
print(w.grad)   

The right result is：
tensor([[[ 7.,  9., 11.],
         [13., 15., 17.],
         [19., 21., 23.],
         [25., 27., 29.]]])

But，When I want to change the shape of the variable W from [1,4,3] to [4,3], a warning occurs and the output is none。
As shown below：
w = torch.tensor([[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.],[10.,11.,12.]]], requires_grad=True)
w=w.squeeze()
x = torch.tensor([4.], requires_grad=True) 

a = torch.add(w, x)     # a = w + x
b = torch.add(w, 1)     # b = w + 1
y = torch.mul(a, b)     # y = a * b

y.backward(torch.ones_like(w))   
print(w.grad)  

The warning:
UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. 
Its .grad attribute won't be populated during autograd.backward(). 
If you indeed want the gradient for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. 
If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead.

My question is Can’t I change the shape of the tensor in the process of gradient back propagation? No matter it’s view, reshape, or squeeze, etc
Waiting for your reply