当前位置:网站首页>torch. Usage and comparison of unsqueeze() squeeze() expand() repeat()
torch. Usage and comparison of unsqueeze() squeeze() expand() repeat()
2022-07-26 19:22:00 【richardxp888】
1. torch.unsqueeze()
torch.unsqueeze(input, dim, out=None)
- effect : Expand dimensions , Returns a new tensor , Insert a dimension into the given location of the input 1
- Be careful : Return tensor and input tensor share memory , So changing the content of one will change the other .
If dim Negative , It will be transformed into dim+input.dim()+1 - Parameters :
tensor (Tensor)– Input tensordim (int)– Insert the index of the dimensionout (Tensor, optional)– The result tensor
2. torch.squeeze()
torch.squeeze(input, dim=None, out=None)
- effect : Dimension reduction , Will enter... In the tensor shape 1 To remove and return to . If the input is in the form of (A×1×B×1×C×1×D), So the output shape is : (A×B×C×D)
When given dim when , So squash is only on a given dimension . for example , The input shape is : (A×1×B), squeeze(input, 0) It's going to keep the tensor constant , Only with squeeze(input, 1), The shape will change (A×B). - Be careful : Return tensor and input tensor share memory , So changing the content of one will change the other .
- Parameters :
input (Tensor)– Input tensordim (int, optional)– If a given , be input It only squeezes in a given dimensionout (Tensor, optional)– Output tensor
3. torch.expand()
- effect :
expand()Function can broadcast tensors to new shapes . - Be careful : Only dimension values of 1 Expand the dimension of , And extended Tensor No new memory will be allocated , Just create a new view based on the original and return to ; Dimension values that do not need to be extended remain unchanged , Dimension for -1 That is, unchanged .
4. torch.repeat()
- effect : and
expand()Works in a similar way , All will tensor Broadcast to new shapes . - Be careful : Dimensions are not allowed -1,1 Is unchanged .
The following is an example of specific function usage .
a = torch.rand((2,1,3,1))
# torch.Size([2,1,3,1])
b = torch.unsqueeze(a, 1)
# torch.Size([2,1,1,3,1])
c = torch.unsqueeze(a, 0)
# torch.Size([1,2,1,3,1])
d = torch.unsqueeze(a, -1)
# torch.Size([2,1,3,1,1])
e = a.torch.expand((2,3,3,2))
# torch.Size([2,3,3,2])
f = a.torch.expand((2,-1,3,2))
# torch.Size([2,1,3,2])
g = a.torch.repeat((1,1,1,2))
# torch.Size([2,1,3,2])
h = torch.squeeze(a)
# torch.Size([2,3])
i = torch.squeeze(a, 0)
# torch.Size([2,1,3,1])
j = torch.squeeze(a, 1)
# torch.Size([2,3,1])
边栏推荐
- “蔚来杯“2022牛客暑期多校训练营2
- Utility website recommendations
- PMP每日一练 | 考试不迷路-7.26(包含敏捷+多选)
- MapReduce (II)
- Redis学习笔记-2.客户端的使用
- Verification palindrome string II of leetcode simple question
- Introduction to Seata
- JS uses readLine to realize terminal input data
- 【C语言实现】----动态/文件/静态通讯录
- Is it safe for CSC qiniu members to open preferential accounts? I don't know if it's the lowest Commission
猜你喜欢

MySQL日志介绍

高防服务器和高防IP的区别

How to solve the problem that win11 has been switched on after upgrading

AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘

2022 welder (elementary) operation certificate examination question bank and simulation examination

MapReduce (II)

Sentinel isolation and degradation

数据湖--概念、特征、架构与案例概述

“蔚来杯“2022牛客暑期多校训练营1

2022 tea master (intermediate) examination question simulation examination question bank and answers
随机推荐
What do indicators and labels do
JS uses readLine to realize terminal input data
TypeScript阶段学习
Customer cases | focus on process experience to help bank enterprise app iteration
EN 1504-6混凝土结构保护和修理用产品钢筋锚固—CE认证
To add a composite primary key
Typescript stage learning
2022 mobile crane driver test questions simulation test platform operation
Is it safe for CSC qiniu members to open preferential accounts? I don't know if it's the lowest Commission
Reentrantlock learning - lock release process
密码一致,总显示如下图
LeetCode简单题之验证回文字符串 Ⅱ
基础模块及算例#pytorch学习
Data Lake -- concept, characteristics, architecture and case overview
What aspects should be considered in the selection of MES system?
机器学习笔记 - 构建推荐系统(6) 用于协同过滤的 6 种自动编码器
NLP 学习之路
C#上位机开发—— 修改窗口图标和exe文件图标
conda+pytorch环境教程
【YOLOv5】--详细版训练自己的数据集 保姆级学习日志记录 手把手教程