The function of these two parameters is shown in the form of images .
X = torch.tensor([[1, 2, 3], [4, 5, 6]])
print(X.sum(dim=0, keepdim=True))
print(X.sum(dim=1, keepdim=True))
print(X.sum(dim=0, keepdim=False))
print(X.sum(dim=1, keepdim=False))
view The usage of the function is as follows , It's for change tensor Dimensions . among -1 Indicates that the current dimension will be adaptive to other specified dimensions .
y = torch.LongTensor([0, 2])
print(y,y.shape)
print(y.view(-1, 1),y.view(-1, 1).shape)
#-------------------------
tensor([0, 2]) torch.Size([2])
tensor([[0],
[2]]) torch.Size([2, 1])
ganther The usage of the function is as follows , Used to retrieve targets in bulk tensor Data corresponding to dimension in .
y_hat = torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]])
y1 = torch.LongTensor([[0, 1, 1]])
y2 = torch.LongTensor([[1,2]])
print(y_hat.gather(0, y1.view(1, -1)))
print(y_hat.gather(1, y2.view(-1, 1)))
#---------------------
tensor([[0.1000, 0.2000, 0.5000]])
tensor([[0.3000],
[0.5000]])
y_hat = torch.tensor([[0.1, 0.3, 0.6], [0.3, 0.2, 0.5]])
print(y_hat.argmax(dim=0))
print(y_hat.argmax(dim=1))
#-----------------------
tensor([1, 0, 0])
tensor([2, 2])