torch.mean()的简单用法

简单来说就是求平均数。

比如以下的三种简单情况:

cpp 复制代码
import torch

x1 = torch.Tensor([1, 2, 3, 4])
x2 = torch.Tensor([[1],
                   [2],
                   [3],
                   [4]])
x3 = torch.Tensor([[1, 2],
                   [3, 4]])
y1 = torch.mean(x1)
y2 = torch.mean(x2)
y3 = torch.mean(x3)
print(y1)
print(y2)
print(y3)

输出:

cpp 复制代码
tensor(2.5000)
tensor(2.5000)
tensor(2.5000)

也就是说,在没有指定维度的情况下,就是对所有数进行求平均。

更多的时候用到的是有维度的情形,如:

cpp 复制代码
import torch

x = torch.Tensor([1, 2, 3, 4, 5, 6]).view(2, 3)
y_0 = torch.mean(x, dim=0)
y_1 = torch.mean(x, dim=1)
print(x)
print(y_0)
print(y_1)

输出:

cpp 复制代码
tensor([[1., 2., 3.],
        [4., 5., 6.]])
tensor([2.5000, 3.5000, 4.5000])
tensor([2., 5.])
相关推荐
风合星语3 天前
2026 具身智能技术实战(一):VLA 到底怎么控制机器人?——用 LeRobot 跑通 SmolVLA 推理
pytorch·机器人·具身智能·vla·lerobot·smolvla
Tancenter3 天前
sequeeze()和unsequeeze()
pytorch·tensorr
Thomas.Sir3 天前
第21课:PyTorch|GPU多卡训练与分布式训练基础【让多卡并行成为你的加速引擎】
人工智能·pytorch·分布式
CODER03043 天前
ubuntu22.04部署完整deepseek局域网web服务全过程(RTX5090安装黑屏+web端多人并发)
pytorch·webui·ubuntu22.04·ollama·deepseek·rtx5090·自然语言模型
宿州派大星4 天前
[NLP实战] 基于PyTorch实现N-gram词嵌入模型:输入4个词预测第5个词
人工智能·pytorch·深度学习·nlp
Liaiyang664 天前
空圈容错视角下的无人机全链路审计:从理论框架到耦合式检验
人工智能·pytorch·python·深度学习·系统架构·自动驾驶·无人机
AI模力圈4 天前
Pytorch图模式技术原理解析
pytorch·深度学习·torch.compile
Tancenter4 天前
gather和scatter API
pytorch·tensor
磁场转动100万匹4 天前
基于 dlib 与 OpenCV 的疲劳驾驶检测:眼睛纵横比(EAR)原理与代码逐段解析
pytorch·python
Dr_Fourier4 天前
AWQ量化
c++·人工智能·pytorch·ai