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

planet: <pre><code class="lang-auto">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)))
</code></pre>
<p>OUTPUT:</p>
<pre><code class="lang-auto">(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))
</code></pre>
<p>Pytorch equivalent:<br>
<code>torch.symeig(torch.tensor(input), eigenvectors=True)</code></p>
<p>OUTPUT:</p>
<pre><code class="lang-auto">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]]))
</code></pre>
<p>Another:<br>
<code>torch.eig(torch.tensor(input),eigenvectors=True)</code></p>
<p>OUTPUT:</p>
<pre><code class="lang-auto">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]]))
</code></pre>

KFrank: <p>Hi Abhi!</p>
<aside class="quote no-group quote-modified" data-username="planet" data-post="1" data-topic="130661" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/planet/48/31699_2.png" class="avatar"> planet:</div>
<blockquote>
<pre><code class="lang-auto">input = np.array([[2,4,6],[3,5,7],[1,6,8]], dtype=np.float32) 
eigval_all, eigvec_all = tf.self_adjoint_eig(input)
</code></pre>
<p>Pytorch equivalent:<br>
<code>torch.symeig(torch.tensor(input), eigenvectors=True)</code></p>
</blockquote>
</aside>
<p>Your problem is that you are passing a matrix that is not symmetric<br>
to eigenvalue routines that require symmetric (or self-adjoint) matrices.</p>
<p>Pytorch doesn’t verify that the matrix is symmetric and simply uses,<br>
by default, its upper triangle.</p>
<p>Note that you can reproduce the tensorflow result by telling<br>
<code>torch.symeig()</code> to use the lower triangle or by transposing its input:</p>
<pre><code class="lang-plaintext">&gt;&gt;&gt; import torch
&gt;&gt;&gt; torch.__version__
'1.9.0'
&gt;&gt;&gt; t = torch.tensor ([[2,4,6],[3,5,7],[1,6,8]], dtype = torch.float)
&gt;&gt;&gt; torch.symeig (t, upper = True)
torch.return_types.symeig(
eigenvalues=tensor([-1.7482, -0.3348, 17.0831]),
eigenvectors=tensor([]))
&gt;&gt;&gt; torch.symeig (t, upper = False)
torch.return_types.symeig(
eigenvalues=tensor([-0.9423,  2.6310, 13.3113]),
eigenvectors=tensor([]))
&gt;&gt;&gt; torch.symeig (t.T, upper = True)
torch.return_types.symeig(
eigenvalues=tensor([-0.9423,  2.6310, 13.3113]),
eigenvectors=tensor([]))
</code></pre>
<p>Be aware, given that your input matrix is asymmetric, the above results<br>
are not correct; the correct result is the one with complex eigenvalues<br>
that you obtained with <code>torch.eig()</code>.</p>
<p>Best.</p>
<p>K. Frank</p>

planet: <p>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.</p>
<p>Abhi</p>