Pytorch 如何得到不包括某些指数的argmaxed torch张量?

Pytorch 如何得到不包括某些指数的argmaxed torch张量?,pytorch,Pytorch,我想知道我是否可以得到我输入的torch.argmax,不包括某些索引。 比如说, target = torch.tensor([1,2]) input = torch.tensor([[0.1,0.5,0.2,0.2], [0.1,0.5,0.1,0.3]]) 我想获得输入中的最大值,不包括目标上的索引,这样结果将是 output = torch.tensor([[0.2],[0.5]]) 你可以试试这个 将负infy设置为温度张量中的目标索引 然后使用torch.max或torch.a

我想知道我是否可以得到我输入的torch.argmax,不包括某些索引。 比如说,

target = torch.tensor([1,2])
input = torch.tensor([[0.1,0.5,0.2,0.2], [0.1,0.5,0.1,0.3]])
我想获得输入中的最大值,不包括目标上的索引,这样结果将是

output = torch.tensor([[0.2],[0.5]])
你可以试试这个

  • 将负infy设置为温度张量中的
    目标
    索引
  • 然后使用
    torch.max
    torch.argmax
tmp_input=input.clone()
tmp_输入[范围(长度(输入)),目标]=浮点(“-Inf”)
火炬最大值(tmp_输入,尺寸=1)。数值
张量([0.2000,0.5000])
火炬最大值(tmp_输入,尺寸=1)。指数
张量([3,1])
torch.argmax(tmp_输入,尺寸=1)
张量([3,1])
input[target[0]-1,target[1]-1] = -1 # or use -inf 
#-1 is added for python indexing style
output  = torch.max(input,dim = 1)