Hi Fangyu!



 Arthur-99:

when I do backward() to some non-scalar variables $y$, the shape of result is always the same as input $x$.
Is there any method to get a y-shaped result?


I’m not sure that I understand your exact use case, but the beta-version
torch.autograd.functional.jacobian() 3 might be what you want:
>>> torch.__version__
'1.7.1'
>>> def my_model (x):
...     return torch.arange (2 * x.numel()).reshape ((2, -1)) * x * x
...
>>> x = torch.tensor ([2.0, 3.0, 5.0])
>>> my_model (x)
tensor([[  0.,   9.,  50.],
        [ 12.,  36., 125.]])
>>> torch.autograd.functional.jacobian (my_model, x)
tensor([[[ 0.,  0.,  0.],
         [ 0.,  6.,  0.],
         [ 0.,  0., 20.]],

        [[12.,  0.,  0.],
         [ 0., 24.,  0.],
         [ 0.,  0., 50.]]])

Best.
K. Frank