torch.mean()的使用方法

2024-04-11 06:36
文章标签 使用 方法 torch mean

本文主要是介绍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])

这篇关于torch.mean()的使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/893324

相关文章

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

oracle DBMS_SQL.PARSE的使用方法和示例

《oracleDBMS_SQL.PARSE的使用方法和示例》DBMS_SQL是Oracle数据库中的一个强大包,用于动态构建和执行SQL语句,DBMS_SQL.PARSE过程解析SQL语句或PL/S... 目录语法示例注意事项DBMS_SQL 是 oracle 数据库中的一个强大包,它允许动态地构建和执行

Ubuntu固定虚拟机ip地址的方法教程

《Ubuntu固定虚拟机ip地址的方法教程》本文详细介绍了如何在Ubuntu虚拟机中固定IP地址,包括检查和编辑`/etc/apt/sources.list`文件、更新网络配置文件以及使用Networ... 1、由于虚拟机网络是桥接,所以ip地址会不停地变化,接下来我们就讲述ip如何固定 2、如果apt安

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Python itertools中accumulate函数用法及使用运用详细讲解

《Pythonitertools中accumulate函数用法及使用运用详细讲解》:本文主要介绍Python的itertools库中的accumulate函数,该函数可以计算累积和或通过指定函数... 目录1.1前言:1.2定义:1.3衍生用法:1.3Leetcode的实际运用:总结 1.1前言:本文将详

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机