Alternative numpy.linalg.pinv in tensor flow

I am looking for an alternative to numpy.linalg.pinv in a tensor stream. So far, I have found that a tensor flow only has an tf.matrix_inverse(input, adjoint=None, name=None)error if the matrix is ​​not reversible.

+4
source share
3 answers

TensorFlow provides an SVD op, so you can easily calculate the pseudo-inverse:

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)))
+6
source

I do not know alternatives to numpy.linalg.pinv in a tensor flow, but regularization is an alternative if the matrix is ​​not invertible. Example:

try: 
    result = tf.matrix_inverse(input, adjoint=None, name=None)
except: 
    input += np.identity((input.shape))* c 
    result = tf.matrix_inverse(input, adjoint=None, name=None)

Where c is a constant and must be very small, for example c = 0,000001

, . .

+2

You can combine tf.py_func tensorflow with numpy pseudo inverse as follows:

return tf.py_func(np.linalg.pinv, [input], tf.float32)
+1
source

Source: https://habr.com/ru/post/1670989/


All Articles