貴港住房城鄉(xiāng)建設(shè)廳網(wǎng)站今日新聞聯(lián)播主要內(nèi)容
如何取出多維torch指定維度的指定“行”
- 從二維torch開始
- 新建torch
- 取出某一行
- 取出某一列
- 一次性取出多行
- 取出連續(xù)的多行
- 取出不連續(xù)的多行
- 一次取出多列
- 取出連續(xù)的多列
- 取出不連續(xù)的多列
- 考慮三維torch
- 取出三維torch的任意兩行(means 在dim=0上操作)
- 取出連續(xù)的行
- 取出不連續(xù)的行
- 取出任意列
- 取出連續(xù)的列 & 取出任意列
- 取出任意頁(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])
一次性取出多行
取出連續(xù)的多行
——有多種操作方式
######## 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)]] # 取出前兩行
### 當(dāng)沒有別的指示(如冒號等)時,默認對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]])
取出不連續(xù)的多行
如:取出第0行和第2行
a[[0,2]]Out[38]:
tensor([[1, 2, 3, 4],[5, 1, 7, 2]])
一次取出多列
取出連續(xù)的多列
——同樣擁有多種方案
############## 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]])
取出不連續(xù)的多列
如取出第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上操作)
取出連續(xù)的行
以前兩行為例
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]]])
取出不連續(xù)的行
如取出第0行和第2行
b[[0,2]]Out[42]:
tensor([[[ 1, 2, 3, 7],[ 4, 5, 6, 9]],[[13, 14, 15, 4],[16, 17, 18, 6]]])
取出任意列
取出連續(xù)的列 & 取出任意列
######### 取出中間維度(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的任意元素操作
待補充