Why do eigen value computation didn't match with tf code?

input = np.array([[2,4,6],[3,5,7],[1,6,8]], dtype=np.float32) 
eigval_all, eigvec_all = tf.self_adjoint_eig(input)
with tf.compat.v1.Session() as sess:
    print(sess.run(tf.self_adjoint_eig(input)))

OUTPUT:

(array([-0.94226784,  2.6309953 , 13.311278  ], dtype=float32), array([[-0.580067  , -0.7810435 ,  0.2312871 ],
       [ 0.70494777, -0.33907872,  0.62295616],
       [-0.40813133,  0.52440166,  0.7472832 ]], dtype=float32))

Pytorch equivalent:
torch.symeig(torch.tensor(input), eigenvectors=True)

OUTPUT:

torch.return_types.symeig(
eigenvalues=tensor([-1.7482, -0.3348, 17.0831]),
eigenvectors=tensor([[ 0.7941,  0.4292,  0.4304],
        [ 0.1423, -0.8197,  0.5549],
        [-0.5909,  0.3794,  0.7119]]))

Another:
torch.eig(torch.tensor(input),eigenvectors=True)

OUTPUT:

torch.return_types.eig(
eigenvalues=tensor([[14.6176,  0.0000],
        [ 0.1912,  0.6115],
        [ 0.1912, -0.6115]]),
eigenvectors=tensor([[-0.4899,  0.1709,  0.3562],
        [-0.6077,  0.7177,  0.0000],
        [-0.6250, -0.5663, -0.0900]]))

Hi Abhi!

Your problem is that you are passing a matrix that is not symmetric
to eigenvalue routines that require symmetric (or self-adjoint) matrices.

Pytorch doesn’t verify that the matrix is symmetric and simply uses,
by default, its upper triangle.

Note that you can reproduce the tensorflow result by telling
torch.symeig() to use the lower triangle or by transposing its input:

>>> import torch
>>> torch.__version__
'1.9.0'
>>> t = torch.tensor ([[2,4,6],[3,5,7],[1,6,8]], dtype = torch.float)
>>> torch.symeig (t, upper = True)
torch.return_types.symeig(
eigenvalues=tensor([-1.7482, -0.3348, 17.0831]),
eigenvectors=tensor([]))
>>> torch.symeig (t, upper = False)
torch.return_types.symeig(
eigenvalues=tensor([-0.9423,  2.6310, 13.3113]),
eigenvectors=tensor([]))
>>> torch.symeig (t.T, upper = True)
torch.return_types.symeig(
eigenvalues=tensor([-0.9423,  2.6310, 13.3113]),
eigenvectors=tensor([]))

Be aware, given that your input matrix is asymmetric, the above results
are not correct; the correct result is the one with complex eigenvalues
that you obtained with torch.eig().

Best.

K. Frank

2 Likes

I was using earlier versions of Pytorch 1.4 where I could not find torch.linalg.eig. I updated it to newest version 1.9.0 and I able to get identical results comparing with Tensorflow.

Abhi