從二維torch開始
新建torch
import torch
# 新建一個二維 torch
a = torch.tensor ( [[1,2,3,4],
[2,3,1,5],
[5,1,7,2]])
a.shape
Out[5]: torch.Size([3, 4])
取出某一行
a[1] # 取出第1行(從0行開始)
Out[6]: tensor([2, 3, 1, 5])
a[1].shape
Out[28]: torch.Size([4])
取出某一列
a[:,2] # 雖說取出的是第2列,但還是以行的形式顯示
Out[26]: tensor([3, 1, 7])
a[:,2].shape
Out[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)]] # 取出前兩行
### 當沒有別的指示(如冒號等)時,默認對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.shape
Out[10]: torch.Size([3, 2, 4])
b
Out[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)].shape
Out[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)].shape
Out[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)].shape
Out[48]: torch.Size([3, 2, 2])
取出任意頁
如取出第0頁,第2頁和第3頁文章來源:http://www.zghlxwxcb.cn/news/detail-429858.html
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]].shape
Out[50]: torch.Size([3, 2, 3])
else:取出dim=0/dim=1/dim=2的任意元素操作
待補充文章來源地址http://www.zghlxwxcb.cn/news/detail-429858.html
到了這里,關于筆記:對多維torch進行任意維度的多“行”操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!