首先,创建两个tensor,如下:import numpy as np
import torch
x1 = np.array([[1,0,0,1],[1,1,0,1],[0,1,1,0],[0,1,1,1],[1,1,0,0]])
x2 = np.array([[1,0,1],[1,1,1]])
x1= torch.from_numpy(x1).long()
x2= torch.from_numpy(x2).long()
"""
x1:tensor([[1, 0, 0, 1],
[1, 1, 0, 1],
[0, 1, 1, 0],
[0, 1, 1, 1],
[1, 1, 0, 0]])
x1.shape:torch.Size([5, 4])
x2:tensor([[1, 0, 1],
[1, 1, 1]])
x2.shape:torch.Size([2, 3])
x1[x2]:tensor([[[1, 1, 0, 1],
[1, 0, 0, 1],
[1, 1, 0, 1]],
[[1, 1, 0, 1],
[1, 1, 0, 1],
[1, 1, 0, 1]]])
"""