pythonnumpypytorchnumpy-ufunc

what is the equivalent of numpy accumulate ufunc in pytorch


In numpy, I can do the following:

>>> x = np.array([[False, True, False], [True, False, True]])
>>> z0 = np.logical_and.accumulate(x, axis=0)
>>> z1 = np.logical_and.accumulate(x, axis=1)

This returns the following:

>>> z0
array([[False,  True, False],
       [False, False, False]])

>>> z1
array([[False, False, False],
       [ True, False, False]])

What is the equivalent of this ufunc operation in pytorch?


Solution

  • The logical and corresponds to a product in binary terms. You can use cumprod for that:

    >>> x.cumprod(dim=0).bool()
    tensor([[False,  True, False],
            [False, False, False]])
    
    >>> x.cumprod(dim=1).bool()
    tensor([[False, False, False],
            [ True, False, False]])