Python 从一个新的张量将索引和基调值结合起来

Python 从一个新的张量将索引和基调值结合起来,python,pytorch,Python,Pytorch,我有一个像a=torch的张量,张量[1,2,0,1,2]。我想计算一个张量b,它的指数和张量a的值如下: b=张量[[0,1],[1,2],[2,0],[3,1],[4,2]] 编辑:a[i]大于等于0。一种方法是: b = torch.IntTensor(list(zip(range(0, list(a.size())[0], 1), a.numpy()))) 输出: tensor([[0, 1], [1, 2], [2, 0], [3, 1

我有一个像a=torch的张量,张量[1,2,0,1,2]。我想计算一个张量b,它的指数和张量a的值如下: b=张量[[0,1],[1,2],[2,0],[3,1],[4,2]]


编辑:a[i]大于等于0。

一种方法是:

b = torch.IntTensor(list(zip(range(0, list(a.size())[0], 1), a.numpy())))
输出:

tensor([[0, 1],
        [1, 2],
        [2, 0],
        [3, 1],
        [4, 2]], dtype=torch.int32)
tensor([[0, 1],
        [1, 2],
        [2, 0],
        [3, 1],
        [4, 2]])
或者,您也可以使用torch.cat,如下所示:

a = torch.tensor([1,2,0,1,2])
indices = torch.arange(0, list(a.size())[0])
res = torch.cat([indices.view(-1, 1), a.view(-1, 1)], 1) 
输出:

tensor([[0, 1],
        [1, 2],
        [2, 0],
        [3, 1],
        [4, 2]], dtype=torch.int32)
tensor([[0, 1],
        [1, 2],
        [2, 0],
        [3, 1],
        [4, 2]])

一种方法是:

b = torch.IntTensor(list(zip(range(0, list(a.size())[0], 1), a.numpy())))
输出:

tensor([[0, 1],
        [1, 2],
        [2, 0],
        [3, 1],
        [4, 2]], dtype=torch.int32)
tensor([[0, 1],
        [1, 2],
        [2, 0],
        [3, 1],
        [4, 2]])
或者,您也可以使用torch.cat,如下所示:

a = torch.tensor([1,2,0,1,2])
indices = torch.arange(0, list(a.size())[0])
res = torch.cat([indices.view(-1, 1), a.view(-1, 1)], 1) 
输出:

tensor([[0, 1],
        [1, 2],
        [2, 0],
        [3, 1],
        [4, 2]], dtype=torch.int32)
tensor([[0, 1],
        [1, 2],
        [2, 0],
        [3, 1],
        [4, 2]])