关于python列表的索引的相关用法

☆樱花仙子☆ 提交于 2020-02-23 15:27:39

先看如下代码:

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])

穷举了很多中索引组合,自己去发现规律。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!