Upper Triangular Matrix Vectorization

You could do this with a mask

def tril_mask(value):
    n = value.size(-1)
    coords = value.new(n)
    torch.arange(n, out=coords)
    return coords <= coords.view(n, 1)

which is used as

>>> value = torch.arange(9).view(3,3)
>>> value[tril_mask(value)]
 0
 3
 4
 6
 7
 8
[torch.FloatTensor of size 6]
1 Like