網站首頁 編程語言 正文
持續更新一些常用的Tensor操作,比如List,Numpy,Tensor之間的轉換,Tensor的拼接,維度的變換等操作。
其它Tensor操作如 einsum等見:待更新。
用到兩個函數:
torch.cat
torch.stack
一、List Tensor轉Tensor (torch.cat)
// An highlighted block >>> t1 = torch.FloatTensor([[1,2],[5,6]]) >>> t2 = torch.FloatTensor([[3,4],[7,8]]) >>> l = [] >>> l.append(t1) >>> l.append(t2) >>> ta = torch.cat(l,dim=0) >>> ta = torch.cat(l,dim=0).reshape(2,2,2) >>> tb = torch.cat(l,dim=1).reshape(2,2,2) >>> ta tensor([[[1., 2.], [5., 6.]], [[3., 4.], [7., 8.]]]) >>> tb tensor([[[1., 2.], [3., 4.]], [[5., 6.], [7., 8.]]])
高維tensor
** 如果理解了2D to 3DTensor,以此類推,不難理解3D to 4D,看下面代碼即可明白:**
>>> t1 = torch.range(1,8).reshape(2,2,2) >>> t2 = torch.range(11,18).reshape(2,2,2) >>> l = [] >>> l.append(t1) >>> l.append(t2) >>> torch.cat(l,dim=2).reshape(2,2,2,2) tensor([[[[ 1., 2.], [11., 12.]], [[ 3., 4.], [13., 14.]]], [[[ 5., 6.], [15., 16.]], [[ 7., 8.], [17., 18.]]]]) >>> torch.cat(l,dim=1).reshape(2,2,2,2) tensor([[[[ 1., 2.], [ 3., 4.]], [[11., 12.], [13., 14.]]], [[[ 5., 6.], [ 7., 8.]], [[15., 16.], [17., 18.]]]]) >>> torch.cat(l,dim=0).reshape(2,2,2,2) tensor([[[[ 1., 2.], [ 3., 4.]], [[ 5., 6.], [ 7., 8.]]], [[[11., 12.], [13., 14.]], [[15., 16.], [17., 18.]]]])
二、List Tensor轉Tensor (torch.stack)
代碼:
import torch t1 = torch.FloatTensor([[1,2],[5,6]]) t2 = torch.FloatTensor([[3,4],[7,8]]) l = [t1, t2] t3 = torch.stack(l, dim=2) print(t3.shape) print(t3) ## output: ## torch.Size([2, 2, 2]) ## tensor([[[1., 3.], ## [2., 4.]], ## [[5., 7.], ## [6., 8.]]])
原文鏈接:https://blog.csdn.net/bagba/article/details/107047202
相關推薦
- 2022-05-31 python庫pydantic的簡易入門教程_python
- 2022-01-29 寶塔部署Yii框架多個商城項目,隊列問題“服務測試失敗,請檢查服務是否正常運行”
- 2022-09-15 python接口測試對修改密碼接口進行壓測_python
- 2022-09-17 Python高效處理大文件的方法詳解_python
- 2022-11-30 Docker容器數據卷的使用教程_docker
- 2022-03-30 Android使用Retrofit上傳文件功能_Android
- 2023-05-31 Pandas多個條件(AND,OR,NOT)中提取行_python
- 2022-07-31 oracle定時任務定時無效的原因分析與解決_oracle
- 最近更新
-
- 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同步修改后的遠程分支