torch.index_select()的示例
x = torch.randn(3, 4)
print(x)
indices = torch.LongTensor([0, 2])
y = torch.index_select(x, 0, indices)
print(y)
z = torch.index_select(x, 1, indices)
print(z)
运行结果:
另一种示例:
embedding = nn.Embedding(4, 3)
idxs = [1,2, 3]
tensor = torch.LongTensor(idxs)
ids = autograd.Variable(tensor)
embs = embedding(ids)
print(embs)
print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&')
embs = embs[1:,:]
print(embs)
print('*******************************')
head = torch.rand(1, 3)
head = autograd.Variable(head)
result = torch.cat((head, embs), 0)
print(result)
结果如下: