網(wǎng)站首頁 編程語言 正文
Pytorch實(shí)現(xiàn)List?Tensor轉(zhuǎn)Tensor,reshape拼接等操作_python
作者:Bagba ? 更新時(shí)間: 2022-12-06 編程語言持續(xù)更新一些常用的Tensor操作,比如List,Numpy,Tensor之間的轉(zhuǎn)換,Tensor的拼接,維度的變換等操作。
其它Tensor操作如 einsum等見:待更新。
用到兩個(gè)函數(shù):
torch.cat
torch.stack
一、List Tensor轉(zhuǎn)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轉(zhuǎn)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
相關(guān)推薦
- 2022-09-24 C++實(shí)現(xiàn)并優(yōu)化異常系統(tǒng)_C 語言
- 2022-07-22 SpringBoot允許跨域訪問配置
- 2023-01-20 Django如何實(shí)現(xiàn)RBAC權(quán)限管理_python
- 2023-01-06 Linux下find?命令的?7?種用法_linux shell
- 2023-04-06 C++深淺拷貝及簡易string類實(shí)現(xiàn)方式_C 語言
- 2022-02-22 如何利用nginx做代理緩存淺析_nginx
- 2022-11-10 Linux實(shí)現(xiàn)壓縮文件的生成與查看的常用命令總結(jié)_linux shell
- 2022-10-17 一文教會你用nginx+uwsgi部署自己的django項(xiàng)目_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支