Pinverse of a batch of 2d matrices

Is it possible to calculate the pseudo inverse of n matrices in one tensor?
Input tensor (input) has the shape of [n, rows, columns] and in the output should be a tensor (let’s call it pinv) with shape of [n, columns, rows] which for each i, pinv[i, :,:] is the pseudo inverse of input[i, :, :].


We do have a batch version of the inverse method but not the pinverse I’m afraid.

then it should be possible to calculate the batch pseudo inverse using the batch inverse like this:

def pinv(self, A):
        P1 = th.matmul(A.transpose(1,2), A)
        P2 = P1.inverse()
        P3 = th.matmul(P2, A.transpose(1,2))
        return P3

Of course it would be the left inverse

pinverse happily takes batches:

a = torch.randn(5,6,3)
b = torch.pinverse(a)
c = torch.stack([torch.pinverse(a_i) for a_i in a], 0)

gives 5e-8 ish discrepancy.

As it goes with Linear Algebra, the invariably awesome @vishwakftw implemented it just in time for the 1.3 release.

As a general rule, the nicely named linear algebra functions do batches.

Best regards


1 Like

Ho nice !
I was looking at the doc and this change was not reflected there. I’ll fix that quickly.