我想在 TensorFlow 中使用特征值,但它使用“tf.self_adjoint_eig”给出了错误的特征值/特征向量
在下面的代码中,我试图恢复原始矩阵,但输出与原始矩阵不同,知道如何修复吗?
`A11 = tf.constant([[1, 3],[1, 1]], dtype=tf.float32)
[e1, W1] = tf.self_adjoint_eig(A11)
e1 = tf.diag(e1)
A11r = tf.matmul(tf.matmul(W1, e1), tf.matrix_inverse(W1))
A11: [[1. 3.]
[1. 1.]]
A11r: [[0.9999999 0.9999999]
[0.9999999 0.9999999]]`
最佳答案
如果'tf.self_adjoint_eig'不是必须的,你可以这样做:
A11 = tf.constant([[1., 3.],[1., 1.]], dtype=tf.float32)
[e1, W1] = tf.linalg.eig(A11)
e1 = tf.linalg.diag(e1)
A11r = tf.matmul(tf.matmul(W1, e1), tf.linalg.inv(W1))
A11: [[1. 3.]
[1. 1.]]
A11r: [[0.9999999 0.9999999]
[0.9999999 0.9999999]]
试着把 tf.linalg.function_name()
以下是与您的相比的结果。
Input: A11
Output: <tf.Tensor: shape=(2, 2), dtype=float32, numpy=
array([[1., 3.],
[1., 1.]], dtype=float32)>
Input: A11r
Output: <tf.Tensor: shape=(2, 2), dtype=complex64, numpy=
array([[1.0000001+0.j, 3.0000005+0.j],
[1. +0.j, 1. +0.j]], dtype=complex64)>
Here are what I'm getting as errors, and check to see for 'import tensorflow as tf'
- AttributeError: module 'tensortlow' has no attribute 'self_adjoint_eig'
- AttributeError: module 'tensorflow' has no attribute 'diag'
- AttributeError: module 'tensorflow' has no attribute 'matrix_inverse'
关于python - TensorFlow 特征值和特征向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51535479/