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.])
相关推荐
盼小辉丶5 天前
PyTorch实战(30)——使用TorchScript和ONNX导出通用PyTorch模型
人工智能·pytorch·深度学习·模型部署
封奚泽优5 天前
使用mmdetection项目进行训练记录
pytorch·python·cuda·mmdetection·mmcv
tony3655 天前
pytorch分布式训练解释
人工智能·pytorch·分布式
weixin_贾5 天前
深度学习基础理论与 PyTorch 实战 —— 从传统机器学习到前沿模型全攻略
pytorch·深度学习·机器学习
大连好光景6 天前
PyTorch深度学习----优化器
pytorch·深度学习·学习
多恩Stone7 天前
【3D-AICG 系列-11】Trellis 2 的 Shape VAE 训练流程梳理
人工智能·pytorch·算法·3d·aigc
隔壁大炮7 天前
08. PyTorch_张量基本创建方式
人工智能·pytorch·python
隔壁大炮7 天前
07. PyTorch框架简介
人工智能·pytorch·python
大鹏的NLP博客7 天前
Rust + PyTorch 实现 BGE 向量检索系统
人工智能·pytorch·rust
勾股导航9 天前
蚁群优化算法
人工智能·pytorch·python