当前位置:网站首页>pytorch学习08:拼接与拆分
pytorch学习08:拼接与拆分
2022-06-21 23:47:00 【HMTT】
cat
矩阵拼接操作,将符合要求的不同矩阵在某一维度上进行拼接。
cat要求进行拼接的矩阵在非拼接维度上完全相同。
import torch
a1 = torch.rand(4, 3, 32, 32)
a2 = torch.rand(5, 3, 32, 32)
a3 = torch.rand(4, 1, 32, 32)
a4 = torch.rand(4, 3, 16, 32)
# 要求其他维度必须相同
print("torch.cat([a1, a2], dim=0): ",
torch.cat([a1, a2], dim=0).shape)
print("torch.cat([a1, a3], dim=1): ",
torch.cat([a1, a3], dim=1).shape)
print("torch.cat([a1, a4], dim=2): ",
torch.cat([a1, a4], dim=2).shape)

- 只有需要拼接的维度可以不同,其他维度必须相同
stack
矩阵堆叠,将若干维度完全相同的句子在某一维度上进行堆叠。
stack操作会在指定维度前增加一个维度,然后让后面的维度堆叠起来。
要求堆叠的矩阵维度完全相同。
import torch
a1 = torch.rand(4, 3, 16, 32)
a2 = torch.rand(4, 3, 16, 32)
print("torch.cat([a1, a2], dim=2): ",
torch.cat([a1, a2], dim=2).shape)
# stack会新插入一个维度,使其他维度保持不变
# stack要求两个矩阵维度完全一致
print("torch.stack([a1, a2], dim=2): ",
torch.stack([a1, a2], dim=2).shape)
a = torch.rand(32, 8)
b = torch.rand(32, 8)
print("torch.stack([a, b], dim=0): ",
torch.stack([a, b], dim=0).shape)
# 可用于将两个相同的表整合起来,且会保持两张表的独立性

- 要求两个矩阵维度完全相同
split
split可对矩阵按长度进行分割。
split有两种分割模式:
- 输入列表:会根据列表中的值进行分割,要求列表值之和等于被分割维度元素个数。
- 输入数值:会根据数值进行分割,会分割出若干个等长的矩阵,当剩余长度小于指定长度时,也会将加入返回列表。要求输入数值小于等于被分割维度元素个数。
import torch
a = torch.rand(32, 8)
c = torch.stack([a, a, a, a, a, a], dim=0)
print("c.shape: ", c.shape)
def showSplits(ms):
for index, m in enumerate(ms):
print("第{}个tensor的shape为 {}".format(
index + 1, m.shape
))
print()
# 自定义拆分,列表中每个数表示拆分后目标维度的长度
# 要求列表元素和等于目标维度拆分前的长度
ms = c.split([1, 2, 1, 1, 1], dim=0)
print("c.split([1, 2, 1, 1, 1], dim=0):")
showSplits(ms)
# 按长度拆分,输入的值表示每个拆分后的tensor的目标维度的长度
# 最后一个tensor如果长度不够也会返回
ms = c.split(4, dim=0)
print("c.split(4, dim=0):")
showSplits(ms)
ms = c.split(8, dim=1)
print("c.split(8, dim=1):")
showSplits(ms)
ms = c.split(32, dim=1)
print("c.split(32, dim=1):")
showSplits(ms)

chunck
chunck可按照数量对矩阵进行分割,将矩阵分割为指定个数。
若矩阵可被均匀分割,则返回指定个数的相同维度矩阵。
若不可被均匀分割,则返回的最后一个矩阵维度会较低。
import torch
a = torch.rand(32, 8)
c = torch.stack([a, a, a, a, a, a], dim=0)
print("c.shape: ", c.shape)
def showSplits(ms):
for index, m in enumerate(ms):
print("第{}个tensor的shape为 {}".format(
index + 1, m.shape
))
print()
# 按个数拆分,输入拆分的个数和维度
ms = c.chunk(2, dim=0)
print("c.chunk(2, dim=0):")
showSplits(ms)
ms = c.chunk(3, dim=0)
print("c.chunk(3, dim=0):")
showSplits(ms)
ms = c.chunk(5, dim=1)
print("c.chunk(5, dim=0):")
showSplits(ms)

边栏推荐
- Overview of embedded system and embedded design software
- 汇编语言范例
- TCP/IP--路由选择
- eslint:错误
- [sword finger offer] 43 Number of occurrences of 1 in integers 1 to n
- 【Try to Hack】nmap
- Win10使用用户初始密码,连接Win Server失败
- Opérations de bits bits et
- The interviewer asked me how the order ID was generated? Is it not MySQL self incrementing primary key?
- Arm assembles DCB, DCW, DCD and DCQ parsing
猜你喜欢

Importance of data governance

合理选择液压滑环密封间隙的重要性

前加后加探索和函数调用探索

唐太宗把微服务的“心跳机制”玩到了极致!

Getting started with go web programming: validators

If a programmer goes to prison, will he be assigned to write code?

It took 2 hours to build an Internet of things project, which is worth~

你有一个机会,这里有一个舞台

过孔式导电滑环怎么用

对面积的曲面积分中dS与dxdy的转换
随机推荐
徽商期货开户可靠吗?新手如何安全开户?
redis精讲系列介绍七-过期策略
Brève idée et cas simple de l'ajustement JVM - mécanisme de garantie de l'allocation de l'espace dans les temps anciens
在徽商期货开户做期货安全吗?
12 BeautifulSoup类的初始化
Mathematical knowledge: sum of divisors - divisors
Assembly language example
ARM 的 缓存一致性
DOM 节点
leetcode 279. Perfect Squares 完全平方数(中等)
[wechat applet] obtain the current geographic latitude and IP address
记录一次小jsp的bug
The difference between overloading and overriding
Record a small JSP bug
积分体系运营汇中,用户的哪些行为可以获得积分
Hongmeng OS learning (rotation chart, list, icon)
Qt之自制MP3播放器
What are the current mainstream overseas social media? Could you give me a brief introduction?
eslint:错误
数据魔术师告诉你整数规划COPT5.0离CPLEX还有多远?