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])
相关推荐
蚝油菜花几秒前
TheoremExplainAgent – AI教学双智能体,数理化定理自动转动画
人工智能·数学·开源
蚝油菜花2 分钟前
Archon – 开源 AI 智能体框架,自主生成代码构建 AI 智能体
人工智能·开源
Hello kele6 分钟前
大型项目,选择conda还是Poetry要点分析
人工智能·python·conda·ai编程·poetry
SmallBambooCode9 分钟前
【人工智能】【Python】在Scikit-Learn中使用KNN(K最近邻算法)
人工智能·python·机器学习·scikit-learn·近邻算法
訾博ZiBo24 分钟前
AI日报 - 2025年3月7日
人工智能
梓羽玩Python27 分钟前
一夜刷屏AI圈!Manus:这不是聊天机器人,是你的“AI打工仔”!
人工智能
Gene_INNOCENT28 分钟前
大型语言模型训练的三个阶段:Pre-Train、Instruction Fine-tuning、RLHF (PPO / DPO / GRPO)
人工智能·深度学习·语言模型
游戏智眼28 分钟前
中国团队发布通用型AI Agent产品Manus;GPT-4.5正式面向Plus用户推出;阿里发布并开源推理模型通义千问QwQ-32B...|游戏智眼日报
人工智能·游戏·游戏引擎·aigc
挣扎与觉醒中的技术人30 分钟前
如何优化FFmpeg拉流性能及避坑指南
人工智能·深度学习·性能优化·ffmpeg·aigc·ai编程
watersink33 分钟前
Dify框架下的基于RAG流程的政务检索平台
人工智能·深度学习·机器学习