欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

pytorch的切片

程序员文章站 2022-03-30 09:38:03
...

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)

运行结果:
pytorch的切片
另一种示例:

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)

结果如下:
pytorch的切片

相关标签: pytorch 切片