gpt4 book ai didi

python - tensorflow 中 numpy.linalg.pinv 的替代方案

转载 作者:太空狗 更新时间:2023-10-30 01:47:56 25 4
gpt4 key购买 nike

我正在寻找 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com