[Python] How to apply the torch.inverse() function of PyTorch to every sample in the batch?

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    This may seem like a basic question, but I am unable to work it through.

    In the forward pass of my neural network, I have an output tensor of shape 8x3x3, where 8 is my batch size. We can assume each 3x3 tensor to be a non-singular matrix. I need to find the inverse of these matrices. The PyTorch inverse() function only works on square matrices. Since I now have 8x3x3, how do I apply this function to every matrix in the batch in a differentiable manner?

    If I iterate through the samples and append the inverses to a python list, which I then convert to a PyTorch tensor, should it be a problem during backprop? (I am asking since converting PyTorch tensors to numpy to perform some operations and then back to a tensor won't compute gradients during backprop for such operations)

    I also get the following error when I try to do something like that.

    a = torch.arange(0,8).view(-1,2,2)
    b = [m.inverse() for m in a]
    c = torch.FloatTensor(b)


    TypeError: 'torch.FloatTensor' object does not support indexing
    ​

    Continue reading...

Compartilhe esta Página