``````a=nn.Parameter(torch.tensor([[2.]]))
b=a*2
print(b)
d=nn.Parameter(torch.tensor(3.))
print(d)
e=b[,:]*d
b=e#inplaced
print(e)
loss1=e.sum()
e=b[,:]*d
b=e
print(e)
loss1+=e.sum()
``````

`loss1.backward()` works well,why?the initial b(

) has been inplaced by e,how can this loss backward to a?

i just find the gradient to a works well,but do not know why?becuase i konw pytorch always encounter problems when using inplace operation,but this case works well,why?

Hi Bingqing!

The `b[,:]` in the line `e=b[,:]*d` creates a new tensor, rather
than just a view into `b`, and it is this new tensor that is stored in the
computation graph and then used for backpropagation. So, even
though `b=e#inplaced` does modify `b` in place, the actual tensor
stored in the computation graph is not modified in place.

Consider:

``````>>> import torch
>>> print (torch.__version__)
1.10.2
>>>
>>> a = torch.nn.Parameter (torch.tensor ([[2.]]))
>>> b = a * 2
>>> c_new =  b[, :]   # c_new is a new tensor
>>> c_view = b        # c_view is a view into b
>>> print (b)
>>> print (c_new)
>>> print (c_view)
>>> b = 99.9          # inplace modification of b
>>> print (b)
>>> print (c_new)        # c_new is not modified -- won't break backpropagation
>>> print (c_view)       # c_view reflects the modification -- breaks backpropagation
>>>
>>> a = torch.nn.Parameter (torch.tensor ([[2.]]))
>>> b = a * 2
>>> print (b)
>>> d = torch.nn.Parameter (torch.tensor (3.))
>>> print (d)
Parameter containing:
>>> e = b[, :] * d    # b[, :] is actually a new tensor in the computation graph
>>> b = e             # inplaced, but doesn't break backpropagation
>>> print (e)
>>> loss1 = e.sum()
>>> loss1.backward()
>>>
>>> a = torch.nn.Parameter (torch.tensor ([[2.]]))
>>> b = a * 2
>>> print (b)
>>> d = torch.nn.Parameter (torch.tensor (3.))
>>> print (d)
Parameter containing:
>>> e = b * d         # b is a view into b
>>> b = e             # inplaced -- breaks backpropagation
>>> print (e)
>>> loss1 = e.sum()
>>> loss1.backward()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<path_to_pytorch_install>\torch\_tensor.py", line 307, in backward