Hi Xianqian!



 xianqian:

How can one implement such a check in pytorch (I specifically meant not using a for loop because that won’t be a feasible check in a full fledged code)?


cumprod() – for boolean values – will test for “decreasing values”:
>>> import torch
>>> torch.__version__
'1.9.0'
>>>
>>> b1 = torch.tensor ([
...     [True,  False, False, False],
...     [False, False, False, False],
...     [True,  True,  False, False],
...     [True,  True,  True,  True ]
...     ])
>>>
>>> b2 = torch.tensor ([
...     [True,  False, True,  True ],
...     [False, False, False, False],
...     [True,  True,  False, False],
...     [True,  True,  True,  True ]
...     ])
>>>
>>> b3 = torch.tensor ([
...     [True,  False, True,  True ],
...     [False, True,  False, False],
...     [True,  True,  False, False],
...     [True,  True,  True,  True ]
...     ])
>>>
>>> torch.equal (b1, b1.cumprod (1).bool())
True
>>> torch.equal (b2, b2.cumprod (1).bool())
False
>>> torch.equal (b3, b3.cumprod (1).bool())
False

To see in a little more detail what is going on, consider:
>>> torch.all (torch.eq (b1, b1.cumprod (1).bool()), 1)
tensor([True, True, True, True])
>>> torch.all (torch.eq (b2, b2.cumprod (1).bool()), 1)
tensor([False,  True,  True,  True])
>>> torch.all (torch.eq (b3, b3.cumprod (1).bool()), 1)
tensor([False, False,  True,  True])

Best.
K. Frank