沈阳网站建设公司怎么样西安网站定制开发
如何取出多维torch指定维度的指定“行”
- 从二维torch开始
- 新建torch
- 取出某一行
- 取出某一列
- 一次性取出多行
- 取出连续的多行
- 取出不连续的多行
- 一次取出多列
- 取出连续的多列
- 取出不连续的多列
- 考虑三维torch
- 取出三维torch的任意两行(means 在dim=0上操作)
- 取出连续的行
- 取出不连续的行
- 取出任意列
- 取出连续的列 & 取出任意列
- 取出任意页(dim=2)
- 取出前n页
- 取出任意页
- else:取出dim=0/dim=1/dim=2的任意元素操作
从二维torch开始
新建torch
import torch# 新建一个二维 torch
a = torch.tensor ( [[1,2,3,4],[2,3,1,5],[5,1,7,2]])
a.shapeOut[5]: torch.Size([3, 4])
取出某一行
a[1] # 取出第1行(从0行开始)Out[6]: tensor([2, 3, 1, 5])a[1].shapeOut[28]: torch.Size([4])
取出某一列
a[:,2] # 虽说取出的是第2列,但还是以行的形式显示Out[26]: tensor([3, 1, 7])a[:,2].shapeOut[27]: torch.Size([3])
一次性取出多行
取出连续的多行
——有多种操作方式
######## scheme 1 ##########
a[[0, 1]] # 取出前两行
### 此时需注意,需要使用两个中括号,使用 a[0,1] 的格式取出的是 a 的第0行第1列的具体某个元素“tensor(2)”Out[7]:
tensor([[1, 2, 3, 4],[2, 3, 1, 5]])######## scheme 2 ###########
a[[range(2)]] # 取出前两行
### 当没有别的指示(如冒号等)时,默认对dim=0进行操作Out[8]:
tensor([[1, 2, 3, 4],[2, 3, 1, 5]])######### scheme 3 ###########
a[range(2)] # 也可以不使用两个中括号
Out[31]:
tensor([[1, 2, 3, 4],[2, 3, 1, 5]])
取出不连续的多行
如:取出第0行和第2行
a[[0,2]]Out[38]:
tensor([[1, 2, 3, 4],[5, 1, 7, 2]])
一次取出多列
取出连续的多列
——同样拥有多种方案
############## scheme 1 ###############
a[:,range(2)] # 取出前两列Out[31]:
tensor([[1, 2],[2, 3],[5, 1]])############ scheme 2 #################
a[:,[0,1]]
Out[32]:
tensor([[1, 2],[2, 3],[5, 1]])
取出不连续的多列
如取出第0列和第3列
a[:,[0,3]]Out[40]:
tensor([[1, 4],[2, 5],[5, 2]])
考虑三维torch
# 新建一个三维torch
b = torch.tensor([ [[1, 2, 3, 7], [4, 5, 6, 9]],
[[7, 8, 9, 2], [10, 11, 12, 3]],
[[13, 14, 15, 4], [16, 17, 18, 6]]])b.shapeOut[10]: torch.Size([3, 2, 4])bOut[11]:
tensor([[[ 1, 2, 3, 7],[ 4, 5, 6, 9]],[[ 7, 8, 9, 2],[10, 11, 12, 3]],[[13, 14, 15, 4],[16, 17, 18, 6]]])
此三维torch可视化如下:
取出三维torch的任意两行(means 在dim=0上操作)
取出连续的行
以前两行为例
b[range(2)] # 还是使用中括号Out[12]:
tensor([[[ 1, 2, 3, 7],[ 4, 5, 6, 9]],[[ 7, 8, 9, 2],[10, 11, 12, 3]]])b[range(2)].shapeOut[13]: torch.Size([2, 2, 4])b[range(2)] == b[[range(2)]] # 使用一个中括号还是两个中括号,都是一样的效果### 但是不能使用三个,shape 会变成 torch.Size([1,2,2,4])Out[34]:
tensor([[[True, True, True, True],[True, True, True, True]],[[True, True, True, True],[True, True, True, True]]])
取出不连续的行
如取出第0行和第2行
b[[0,2]]Out[42]:
tensor([[[ 1, 2, 3, 7],[ 4, 5, 6, 9]],[[13, 14, 15, 4],[16, 17, 18, 6]]])
取出任意列
取出连续的列 & 取出任意列
######### 取出中间维度(dim=1)的前一列
b[:,range(1)].shape Out[19]: torch.Size([3, 1, 4])b[:,range(1)]Out[25]:
tensor([[[ 1, 2, 3, 7]],[[ 7, 8, 9, 2]],[[13, 14, 15, 4]]])############# 取出前两列
b[:,range(2)] Out[43]:
tensor([[[ 1, 2, 3, 7],[ 4, 5, 6, 9]],[[ 7, 8, 9, 2],[10, 11, 12, 3]],[[13, 14, 15, 4],[16, 17, 18, 6]]])b[:,range(2)].shapeOut[44]: torch.Size([3, 2, 4])############## 取出任意一列
b[:,1]Out[46]:
tensor([[ 4, 5, 6, 9],[10, 11, 12, 3],[16, 17, 18, 6]])
取出任意页(dim=2)
取出前n页
##################### 取出前两页
b[:,:,range(2)]Out[47]:
tensor([[[ 1, 2],[ 4, 5]],[[ 7, 8],[10, 11]],[[13, 14],[16, 17]]])b[:,:,range(2)].shapeOut[48]: torch.Size([3, 2, 2])
取出任意页
如取出第0页,第2页和第3页
b[:,:,[0,2,3]]Out[49]:
tensor([[[ 1, 3, 7],[ 4, 6, 9]],[[ 7, 9, 2],[10, 12, 3]],[[13, 15, 4],[16, 18, 6]]])b[:,:,[0,2,3]].shapeOut[50]: torch.Size([3, 2, 3])
else:取出dim=0/dim=1/dim=2的任意元素操作
待补充