我有以下代码片段。许多西格玛矩阵是由神经网络生成的,并且这些矩阵必须是正定的。所以这是在调试功能中检查的。
奇怪的是 Sigma2 没有给出 *** LinAlgError: Matrix is not Positive Defined Error,而 Sigma 却给出了。总而言之,前 1499 次迭代一切顺利。
但 np.sum(np.abs(Sigma-Sigma2)) 给出的值为 19687.622918061788。因此,Tensorflow 中构建的 Sigma 与 Numpy 中生成的 Sigma2 的值似乎不同。这怎么可能?
# Because there is no tensorflow equivalent for np.eye()
identity = tf.Variable(tf.convert_to_tensor(np.eye(mu_shape[-1].value), dtype=tf.float32))
Sigma = tf.batch_matmul(L,tf.matrix_transpose(L)) + 1e-4*identity
# Check positive definite
def _debug_print_func(Sigma,L):
try:
C = np.linalg.cholesky(Sigma)
except np.linalg.LinAlgError:
Sigma2 = np.matmul(L,np.transpose(L,axes=[0,1,3,2]))+1e-4*np.eye(4))
print 'LinAlgError'
# Sigma different from Sigma2??
pdb.set_trace()
return False
debug_print_op = tf.py_func(_debug_print_func, [Sigma,L], [tf.bool])
with tf.control_dependencies(debug_print_op):
Sigma = tf.identity(Sigma, name='out')
编辑:
还有一个小数字示例
在我计算的调试函数中
W=np.matmul(L[0,0,:,:],L[0,0,:,:].T)+1e-4*np.eye(4)
那我就做
Sigma[0,0,:,:]-W
Sigma[0,0,:,:]-W 给出
array([[ -2.39243686e-05, -2.29620725e-05, 1.46458477e-05, 2.68967569e-05],
[ -2.29620725e-05, -3.72494698e-05, 2.18125388e-05, 3.86031806e-05],
[ 1.46458477e-05, 2.18125388e-05, -3.83383453e-05, 2.45112851e-05],
[ 2.68967569e-05, 3.86031806e-05, 2.45112851e-05, -5.60631990e-05]])
因此,Numpy 和 TensorFlow 之间的所有元素似乎都存在 1e-5 顺序的差异。这似乎决定了正定与否之间的区别。
这是 TensorFlow 中的错误还是有办法解决这个问题?