Add overloads to diagonal docs (#144214)

Fixes #126827. Refactored doc to demonstrate when none of the optional values are passed in. Added another example so that all overloads of the function are covered.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/144214
Approved by: https://github.com/albanD
This commit is contained in:
Jackson 2025-01-31 15:53:59 +00:00 committed by PyTorch MergeBot
parent 2af876707b
commit a7c2d85c18

View file

@ -3611,13 +3611,20 @@ Examples::
[ 0.6927, -0.3735, -0.4945]])
>>> torch.diagonal(a, 0)
>>> torch.diagonal(a)
tensor([-1.0854, -0.0905, -0.4945])
>>> torch.diagonal(a, 1)
tensor([ 1.1431, 0.0360])
>>> b = torch.randn(2, 5)
>>> b
tensor([[-1.7948, -1.2731, -0.3181, 2.0200, -1.6745],
[ 1.8262, -1.5049, 0.4114, 1.0704, -1.2607]])
>>> torch.diagonal(b, 1, 1, 0)
tensor([1.8262])
>>> x = torch.randn(2, 5, 4, 2)
>>> torch.diagonal(x, offset=-1, dim1=1, dim2=2)