torch.squeeze() dim=1 dim=-1 dim=2

对数据的维度进行压缩

使用方式:torch.squeeze(input, dim=None, out=None)

将输入张量形状中的1 去除并返回。 如果输入是形如(A×1×B×1×C×1×D),那么输出形状就为: (A×B×C×D)

当给定dim时,那么挤压操作只在给定维度上。例如,输入形状为: (A×1×B), squeeze(input, 0) 将会保持张量不变,只有用 squeeze(input, 1),形状会变成 (A×B)。

注意:

如果dim指定的维度的值为1

第一种情况

python 复制代码
import torch

x = torch.rand(2,1,1,3,1,4)
print('=======x=========')
print(x.shape)
out_1 = torch.squeeze(x, dim=1)
print('=======out_1=========')
print(out_1.shape)
# =======x=========
# torch.Size([2, 1, 1, 3, 1, 4])
# =======out_1=========
# torch.Size([2, 1, 3, 1, 4])

第二种情况

python 复制代码
x = torch.rand(1,2,1,1,3,1,4)
print('=======x=========')
print(x.shape)
out_2 = torch.squeeze(x, dim=1)
print('=======out_2=========')
print(out_2.shape)
# =======x=========
# torch.Size([1, 2, 1, 1, 3, 1, 4])
# =======out_2=========
# torch.Size([1, 2, 1, 1, 3, 1, 4])

第三种情况

python 复制代码
x = torch.rand(1,1,2,1,1,3,1,4)
print('=======x=========')
print(x.shape)
out_3 = torch.squeeze(x, dim=1)
print('=======out_3=========')
print(out_3.shape)
# =======x=========
# # torch.Size([1, 1, 2, 1, 1, 3, 1, 4])
# # =======out_3=========
# # torch.Size([1, 2, 1, 1, 3, 1, 4])

如果dim指定的维度的值为-1

第一种情况 如果dim指定的维度的值为-1

python 复制代码
import torch

x = torch.rand(2,1,1,3,1,4)
print('=======x=========')
print(x.shape)
out_1 = torch.squeeze(x, dim=-1)
print('=======out_1=========')
print(out_1.shape)
# =======x=========
# torch.Size([2, 1, 1, 3, 1, 4])
# =======out_1=========
# torch.Size([2, 1, 1, 3, 1, 4])

第二种情况 如果dim指定的维度的值为-1

python 复制代码
x = torch.rand(2,1,1,3,1,4,1)
print('=======x=========')
print(x.shape)
out_2 = torch.squeeze(x, dim=-1)
print('=======out_2=========')
print(out_2.shape)
# =======x=========
# torch.Size([2, 1, 1, 3, 1, 4, 1])
# =======out_2=========
# torch.Size([2, 1, 1, 3, 1, 4])

第三种情况 如果dim指定的维度的值为-1

python 复制代码
x = torch.rand(2,1,1,3,1,4,1,1)
print('=======x=========')
print(x.shape)
out_3 = torch.squeeze(x, dim=-1)
print('=======out_3=========')
print(out_3.shape)
# =======x=========
# torch.Size([2, 1, 1, 3, 1, 4, 1, 1])
# =======out_3=========
# torch.Size([2, 1, 1, 3, 1, 4, 1])

如果dim指定的维度的值为2

python 复制代码
import torch

x = torch.rand(2,1,3,1,4)
print('=======x=========')
print(x.shape)
out_1 = torch.squeeze(x, dim=2)
print('=======out_1=========')
print(out_1.shape)
# =======x=========
# torch.Size([2, 1, 3, 1, 4])
# =======out_1=========
# torch.Size([2, 1, 3, 1, 4])

x = torch.rand(2,1,1,3,1,4)
print('=======x=========')
print(x.shape)
out_2 = torch.squeeze(x, dim=2)
print('=======out_2=========')
print(out_2.shape)
# =======x=========
# torch.Size([2, 1, 1, 3, 1, 4])
# =======out_2=========
# torch.Size([2, 1, 3, 1, 4])


x = torch.rand(1,2,1,1,3,1,1,4)
print('=======x=========')
print(x.shape)
out_3 = torch.squeeze(x, dim=2)
print('=======out_3=========')
print(out_3.shape)
# =======x=========
# torch.Size([1, 2, 1, 1, 3, 1, 1, 4])
# =======out_3=========
# torch.Size([1, 2, 1, 3, 1, 1, 4])
相关推荐
开MINI的工科男23 分钟前
深蓝学院-- 量产自动驾驶中的规划控制算法 小鹏
人工智能·机器学习·自动驾驶
AI大模型知识分享2 小时前
Prompt最佳实践|如何用参考文本让ChatGPT答案更精准?
人工智能·深度学习·机器学习·chatgpt·prompt·gpt-3
张人玉3 小时前
人工智能——猴子摘香蕉问题
人工智能
草莓屁屁我不吃3 小时前
Siri因ChatGPT-4o升级:我们的个人信息还安全吗?
人工智能·安全·chatgpt·chatgpt-4o
小言从不摸鱼4 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
AI科研视界4 小时前
ChatGPT+2:修订初始AI安全性和超级智能假设
人工智能·chatgpt
霍格沃兹测试开发学社测试人社区4 小时前
人工智能 | 基于ChatGPT开发人工智能服务平台
软件测试·人工智能·测试开发·chatgpt
小R资源4 小时前
3款免费的GPT类工具
人工智能·gpt·chatgpt·ai作画·ai模型·国内免费
artificiali7 小时前
Anaconda配置pytorch的基本操作
人工智能·pytorch·python
酱香编程,风雨兼程8 小时前
深度学习——基础知识
人工智能·深度学习