我正在寻找 tensorflow 中 numpy.linalg.pinv 的替代品。
到目前为止,我发现 tensorflow 只有 tf.matrix_inverse(input, adjoint=None, name=None)
如果矩阵不可逆,它会抛出错误。
最佳答案
TensorFlow 提供了 SVD 运算,因此您可以很容易地从中计算伪逆:
def pinv(A, b, reltol=1e-6):
# Compute the SVD of the input matrix A
s, u, v = tf.svd(A)
# Invert s, clear entries lower than reltol*s[0].
atol = tf.reduce_max(s) * reltol
s = tf.boolean_mask(s, s > atol)
s_inv = tf.diag(tf.concat([1. / s, tf.zeros([tf.size(b) - tf.size(s)])], 0))
# Compute v * s_inv * u_t * b from the left to avoid forming large intermediate matrices.
return tf.matmul(v, tf.matmul(s_inv, tf.matmul(u, tf.reshape(b, [-1, 1]), transpose_a=True)))
关于python - tensorflow 中 numpy.linalg.pinv 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42501715/