先看如下代码:
from torch.nn import functional as F
import torch
import torch.autograd as autograd
input = torch.Tensor([[-0.0763,0.3447,-0.1181],[2.0828,0.5774,0.6320],[0.1,0.2,0.3]])
print(input)
output = F.log_softmax(input,dim = 1)
#每一维的索引单独用一个中括号的情况
print(output)
"""
#tensor([[-1.2478, -0.8268, -1.2896],
[-0.3759, -1.8813, -1.8267],
[-1.2019, -1.1019, -1.0019]] )
"""
print(output[range(3)][0])
#tensor([-1.2478, -0.8268, -1.2896])
print(output[0][range(3)])
#tensor([-1.2478, -0.8268, -1.2896])
print(output[range(3)][range(3)])
"""
#tensor([[-1.2478, -0.8268, -1.2896],
[-0.3759, -1.8813, -1.8267],
[-1.2019, -1.1019, -1.0019]])
"""
print(output[0:3][0:3])
"""
#tensor([[-1.2478, -0.8268, -1.2896],
[-0.3759, -1.8813, -1.8267],
[-1.2019, -1.1019, -1.0019]])
"""
#每一维的索引都在一个中括号的情况
print(output[range(3),range(3)])
#tensor([-1.2478, -1.8813, -1.0019])
print(output[0:3,0:3])
"""
#tensor([[-1.2478, -0.8268, -1.2896],
[-0.3759, -1.8813, -1.8267],
[-1.2019, -1.1019, -1.0019]])
"""
print(output[0,range(3)])
#tensor([-1.2478, -0.8268, -1.2896])
print(output[0,0:3])
#tensor([-1.2478, -0.8268, -1.2896])
穷举了很多中索引组合,自己去发现规律。