[Python] How do you invert a tensor of boolean values in Pytorch?

Discussão em 'Python' iniciado por Stack, Junho 27, 2025.

  1. Stack

    Stack Membro Participativo

    With NumPy, you can do it with np.invert(array), but there's no invert function in Pytorch. Let's say I have a 2D tensor of boolean values:

    import torch

    ts = torch.rand((10, 4)) < .5


    tensor([[ True, True, False, True],
    [ True, True, True, True],
    [ True, False, True, True],
    [False, True, True, False],
    [False, True, True, True],
    [ True, True, True, True],
    [ True, False, True, True],
    [False, True, False, True],
    [ True, True, False, True],
    [False, False, True, False]])


    How do I transform the False into True and vice versa?

    Continue reading...

Compartilhe esta Página