【无标题】PyTorch 常用算子说明

1.增加维度

print(a.unsqueeze(0).shape) # 在0号维度位置插入一个维度

print(a.unsqueeze(-1).shape) # 在最后插入一个维度

print(a.unsqueeze(3).shape) # 在3号维度位置插入一个维度

2.删减维度

a = torch.Tensor(1, 4, 1, 9)

print(a.squeeze().shape) # 能删除的都删除掉

print(a.squeeze(0).shape) # 尝试删除0号维度,ok

3.维度扩展(expand)

b = torch.rand(32)

f = torch.rand(4, 32, 14, 14)

先进行维度增加

b = b.unsqueeze(1).unsqueeze(2).unsqueeze(0)

print(b.shape)

再进行维度扩展

b = b.expand(4, -1, 14, 14) # -1表示这个维度保持不变,这里写32也可以

print(b.shape)

输出:

torch.Size([1, 32, 1, 1])

torch.Size([4, 32, 14, 14])

4.维度重复(repeat)

print(b.shape)

维度重复,32这里不想进行重复,所以就相当于"重复至1次"

b = b.repeat(4, 1, 14, 14)

print(b.shape)

输出:

torch.Size([1, 32, 1, 1])

torch.Size([4, 32, 14, 14])

5.转置

只适用于dim=2的Tensor。

c = torch.Tensor(2, 4)

print(c.t().shape)

输出:

torch.Size([4, 2])

  1. 维度交换

d = torch.Tensor(6, 3, 1, 2)

print(d.transpose(1, 3).contiguous().shape) # 1号维度和3号维度交换

输出:

torch.Size([6, 2, 1, 3])

7.permute

h = torch.rand(4, 3, 6, 7)

print(h.permute(0, 2, 3, 1).shape)

输出:

torch.Size([4, 6, 7, 3])

8.gather

1)input:输入

2)dim:维度,常用的为0和1

3)index:索引位置

a=t.arange(0,16).view(4,4)

print(a)

index_1=t.LongTensor([[3,2,1,0]])

b=a.gather(0,index_1)

print(b)

index_2=t.LongTensor([[0,1,2,3]]).t()#tensor转置操作:(a)T=a.t()

c=a.gather(1,index_2)

print(c)

outout输出:

tensor([[ 0, 1, 2, 3],

4, 5, 6, 7\], \[ 8, 9, 10, 11\], \[12, 13, 14, 15\]\]) tensor(\[\[12, 9, 6, 3\]\]) tensor(\[\[ 0\], \[ 5\], \[10\], \[15\]\]) 在gather中,我们是通过index对input进行索引把对应的数据提取出来的,而dim决定了索引的方式。 **9.Chunk** torch.chunk(tensor, chunks, dim=0) 在给定维度(轴)上将输入张量进行分块儿 直接用上面的数据来举个例子: l, m, n = x.chunk(3, 0) # 在 0 维上拆分成 3 份 l.size(), m.size(), n.size() (torch.Size(\[1, 10, 6\]), torch.Size(\[1, 10, 6\]), torch.Size(\[1, 10, 6\])) u, v = x.chunk(2, 0) # 在 0 维上拆分成 2 份 u.size(), v.size() (torch.Size(\[2, 10, 6\]), torch.Size(\[1, 10, 6\])) **10.Stack** 合并新增(stack) stack需要保证两个Tensor的shape是一致的。 c = torch.rand(4, 3, 32, 32) d = torch.rand(4, 3, 32, 32) print(torch.stack(\[c, d\], dim=2).shape) print(torch.stack(\[c, d\], dim=0).shape) 运行结果: torch.Size(\[4, 3, 2, 32, 32\]) torch.Size(\[2, 4, 3, 32, 32\]) **11.View** Pytorch中的view函数主要用于Tensor维度的重构,即返回一个有相同数据但不同维度的Tensor。 a3 = torch.tensor(\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24\]) a4 = a3.view(4, -1) a5 = a3.view(2, 3, -1) 输出: #a3 tensor(\[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24\]) #a4 tensor(\[\[ 1, 2, 3, 4, 5, 6\], \[ 7, 8, 9, 10, 11, 12\], \[13, 14, 15, 16, 17, 18\], \[19, 20, 21, 22, 23, 24\]\]) #a5 tensor(\[\[\[ 1, 2, 3, 4\], \[ 5, 6, 7, 8\], \[ 9, 10, 11, 12\]\], \[\[13, 14, 15, 16\], \[17, 18, 19, 20\], \[21, 22, 23, 24\]\]\]) **12.reshape** 返回与 input张量数据大小一样、给定 shape的张量。如果可能,返回的是input 张量的视图,否则返回的是其拷贝。 a1 = torch.tensor(\[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\]) a2 = torch.reshape(a1, (3, 4)) print(a1.shape) print(a1) print(a2.shape) print(a2) 运行结果: torch.Size(\[12\]) tensor(\[ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12\]) torch.Size(\[3, 4\]) tensor(\[\[ 1, 2, 3, 4\], \[ 5, 6, 7, 8\], \[ 9, 10, 11, 12\]\]) 同view函数,也可以自动推断维度:a4 = torch.reshape(a1, (-1, 6))

相关推荐
一个无名的炼丹师9 分钟前
多模态RAG系统进阶:从零掌握olmOCR与MinerU的部署与应用
python·大模型·ocr·多模态·rag
u01092727123 分钟前
使用XGBoost赢得Kaggle比赛
jvm·数据库·python
MediaTea30 分钟前
<span class=“js_title_inner“>Python:实例对象</span>
开发语言·前端·javascript·python·ecmascript
feasibility.1 小时前
多模态模型Qwen3-VL在Llama-Factory中断LoRA微调训练+测试+导出+部署全流程--以具身智能数据集open-eqa为例
人工智能·python·大模型·nlp·llama·多模态·具身智能
我需要一个支点1 小时前
douyin无水印视频下载
爬虫·python
喵手1 小时前
Python爬虫实战:采集各大会展平台的展会名称、举办时间、展馆地点、主办方、行业分类等结构化数据(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集大会展平台信息·展会名称举办时间展馆地址·采集数据csv/json导出
编码者卢布1 小时前
【Azure APIM】如何实现对经过APIM并到达后端服务请求的全链路追踪呢?
python·flask·azure
0思必得01 小时前
[Web自动化] Selenium执行JavaScript语句
前端·javascript·爬虫·python·selenium·自动化
焱童鞋1 小时前
解决 MeteoInfoLab 3.9.11 中 contourfm 导致的 ArrayIndexOutOfBoundsException
开发语言·python
封奚泽优1 小时前
化学配对记忆游戏:用Python和Pygame打造趣味化学学习工具
python·pygame