TensorFlow provides an SVD op, so you can easily calculate the pseudo-inverse:
def pinv(A, b, reltol=1e-6):
s, u, v = tf.svd(A)
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))
return tf.matmul(v, tf.matmul(s_inv, tf.matmul(u, tf.reshape(b, [-1, 1]), transpose_a=True)))
Pedro source
share