In [2]:
import torch
import numpy as np

torch.__version__


Out[2]:
'0.4.1'

In [3]:
a = torch.arange(1000).view((20, 10, 5))
a.size()


Out[3]:
torch.Size([20, 10, 5])

In [4]:
a[1, 2].size(), a[1, 2, 3].size()


Out[4]:
(torch.Size([5]), torch.Size([]))

In [5]:
idx = torch.LongTensor([[0, 2, 3], [0, 2, 4]])
a[idx].size(), a[idx[0]].size()


Out[5]:
(torch.Size([2, 3, 10, 5]), torch.Size([3, 10, 5]))

In [6]:
a[[0, 0], [0, 0], [0, 1]]


Out[6]:
tensor([0, 1])

In [7]:
idx
a[0, 2, 3]


Out[7]:
tensor(13)

In [8]:
a[idx[:, 0], idx[:, 1], idx[:, 2]]


Out[8]:
tensor([13, 14])

In [9]:
A = np.arange(24).reshape((4, 3, 2))
A[[3, 0, 0, 1], [1, 2, 2, 0], [0, 0, 0, 0]].shape


Out[9]:
(4,)

In [10]:
A = np.arange(12).reshape((4, 3))
print(A)
A[[2, 0], [0, 1]]


[[ 0  1  2]
 [ 3  4  5]
 [ 6  7  8]
 [ 9 10 11]]
Out[10]:
array([6, 1])

In [11]:
idx = torch.LongTensor([0, 2, 1])
a[idx].size()
a.size(), a.select(0, 2).size()


Out[11]:
(torch.Size([20, 10, 5]), torch.Size([10, 5]))

In [12]:
a.index_select(0, idx).size()


Out[12]:
torch.Size([3, 10, 5])

In [13]:
b = torch.tensor([[1, 2], [3, 4], [5, 6]]) + 10
idx2 = torch.LongTensor([[0, 1]])

torch.gather(b, 0, idx2)


Out[13]:
tensor([[11, 14]])

In [14]:
import numpy as np

In [15]:
A = np.arange(10, 16).reshape(3, 2)
A[[1, 0], [1, 0]]


Out[15]:
array([13, 10])

In [ ]:

Pytorch masking

Set unmasked elements to zero.


In [16]:
x = torch.arange(10, 50, dtype=torch.float).view(4, 10)
xlen = torch.LongTensor([4, 8, 1, 10])

m = torch.arange(x.size(1)).unsqueeze(0).expand(x.size())

mask = xlen.unsqueeze(1).expand(x.size()) <= m
x[mask] = float('-inf')

x


Out[16]:
tensor([[10.0000, 11.0000, 12.0000, 13.0000,    -inf,    -inf,    -inf,    -inf,
            -inf,    -inf],
        [20.0000, 21.0000, 22.0000, 23.0000, 24.0000, 25.0000, 26.0000, 27.0000,
            -inf,    -inf],
        [30.0000,    -inf,    -inf,    -inf,    -inf,    -inf,    -inf,    -inf,
            -inf,    -inf],
        [40.0000, 41.0000, 42.0000, 43.0000, 44.0000, 45.0000, 46.0000, 47.0000,
         48.0000, 49.0000]])

In [17]:
s = torch.nn.Softmax(dim=-1)
s(x)


Out[17]:
tensor([[0.0321, 0.0871, 0.2369, 0.6439, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
         0.0000],
        [0.0006, 0.0016, 0.0043, 0.0116, 0.0315, 0.0856, 0.2326, 0.6323, 0.0000,
         0.0000],
        [1.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000,
         0.0000],
        [0.0001, 0.0002, 0.0006, 0.0016, 0.0043, 0.0116, 0.0315, 0.0856, 0.2326,
         0.6321]])

Accuracy using masked elements


In [18]:
ground_truth = torch.LongTensor(4, 5).random_(0, 3)
ground_truth


Out[18]:
tensor([[2, 2, 2, 0, 1],
        [0, 2, 0, 0, 2],
        [2, 0, 2, 2, 2],
        [0, 0, 2, 1, 1]])

In [19]:
prediction = torch.LongTensor(4, 5).random_(0, 3)
prediction


Out[19]:
tensor([[0, 2, 2, 0, 0],
        [1, 1, 1, 0, 2],
        [1, 1, 2, 0, 2],
        [1, 2, 1, 0, 0]])

In [20]:
doc_lens = torch.LongTensor([2, 5, 4, 1])

In [21]:
m = torch.arange(ground_truth.size(1)).unsqueeze(0).expand(ground_truth.size())
mask = doc_lens.unsqueeze(1).expand(ground_truth.size()) <= m
mask


Out[21]:
tensor([[0, 0, 1, 1, 1],
        [0, 0, 0, 0, 0],
        [0, 0, 0, 0, 1],
        [0, 1, 1, 1, 1]], dtype=torch.uint8)

In [22]:
correct = torch.eq(ground_truth, prediction)
correct[mask] = 0
correct


Out[22]:
tensor([[0, 1, 0, 0, 0],
        [0, 0, 0, 1, 1],
        [0, 0, 1, 0, 0],
        [0, 0, 0, 0, 0]], dtype=torch.uint8)

In [23]:
correct.sum().item() / doc_lens.sum().item()


Out[23]:
0.3333333333333333

In [ ]:


In [99]:
X = torch.arange(36).view(3, -1)
splits = torch.LongTensor([3, 10, 4])

mask = torch.arange(12).unsqueeze(0).expand((3, 12)) < splits.unsqueeze(1).expand((3, 12))
X2 = X.clone()
X2[mask] = 0
X, X2
#X[[0, 1, 2], 2:4]


Out[99]:
(tensor([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11],
         [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23],
         [24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]]),
 tensor([[ 0,  0,  0,  3,  4,  5,  6,  7,  8,  9, 10, 11],
         [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 22, 23],
         [ 0,  0,  0,  0, 28, 29, 30, 31, 32, 33, 34, 35]]))

In [105]:
X3 = torch.zeros_like(X)

i = np.repeat([0, 1, 2], X.size(1) - splits)
import itertools
j = list(itertools.chain(*(range(s.item(), X.size(1)) for s in splits)))

len(i), len(j)


Out[105]:
(19, 19)

In [106]:
flipped_mask = mask[:, list(range(X.size(1)-1, -1, -1))]

X3[~flipped_mask] = X[i, j]
X3


Out[106]:
tensor([[ 3,  4,  5,  6,  7,  8,  9, 10, 11,  0,  0,  0],
        [22, 23,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
        [28, 29, 30, 31, 32, 33, 34, 35,  0,  0,  0,  0]])

In [102]:
i = np.repeat([0, 1, 2], splits)
j = list(itertools.chain(*(range(s) for s in splits)))

X3[flipped_mask] = X[i, j]

X3


Out[102]:
tensor([[ 3,  4,  5,  6,  7,  8,  9, 10, 11,  0,  1,  2],
        [22, 23, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21],
        [28, 29, 30, 31, 32, 33, 34, 35, 24, 25, 26, 27]])

In [103]:
X[[0, 1, 2], [0, 2, 3]]


Out[103]:
tensor([ 0, 14, 27])

In [ ]: