Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative of numpy.linalg.pinv in tensorflow

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.

like image 654
Rahul Avatar asked Dec 14 '22 00:12

Rahul


1 Answers

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)))
like image 141
Pedro Avatar answered Dec 16 '22 14:12

Pedro