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

torch.mean()的使用方法

对一个三维数组的每一维度进行操作

1,dim=0

a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7]).view(2, 2, 2) 
print(a) 
mean = torch.mean(a, 0) 
print(mean, mean.shape)

输出结果:

tensor([[[0., 1.],

             [2., 3.]],

             [[4., 5.],

              [6., 7.]]])

tensor([[2., 3.],

            [4., 5.]]) torch.Size([2, 2])

2,dim=1

a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7]).view(2, 2, 2) 
print(a) 
mean = torch.mean(a, 1) 
print(mean, mean.shape)

输出结果

tensor(

[[[0., 1.],

[2., 3.]],

[[4., 5.],

[6., 7.]]])

tensor(

[[1., 2.],

[5., 6.]]) torch.Size([2, 2])

3,dim=2

a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7]).view(2, 2, 2) 
print(a) 
mean = torch.mean(a, 2) 
print(mean, mean.shape)

输出结果

tensor(

[[[0., 1.],

[2., 3.]],

[[4., 5.],

[6., 7.]]])

tensor(

[[0.5000, 2.5000],

[4.5000, 6.5000]]) torch.Size([2, 2])

补充,如果在函数中添加了True,表示要和原来数的维度一致,不够的用维度1来添加,如下


a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7]).view(2, 2, 2) 
print(a) 
mean = torch.mean(a, 2, True) 
print(mean, mean.shape)
tensor([[[0., 1.],[2., 3.]],[[4., 5.],[6., 7.]]])
tensor([[[0.5000],[2.5000]],[[4.5000],[6.5000]]]) torch.Size([2, 2, 1])

补充多维度变化


a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15]).view(2, 2, 2,2) 
print(a) 
mean = torch.mean(a, 0, True) 
print(mean, mean.shape)
tensor([[[[ 0.,  1.],[ 2.,  3.]],[[ 4.,  5.],[ 6.,  7.]]],[[[ 8.,  9.],[10., 11.]],[[12., 13.],[14., 15.]]]])
tensor([[[[ 4.,  5.],[ 6.,  7.]],[[ 8.,  9.],[10., 11.]]]]) torch.Size([1, 2, 2, 2])

a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15]).view(2, 2, 2,2) 
print(a) 
mean = torch.mean(a, 1, True) 
print(mean, mean.shape)
tensor([[[[ 0.,  1.],[ 2.,  3.]],[[ 4.,  5.],[ 6.,  7.]]],[[[ 8.,  9.],[10., 11.]],[[12., 13.],[14., 15.]]]])
tensor([[[[ 2.,  3.],[ 4.,  5.]]],[[[10., 11.],[12., 13.]]]]) torch.Size([2, 1, 2, 2])
a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15]).view(2, 2, 2,2) 
print(a) 
mean = torch.mean(a, 2, True) 
print(mean, mean.shape)tensor([[[[ 0.,  1.],[ 2.,  3.]],[[ 4.,  5.],[ 6.,  7.]]],[[[ 8.,  9.],[10., 11.]],[[12., 13.],[14., 15.]]]])
tensor([[[[ 1.,  2.]],[[ 5.,  6.]]],[[[ 9., 10.]],[[13., 14.]]]]) torch.Size([2, 2, 1, 2])

a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15]).view(2, 2, 2,2) 
print(a) 
mean = torch.mean(a, 3, True) 
print(mean, mean.shape)
tensor([[[[ 0.,  1.],[ 2.,  3.]],[[ 4.,  5.],[ 6.,  7.]]],[[[ 8.,  9.],[10., 11.]],[[12., 13.],[14., 15.]]]])
tensor([[[[ 0.5000],[ 2.5000]],[[ 4.5000],[ 6.5000]]],[[[ 8.5000],[10.5000]],[[12.5000],[14.5000]]]]) torch.Size([2, 2, 2, 1])

a = torch.Tensor([0, 1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15,0, 1, 2, 3, 4, 5,6,7,8,9,10,11,12,13,14,15]).view(2, 2, 2,2,2) 
print(a) 
mean = torch.mean(a, 3, True) 
print(mean, mean.shape)
tensor([[[[[ 0.,  1.],[ 2.,  3.]],[[ 4.,  5.],[ 6.,  7.]]],[[[ 8.,  9.],[10., 11.]],[[12., 13.],[14., 15.]]]],[[[[ 0.,  1.],[ 2.,  3.]],[[ 4.,  5.],[ 6.,  7.]]],[[[ 8.,  9.],[10., 11.]],[[12., 13.],[14., 15.]]]]])
tensor([[[[[ 1.,  2.]],[[ 5.,  6.]]],[[[ 9., 10.]],[[13., 14.]]]],[[[[ 1.,  2.]],[[ 5.,  6.]]],[[[ 9., 10.]],[[13., 14.]]]]]) torch.Size([2, 2, 2, 1, 2])

http://www.lryc.cn/news/335991.html

相关文章:

  • windows安装Redis,Mongo,ES并快速基本掌握开发流程
  • ruoyi-nbcio-plus基于vue3的flowable的自定义业务提交申请组件的升级修改
  • 掌握网络抓取技术:利用RobotRules库的Perl下载器一览小红书的世界
  • 典型新能源汽车热管理系统方案分析
  • 使用Docker部署开源项目FreeGPT35来免费调用ChatGPT3.5 API
  • 《Linux运维实战:Kylin V10操作系统开启安装软件保留缓存设置》
  • 视频生成技术:从GAN到Latte
  • 机器学习中的激活函数
  • LinuxAndroid: 旋转编码器input输入事件适配(旋转输入)
  • 机器学习和深度学习-- 李宏毅(笔记与个人理解)Day10
  • perl 交叉编译
  • 浅谈.版本管理工具
  • 【汇编语言实战】已知10个整数求最大值
  • 在 CentOS 7 上安装 Redis
  • 『51单片机』蜂鸣器
  • 计算机视觉 | 基于二值图像数字矩阵的距离变换算法
  • Arcgis windows webadaptor配置
  • 对接阿里云实时语音转文字的思路
  • 如何转行成为产品经理?
  • SpringCloudAlibaba-整合nacos(二)
  • STM32H7通用定时器计数功能的使用
  • 信息系统项目管理师0044:IT治理方法与标准(3信息系统治理—3.1 IT治理—3.1.4 IT治理方法与标准)
  • 探索Linux:在VMware虚拟机上安装Linux操作系统
  • JavaScript进阶6之函数式编程与ES6ESNext规范
  • AcWing 1381. 阶乘
  • Leetcode 394. 字符串解码
  • LeetCode - 1702. 修改后的最大二进制字符串
  • 虹科Pico汽车示波器 | 免拆诊断案例 | 2011款东风悦达起亚K5车发动机偶尔起动困难
  • Docker- Redis
  • Python 基于 OpenCV 视觉图像处理实战 之 OpenCV 简单视频处理实战案例 之八 简单视频素描效果