当前位置:网站首页>Pytoch squeeze() unsqueeze() usage
Pytoch squeeze() unsqueeze() usage
2022-07-26 21:46:00 【Shadow follows】
brief introduction
torch.squeeze(input, dim=None, out=None): Compress the dimension of data , Remove the dimension as 1 Dimension of .
squeeze The functionality : Remove size by 1 Dimensions , Including rows and columns . When the dimension is greater than or equal to 2 when ,squeeze() No use .
squeeze(0): If the value of the first dimension is 1 Remove the first dimension , for example a.squeeze(0),a by torch.tensor() Format tensor .
squeeze(1): If the value of the second dimension is 1 Remove the second dimension
squeeze(-1): Remove the final dimension value as 1 Dimensions
torch.unsqueeze (input, dim=None, out=None): Expand the dimension of data , Upgrade dimension .
The format used can be torch.unsqueeze(x, 0), It can also be x.unsqueeze(0).
The sample code
a = torch.Tensor(1, 3)
print(a)
print(a.squeeze(0))
print(a.squeeze(1))
b = torch.Tensor(2, 3)
print(b)
print(b.squeeze(0))
print(b.squeeze(1))
c = torch.Tensor(3, 1)
print(c)
print(c.squeeze(0))
print(c.squeeze(1))
x = torch.tensor([1, 2, 3, 4])
print(x)
print(torch.unsqueeze(x, 0))
print(torch.unsqueeze(x, 1))
Process analysis
Define tensor a, by 2 dimension , The first dimension is 1 Elements , The second dimension is 3 Elements .
Output :tensor([[2.6994e-30, 2.4164e-13, 1.8392e-13]])
adopt a.squeeze(0) Reduce the dimension of the first dimension , At this time, the first dimension has 1 Elements , Dimensionality reduction , The first dimension disappears , The second dimension automatically becomes the first dimension with three elements , And a comparison , That is, it disappears “[]”.
Output :tensor([2.6994e-30, 2.4164e-13, 1.8392e-13])
adopt a.squeeze(1) Reduce the dimension of the second dimension , At this time, the first dimension has 3 Elements , Dimensionality cannot be reduced , Do not operate , Output and a identical .
Output :tensor([[2.6994e-30, 2.4164e-13, 1.8392e-13]])
Define tensor b, by 2 dimension , The first dimension is 2 Elements , The second dimension is 3 Elements .
First of all 、 Two dimensions cannot be reduced , Because the three outputs are the same .
Output :
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
Define tensor c, by 2 dimension , The first dimension is 3 Elements , The second dimension is 1 Elements .
Output :
tensor([[0.0000e+00],
[ nan],
[5.2781e-24]])
adopt c.squeeze(0) Reduce the dimension of the first dimension , At this time, the first dimension has 3 Elements , Dimensionality cannot be reduced , Do not operate , Output and c identical .
Output :
tensor([[0.0000e+00],
[ nan],
[5.2781e-24]])
adopt c.squeeze(1) Reduce the dimension of the second dimension , At this time, the second dimension has 1 Elements , Dimensionality reduction , The second dimension disappears , The value of the second dimension automatically enters the first dimension .
Output :tensor([0.0000e+00, nan, 5.2781e-24])
Define tensor x, by 1 dimension , The values are 1, 2, 3, 4.
Output :tensor([1, 2, 3, 4])
adopt x.unsqueeze(0) Add a dimension to the first dimension , Change the original tensor into 2 dimension , Dimension becomes (1, 4). And x comparison , That is to add a layer “[]”.
Output :tensor([[1, 2, 3, 4]])
adopt x.unsqueeze(1) Add a dimension to the second dimension , Change the original tensor into 2 dimension , Dimension becomes (4, 1).
Output :tensor([[1], [2], [3], [4]])
Running results
tensor([[2.6994e-30, 2.4164e-13, 1.8392e-13]])
tensor([2.6994e-30, 2.4164e-13, 1.8392e-13])
tensor([[2.6994e-30, 2.4164e-13, 1.8392e-13]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[0., 0., 0.],
[0., 0., 0.]])
tensor([[0.0000e+00],
[ nan],
[5.2781e-24]])
tensor([[0.0000e+00],
[ nan],
[5.2781e-24]])
tensor([0.0000e+00, nan, 5.2781e-24])
tensor([1, 2, 3, 4])
tensor([[1, 2, 3, 4]])
tensor([[1],
[2],
[3],
[4]])
边栏推荐
猜你喜欢

TypeScript中的类型断言

golang版本管理gvm

Selenium自动化测试面试题全家桶

Summer vacation daily question week 7:7.18 - 7.24

Thorough load balancing

新来个技术总监要我做一个 IP 属地功能~

Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management

京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?

Solution to the problem of sticking and unpacking TCP

(C语言)文件的基本操作
随机推荐
(C语言)文件的基本操作
Search eBay product API by keyword
FreeRTOS个人笔记-软件定时器
Pinduoduo gets search term recommendation API
Devops has been practiced for many years. What is the most painful thing?
From manual test to automatic test, it only took me a few months to double my salary
Filtering and distortion
Can I view the history in the "stealth" mode of the secure browser?
LeetCode 练习——剑指 Offer II 005. 单词长度的最大乘积
任正非再谈美国打压:活下去就是胜利,要战胜美国
Pbootcms一级栏目下的二级三级栏目高亮
缩放文本以适合元素内部
内容管理工具,用蓝色书签就足够
会用redis吗?那还不快来了解下redis protocol
TCP的粘包拆包问题解决方案
通过单击表头对表进行排序
我的sql没问题为什么还是这么慢|MySQL加锁规则
华为发布2025十大趋势:5G、机器人、AI等上榜
Attack and defense world ----- ics-07
Drag and drop table rows