PyTorch中關(guān)于tensor.repeat()的使用
關(guān)于tensor.repeat()的使用
考慮到很多人在學(xué)習(xí)這個(gè)函數(shù),我想在這里提 一個(gè)建議:
強(qiáng)烈推薦 使用 einops 模塊中的 repeat() 函數(shù) 替代 tensor.repeat()!
它可以擺脫 tensor.repeat() 參數(shù)的神秘主義。
einops 模塊文檔地址:https://nbviewer.jupyter.org/github/arogozhnikov/einops/blob/master/docs/1-einops-basics.ipynb
學(xué)習(xí) tensor.repeat() 這個(gè)函數(shù)的功能的時(shí)候,最好還是要觀察所得到的 結(jié)果的維度。
不多說(shuō),看代碼:
>>> import torch >>> >>> # 定義一個(gè) 33x55 張量 >>> a = torch.randn(33, 55) >>> a.size() torch.Size([33, 55]) >>> >>> # 下面開(kāi)始嘗試 repeat 函數(shù)在不同參數(shù)情況下的效果 >>> a.repeat(1,1).size() # 原始值:torch.Size([33, 55]) torch.Size([33, 55]) >>> >>> a.repeat(2,1).size() # 原始值:torch.Size([33, 55]) torch.Size([66, 55]) >>> >>> a.repeat(1,2).size() # 原始值:torch.Size([33, 55]) torch.Size([33, 110]) >>> >>> a.repeat(1,1,1).size() # 原始值:torch.Size([33, 55]) torch.Size([1, 33, 55]) >>> >>> a.repeat(2,1,1).size() # 原始值:torch.Size([33, 55]) torch.Size([2, 33, 55]) >>> >>> a.repeat(1,2,1).size() # 原始值:torch.Size([33, 55]) torch.Size([1, 66, 55]) >>> >>> a.repeat(1,1,2).size() # 原始值:torch.Size([33, 55]) torch.Size([1, 33, 110]) >>> >>> a.repeat(1,1,1,1).size() # 原始值:torch.Size([33, 55]) torch.Size([1, 1, 33, 55]) >>> >>> # ------------------ 割割 ------------------ >>> # repeat()的參數(shù)的個(gè)數(shù),不能少于被操作的張量的維度的個(gè)數(shù), >>> # 下面是一些錯(cuò)誤示例 >>> a.repeat(2).size() # 1D < 2D, error Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor >>> >>> # 定義一個(gè)3維的張量,然后展示前面提到的那個(gè)錯(cuò)誤 >>> b = torch.randn(5,6,7) >>> b.size() # 3D torch.Size([5, 6, 7]) >>> >>> b.repeat(2).size() # 1D < 3D, error Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor >>> >>> b.repeat(2,1).size() # 2D < 3D, error Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: Number of dimensions of repeat dims can not be smaller than number of dimensions of tensor >>> >>> b.repeat(2,1,1).size() # 3D = 3D, okay torch.Size([10, 6, 7]) >>>
Tensor.repeat()的簡(jiǎn)單用法
相當(dāng)于手動(dòng)實(shí)現(xiàn)廣播機(jī)制,即沿著給定的維度對(duì)tensor進(jìn)行重復(fù):
比如說(shuō)對(duì)下面x的第1個(gè)通道復(fù)制三次,其余通道保持不變:
import torch x = torch.randn(1, 3, 224, 224) y = x.repeat(3, 1, 1, 1) print(x.shape) print(y.shape)
結(jié)果為:
torch.Size([1, 3, 224, 224])
torch.Size([3, 3, 224, 224])
這個(gè)在復(fù)制batch的時(shí)候用的比較多,上面的情況就相當(dāng)于batch為1的3×224×224特征圖復(fù)制成了batch為3
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Django實(shí)現(xiàn)圖片文字同時(shí)提交的方法
這篇文章主要介紹了Django實(shí)現(xiàn)圖片文字同時(shí)提交的方法,較為詳細(xì)的分析了Django+jQuery實(shí)現(xiàn)圖片與文字同時(shí)提交的相關(guān)技巧,需要的朋友可以參考下2015-05-05關(guān)于如何把Python對(duì)象存儲(chǔ)為文件的方法詳解
本文將給大家介紹如何把Python對(duì)象存儲(chǔ)為文件的方法,pickle可以用二進(jìn)制表示并讀寫(xiě)python數(shù)據(jù),這個(gè)功能并不安全,如果把一個(gè)pickle暴露給別人,有被植入惡意程序的風(fēng)險(xiǎn),文中通過(guò)代碼給大家講解的非常詳細(xì),需要的朋友可以參考下2024-01-01基于Google的Python編碼規(guī)范標(biāo)準(zhǔn)
這篇文章主要介紹了基于Google的Python編碼規(guī)范標(biāo)準(zhǔn),其中包含了分號(hào),行長(zhǎng)度,括號(hào),縮進(jìn),空行,空格等基本符號(hào)的使用規(guī)則,有需要的朋友可以參考下2021-08-08Python3 實(shí)現(xiàn)隨機(jī)生成一組不重復(fù)數(shù)并按行寫(xiě)入文件
下面小編就為大家分享一篇Python3 實(shí)現(xiàn)隨機(jī)生成一組不重復(fù)數(shù)并按行寫(xiě)入文件的示例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Pandas數(shù)值排序 sort_values()的使用
本文主要介紹了Pandas數(shù)值排序 sort_values()的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07python使用PyFetion來(lái)發(fā)送短信的例子
這篇文章主要介紹了python使用PyFetion來(lái)發(fā)送短信的例子,需要的朋友可以參考下2014-04-04