当前位置:网站首页>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])
边栏推荐
- 第九章 实用建模技术
- MySQL主从复制配置详解
- How far can Volvo, which has lost its "safety brand" due to declining sales and marginalization of its brand?
- C语言-入门-语法-字符串(十一)
- Leetcode notes: Weekly contest 303
- 配置服务器环境
- Reentrantlock learning - lock release process
- 彻底关闭win10自动更新
- Sre person in charge of station B personally describes the multi activity disaster recovery construction after the 713 accident | takintalks share
- 如何保护电子商务网站免受网络攻击?
猜你喜欢

C # get local time / system time

This article explains in detail the five benefits that MES system brings to enterprises, with application scenarios

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

指标和标签是做什么的

C#获取本地时间/系统时间

Some time series modeling strategies (I)

I'm cool, so I'm here

MySQL - function and constraint commands

What is a server cluster? What are the advantages of overseas server clusters?

Network protocol: tcp/ip protocol
随机推荐
Mongodb stats counts the space occupied by the collection
After the exam on June 25, see how the new exam outline reviews PMP
[server data recovery] data recovery case of server storage shared folder loss
销量下滑,品牌边缘化,失去“安全牌”的沃尔沃,还能走多远?
Basic module and example pytorch learning
时空预测4-graph wavenet
MySQL learning notes -2. how to improve the query performance of SQL statements
当前占位,之后再写
TypeScript阶段学习
LeetCode简单题之数组能形成多少数对
2022g1 industrial boiler stoker certificate question bank and simulation examination
配置服务器环境
密码一致,总显示如下图
Microsoft silently donated $10000 to curl, which was not notified until half a year later
Introduce the difference between @getmapping and @postmapping in detail
Simulated 100 questions and simulated examination of refrigeration and air conditioning equipment operation examination in 2022
第九章 实用建模技术
自动化测试的使用场景
Advanced template (runner's notes)
JS uses readLine to realize terminal input data