pytorch中squeeze函数用法

squeeze的中文意思是"挤压 ",顾名思义,该函数的作用是压缩维度

python 复制代码
squeeze(input, dim=None) -> Tensor

input一个高维张量,如果各个维度中存在大小为1的维度,squeeze才起作用,下面举例说明

python 复制代码
x = torch.arange(6).reshape(2,1,3)

# tensor([[[0, 1, 2]],
#         [[3, 4, 5]]])  shape=(2,1,3)

x = x.squeeze()

# tensor([[0, 1, 2],
#        [3, 4, 5]])    shape=(2,3)

如果指定dim,当dim=1时候,效果和上面相同,如果dim为其他,那么x的维度不变。

当然,如果各个维度中不存在大小为1的维度,那么squeeze函数对x张量无效。

相关推荐
用户51914958484520 小时前
libcurl Headers API 释放后重利用漏洞:跨请求复用头句柄导致堆内存安全风险
人工智能·aigc
踩蚂蚁20 小时前
自定义语音唤醒词:从训练到部署的完整链路实践
人工智能
用户51914958484520 小时前
CVE-2025-1094 PostgreSQL SQL注入与WebSocket劫持远程代码执行利用工具
人工智能·aigc
金銀銅鐵21 小时前
用 Python 实现 Take-Away 游戏
python·游戏
IT_陈寒21 小时前
SpringBoot自动配置这个坑,我踩进去又爬出来了
前端·人工智能·后端
copyer_xyf1 天前
Agent 流程编排
后端·python·agent
copyer_xyf1 天前
Agent RAG
后端·python·agent
copyer_xyf1 天前
【RAG】向量数据库:milvus
后端·python·agent
copyer_xyf1 天前
Agent 记忆管理
后端·python·agent
冬奇Lab1 天前
Agent 系列(23):Web Agent——让 Agent 真正浏览网页
人工智能·llm·agent