How to vectorize matrix inversion while handling runtime error in pytorch
I need to invert some matrices in pytorch. However, some of the matrices are not invertible, which leads to the code throwing runtime error as follows,

matrices = torch.randn([5,3,3])
matrices[[2,3]] = torch.zeros([3,3])
inverses = torch.inverse(matrices)

RuntimeError: inverse_cpu: For batch 2: U(1,1) is zero, singular U.


I have a fallback technique for such situations. However, I can't figure out which of the matrices throw the error. Currently, I have replaced the code with non-vectorized version, but it has become a bottleneck.

Is there a way to handle this without giving up vectorization?

The best way I can think of is to first calculate the determinate of each matrix, then calculate inverses of those that have a abs(det)&gt;0.

matrices = torch.randn([5,3,3])
matrices[[2,3]] = torch.zeros([3,3])
determinants = torch.det(matrices)
inverses = torch.inverse(matrices[determinants.abs()&gt;0.])


You'll have to handle the removal of singular matrices, but that shouldn't be too hard since you have the index values of those matrices from determinants.abs()==0.. This allows you to keep the inversion vectorized.
