当前位置: 首页 > news >正文

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

第一种情况

import torchx = 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])

第二种情况

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])

第三种情况

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

import torchx = 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

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

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

import torchx = 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])

相关文章:

  • UML相关1
  • Docker 国内镜像源更换
  • [SWPUCTF 2022 新生赛]ez_1zpop(php反序列化之pop链构造)
  • MySQL之高级特性(三)
  • 道路堵塞监测识别摄像机
  • 推荐这两款AI工具,真的很好用
  • 屏幕空间环境光遮蔽(SSAO)在AI绘画中的作用
  • 【AI原理解析】— GPT-4o模型
  • 5.2 参照完整性
  • C#操作MySQL从入门到精通(22)——创建表与操纵表
  • 如何手动实现multiSetIfAbsent、multiExpire
  • AI图书推荐:《如何利用ChatGPT在线赚钱》
  • 时序分解 | Matlab实现SCSSA-VMD融合正余弦和柯西变异的麻雀搜索算法优化变分模态分解时间序列信号分解
  • Ps:自动批量处理照片
  • 网络学了点socket,写个聊天室,还得改进
  • JS 中的深拷贝与浅拷贝
  • Android系统模拟器绘制实现概述
  • angular2开源库收集
  • Linux编程学习笔记 | Linux多线程学习[2] - 线程的同步
  • OSS Web直传 (文件图片)
  • springboot_database项目介绍
  • SQL 难点解决:记录的引用
  • WePY 在小程序性能调优上做出的探究
  • 解决jsp引用其他项目时出现的 cannot be resolved to a type错误
  • 你不可错过的前端面试题(一)
  • 实战|智能家居行业移动应用性能分析
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 我这样减少了26.5M Java内存!
  • 一份游戏开发学习路线
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • # Redis 入门到精通(一)数据类型(4)
  • #数据结构 笔记三
  • #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  • (C11) 泛型表达式
  • (Java企业 / 公司项目)点赞业务系统设计-批量查询点赞状态(二)
  • (读书笔记)Javascript高级程序设计---ECMAScript基础
  • (二开)Flink 修改源码拓展 SQL 语法
  • (论文阅读23/100)Hierarchical Convolutional Features for Visual Tracking
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (顺序)容器的好伴侣 --- 容器适配器
  • (转载)VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)
  • .gitignore文件---让git自动忽略指定文件
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET 8 跨平台高性能边缘采集网关
  • .net Stream篇(六)
  • .net 设置默认首页
  • .NET 药厂业务系统 CPU爆高分析
  • .NET/C# 使窗口永不激活(No Activate 永不获得焦点)
  • .Net6使用WebSocket与前端进行通信
  • .NET实现之(自动更新)
  • /使用匿名内部类来复写Handler当中的handlerMessage()方法
  • @JsonFormat与@DateTimeFormat注解的使用
  • [ C++ ] 继承
  • [ linux ] linux 命令英文全称及解释
  • [《百万宝贝》观后]To be or not to be?