I have a tensor like this:
x = torch.tensor([[3, 4, 2], [0, 1, 5]])
and I have a indexes like this:
ind = torch.tensor([[1, 1, 0], [0, 0, 1]])
then I want to generate a new tensor by x and ind:
z = torch.tensor([0, 1, 2], [3, 4, 5])
I impement it with python like this:
# -*- coding: utf-8 -*-
import torch
x = torch.tensor([[3, 4, 2], [0, 1, 5]])
ind = torch.tensor([[1, 1, 0], [0, 0, 1]])
z = torch.zeros_like(x)
for i in range(x.shape[0]):
for j in range(x.shape[1]):
z[i, j] = x[ind[i][j]][j]
print(z)
I want to know how to solve this by pytorch?