I am looking for a alternative of numpy.linalg.pinv in tensorflow.
So far I have found that tensorflow have only tf.matrix_inverse(input, adjoint=None, name=None)
which throws an error if matrix in not invertible.
TensorFlow provides an SVD op, so you can compute the pseudo-inverse from that quite easily:
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With