網站首頁 編程語言 正文
關于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
相關推薦
- 2022-11-13 Git如何實現checkout遠程tag_相關技巧
- 2022-06-21 C#把文件上傳到服務器中的指定地址_C#教程
- 2022-09-05 內置指令、自定義指令(詳細)、全局指令與局部指令
- 2022-05-25 python?序列去重并保持原始順序操作_python
- 2022-09-17 docker安裝postgresql的圖文教程_docker
- 2022-04-01 k8s 學習 kubeadm join 超時報錯 : error uploading crisock
- 2022-11-03 tomcat的webapps目錄下的應用刪除部署詳解_Tomcat
- 2022-07-23 Python實現雙向鏈表基本操作_python
- 最近更新
-
- 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同步修改后的遠程分支