Einsum calculation accuracy ?

When the matrix result is calculated directly and the two-step calculation, the results are different.
Is it because of the problem of calculation accuracy (torch.einsum)?
I use the GeForce RTX 3090 GPUs

a=torch.randn(2,3,5)
b=torch.randn(2,3,4)
z1=torch.einsum('ijk,ijb->bk',a,b)
z2=torch.einsum('ibk->bk',torch.einsum('ijk,ijb->ibk',a,b))
z1==z2

Floating point numbers cannot / should not be compared with == due to precision errors in representing them.
Use torch.allclose() instead for comparing them.

1 Like