網站首頁 編程語言 正文
關于tensor.repeat()的使用
考慮到很多人在學習這個函數,我想在這里提 一個建議:
強烈推薦 使用 einops 模塊中的 repeat() 函數 替代 tensor.repeat()!
它可以擺脫 tensor.repeat() 參數的神秘主義。
einops 模塊文檔地址:https://nbviewer.jupyter.org/github/arogozhnikov/einops/blob/master/docs/1-einops-basics.ipynb
學習 tensor.repeat() 這個函數的功能的時候,最好還是要觀察所得到的 結果的維度。
不多說,看代碼:
>>> import torch
>>>
>>> # 定義一個 33x55 張量
>>> a = torch.randn(33, 55)
>>> a.size()
torch.Size([33, 55])
>>>
>>> # 下面開始嘗試 repeat 函數在不同參數情況下的效果
>>> 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()的參數的個數,不能少于被操作的張量的維度的個數,
>>> # 下面是一些錯誤示例
>>> 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
>>>
>>> # 定義一個3維的張量,然后展示前面提到的那個錯誤
>>> 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()的簡單用法
相當于手動實現廣播機制,即沿著給定的維度對tensor進行重復:
比如說對下面x的第1個通道復制三次,其余通道保持不變:
import torch
x = torch.randn(1, 3, 224, 224)
y = x.repeat(3, 1, 1, 1)
print(x.shape)
print(y.shape)
結果為:
torch.Size([1, 3, 224, 224])
torch.Size([3, 3, 224, 224])
這個在復制batch的時候用的比較多,上面的情況就相當于batch為1的3×224×224特征圖復制成了batch為3
原文鏈接:https://blog.csdn.net/qq_29695701/article/details/89763168
相關推薦
- 2023-01-27 React?useEffect的理解與使用_React
- 2022-08-02 C#?HttpClient?Post參數同時上傳文件的實現_C#教程
- 2023-02-01 Python中列表遍歷使用range和enumerate的區別講解_python
- 2022-06-22 Android中TextView動態設置縮進距離的方法_Android
- 2023-02-25 Golang內存泄漏場景以及解決方案詳析_Golang
- 2023-01-01 c語言之如何求e的近似值_C 語言
- 2024-02-29 UNI-APP中點擊事件多重響應問題的解決,list列表項item和列表項item中按鈕的點擊事件沖
- 2023-02-10 C/C++開發中extern的一些使用注意事項_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支