A simple question about torch.einsum function
A = torch.randn(5,5)
B = torch.einsum(&quot;ii-&gt;i&quot;,A)
C = torch.einsum(&quot;ii&quot;,A)

Just I exhibit above,I know the result about B that means getting the diagonal elements.
print(&quot;before:&quot;,A)
print(&quot;after:&quot;,B)
print('Why:',C)

results
before: tensor([[-0.2339,  0.2501, -1.1814,  1.4392, -0.5461],
        [ 1.4908,  0.0626, -0.6849, -1.3106,  0.1257],
        [ 3.3362, -1.7438,  0.3027,  0.4346,  0.6830],
        [-0.6183,  0.5965,  1.2653,  1.0319, -0.0670],
        [-0.5531, -0.4245, -2.4869,  1.2972,  0.6732]])
after: tensor([-0.2339,  0.0626,  0.3027,  1.0319,  0.6732])
Why: tensor(1.8366)

So,Why tensor C is 1.8366?

Executing torch.einsum(&quot;ii&quot;, A) is equivalent to torch.einsum(&quot;ii-&gt;&quot;, A), this means the output has no index. You can interpret the output as a rank zero tensor.
So this corresponds to computing the sum of the diagonal elements.
