I have an existing nn.Parameter, and a tensor with a gradient. I want to set the tensor to the parameter in a way that the parameter will have the same gradient (including perhaps .grad_fn and so on). The data of the parameter may be in a different shape than the new tensor.
How can I do it? The following is a not working example:
import torch
a = torch.tensor([1., 2.], requires_grad=True)
b = torch.tensor([3., 4.], requires_grad=True)
c = torch.stack((a,b))
print(c.grad_fn)
param = nn.Parameter(torch.randn(3, 3))
param.data = c
print(param.data.grad_fn)