Hi Rishi!



 snau:

a = torch.zeros(2, 2)
a[0,0] = param
a[1,1] = 2 

then I get the following error
…


You haven’t posted the complete code that leads to your error.  Your
code fragment (which is Lezcano’s solution) should work for your use
case.  Here is a complete example:
>>> import torch
>>> torch.__version__
'1.9.0'
>>> param = torch.ones (1, requires_grad = True)
>>> a = torch.zeros (2, 2)
>>> a[0, 0] = param
>>> a[1, 1] = 2
>>> a
tensor([[1., 0.],
        [0., 2.]], grad_fn=<CopySlices>)
>>> param.grad
>>> a.sum().backward()
>>> param.grad
tensor([1.])

Best.
K. Frank