PyTorch張量拼接、切分、索引的實(shí)現(xiàn)
一、張量拼接與切分
1.1 torch.cat
功能:將張量按維度dim 進(jìn)行拼接
tensors : 張量序列
dim: 要拼接的維度
t = torch.ones((2, 3))
t_0 = torch.cat([t, t], dim=0)
t_1 = torch.cat([t, t, t], dim=1)
print("t_0:{} shape:{}\nt_1:{} shape:{}".format(t_0, t_0.shape, t_1, t_1.shape))
t_0:tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]) shape:torch.Size([4, 3])
t_1:tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.]]) shape:torch.Size([2, 9])
(2,3) -> (2,6)
這里的dim維度與axis相同,0代表列,1代表行。
1.2 torch.stack
功能:在新創(chuàng)建的維度 dim 上進(jìn)行拼接(會(huì)拓寬原有的張量維度)
- tensors:張量序列
- dim:要拼接的維度

t = torch.ones((2, 3))
t_stack = torch.stack([t, t, t], dim=2)
print("\nt_stack:{} shape:{}".format(t_stack, t_stack.shape))可見,它在新的維度上進(jìn)行了拼接。
參數(shù)[t, t, t]的意思就是在第n個(gè)維度上拼接成這個(gè)樣子。
t_stack:tensor([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]]) shape:torch.Size([2, 3, 3])
# 在第二維度上進(jìn)行了拼接
Process finished with exit code 01.3 torch.chunk

功能:將張量按維度 dim 進(jìn)行平均切分
返回值:張量列表
注意事項(xiàng):若不能整除,最后一份張量小于其他張量。
- input : 要切分的張量
- chunks 要切分的份數(shù)
- dim 要切分的維度
# cut into 3
a = torch.ones((2, 7)) # 7
list_of_tensors = torch.chunk(a, dim=1, chunks=3) # 3
for idx, t in enumerate(list_of_tensors):
print("第{}個(gè)張量:{}, shape is {}".format(idx+1, t, t.shape))
可知,切分是7/3向上取整,每份是3,最后剩下的維度直接輸出即可。
第1個(gè)張量:tensor([[1., 1., 1.],
[1., 1., 1.]]), shape is torch.Size([2, 3])
第2個(gè)張量:tensor([[1., 1., 1.],
[1., 1., 1.]]), shape is torch.Size([2, 3])
第3個(gè)張量:tensor([[1.],
[1.]]), shape is torch.Size([2, 1])
1.4 torch.split
torch.split(Tensor, split_size_or_sections, dim)
功能:將張量按維度 dim 進(jìn)行切分
返回值:張量列表
- tensor : 要切分的張量
- split_size_or_sections 為 int 時(shí),表示
每一份的長(zhǎng)度;為 list 時(shí),按 list 元素切分 - dim 要切分的維度
t = torch.ones((2, 5))
list_of_tensors = torch.split(t, [2, 1, 1], dim=1) # [2 , 1, 2]
for idx, t in enumerate(list_of_tensors):
print("第{}個(gè)張量:{}, shape is {}".format(idx+1, t, t.shape))
是按照指定長(zhǎng)度list進(jìn)行切分的。注意list中長(zhǎng)度總和必須為原張量在改維度的大小,不然會(huì)報(bào)錯(cuò)。
第1個(gè)張量:tensor([[1., 1., 1.],
[1., 1., 1.]]), shape is torch.Size([2, 3])
第2個(gè)張量:tensor([[1., 1., 1.],
[1., 1., 1.]]), shape is torch.Size([2, 3])
第3個(gè)張量:tensor([[1.],
[1.]]), shape is torch.Size([2, 1])
二、張量索引
2.1 torch.index_select
torch.index_select(input, dim, index, out=None)
功能:在維度dim 上,按 index 索引數(shù)據(jù)
返回值:依index 索引數(shù)據(jù)拼接的張量
- input : 要索引的張量
- dim 要索引的維度
- index 要索引數(shù)據(jù)的序號(hào)
t = torch.randint(0, 9, size=(3, 3))
idx = torch.tensor([0, 2], dtype=torch.long) # if float will report an error
t_select = torch.index_select(t, dim=0, index=idx)
print(idx)
print("t:\n{}\nt_select:\n{}".format(t, t_select))
可見idx是一個(gè)存儲(chǔ)序號(hào)的張量,而torch.index_select通過該張量索引原tensor并且拼接返回。
tensor([0, 2])
t:
tensor([[4, 5, 0],
[5, 7, 1],
[2, 5, 8]])
t_select:
tensor([[4, 5, 0],
[2, 5, 8]])
2.2 torch.masked_select
功能:按mask 中的 True 進(jìn)行索引
返回值:一維張量(無法確定true的個(gè)數(shù),因此也就無法顯示原來的形狀,因此這里返回一維張量)
- input : 要索引的張量
- mask 與 input 同形狀的布爾類型張量
t = torch.randint(0, 9, size=(3, 3))
mask = t.le(5) # ge is mean greater than or equal/ gt: greater than le lt
t_select = torch.masked_select(t, mask)
print("t:\n{}\nmask:\n{}\nt_select:\n{} ".format(t, mask, t_select))
通過掩碼來索引。
tensor([[4, 5, 0],
[5, 7, 1],
[2, 5, 8]])
mask:
tensor([[ True, True, True],
[ True, False, True],
[ True, True, False]])
t_select:
tensor([4, 5, 0, 5, 1, 2, 5])
Process finished with exit code 0到此這篇關(guān)于PyTorch張量拼接、切分、索引的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)PyTorch張量拼接切分索引內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Pytorch實(shí)現(xiàn)張量的創(chuàng)建與使用方法
- Python Pytorch深度學(xué)習(xí)之Tensors張量
- 人工智能學(xué)習(xí)Pytorch張量數(shù)據(jù)類型示例詳解
- python機(jī)器學(xué)習(xí)pytorch?張量基礎(chǔ)教程
- Pytorch創(chuàng)建張量的四種方法
- Pytorch數(shù)據(jù)類型Tensor張量操作的實(shí)現(xiàn)
- Pytorch創(chuàng)建隨機(jī)值張量的過程詳解
- pytorch張量和numpy數(shù)組相互轉(zhuǎn)換
- PyTorch的張量tensor和自動(dòng)求導(dǎo)autograd詳解
相關(guān)文章
python 實(shí)現(xiàn)方陣的對(duì)角線遍歷示例
今天小編就為大家分享一篇python 實(shí)現(xiàn)方陣的對(duì)角線遍歷示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練詳解
目標(biāo)檢測(cè)是計(jì)算機(jī)視覺上的一個(gè)重要任務(wù),下面這篇文章主要給大家介紹了關(guān)于Yolov5訓(xùn)練意外中斷后如何接續(xù)訓(xùn)練的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
Python 批量操作設(shè)備的實(shí)現(xiàn)步驟
本文將結(jié)合實(shí)例代碼,介紹Python 批量操作設(shè)備的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
python字典dict中常用內(nèi)置函數(shù)的使用
本文主要介紹了python字典dict中常用內(nèi)置函數(shù)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Python與C++中梯度方向直方圖的實(shí)現(xiàn)
在學(xué)習(xí)HOG特征的時(shí)候,發(fā)現(xiàn)一片英文文章講得淺顯易懂。因此翻譯在這里學(xué)習(xí),感興趣的朋友快來看看吧2022-03-03
Python unittest單元測(cè)試框架及斷言方法
這篇文章主要介紹了Python unittest單元測(cè)試框架及斷言方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04

