矩阵m1乘以tf.inverse(m1)不会产生单位矩阵

时间:2017-08-17 18:09:06

标签: python tensorflow matrix-multiplication matrix-inverse

在python中使用TensorFlow,我有以下代码:

sess = tf.InteractiveSession() # so I can eval()
t1 = tf.convert_to_tensor([[1,4,5],[34,5,1],[53,1,4]],dtype=tensorflow.float32)
t1.eval()
OUTPUT>> array([[  1.,   4.,   5.],
       [ 34.,   5.,   1.],
       [ 53.,   1.,   4.]], dtype=float32)
# so far, so good!

t1_inverse = tf.matrix_inverse(t1)
t1_inverse.eval()
OUTPUT>> array([[-0.01294278,  0.00749319,  0.01430518],
   [ 0.05653951,  0.17779292, -0.11512262],
   [ 0.15735695, -0.14373296,  0.08923706]], dtype=float32)
# I'm not a math whiz but this looks like an inverted matrix to me!

(t1*t1_inverse).eval() # should yield identity matrix, but.. 
OUTPUT>> array([[-0.01294278,  0.02997275,  0.07152588],
       [ 1.92234337,  0.88896459, -0.11512262],
       [ 8.33991814, -0.14373296,  0.35694823]], dtype=float32)

所以我的问题是,为什么矩阵t1乘以其逆不产生单位矩阵,或[[1,0,0],[0,1,0],[0,0,1]]?< / p>

2 个答案:

答案 0 :(得分:1)

此处t1*t1_inverse是逐元素乘法,您需要使用tf.matmul

idenity_mat = tf.matmul(t1, t1_inverse)
sess.run(identity_mat)   

# Results: array([[  1.00000000e+00,   5.96046448e-08,   0.00000000e+00],
                  [  0.00000000e+00,   1.00000000e+00,  -6.70552254e-08],
                  [ 0.00000000e+00,   5.96046448e-08,   9.99999881e-01]], dtype=float32) 

答案 1 :(得分:1)

您在代码中使用了正常的乘法符号:

(t1*t1_inverse).eval()

我认为我会做broadcast multiplication

您要使用的是matmul

tf.matmul(t1,t1_inverse)