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.])
相关推荐
大模型玩家七七13 小时前
证据不足 vs 证据冲突:哪个对模型更致命
数据库·人工智能·pytorch·深度学习·安全
weixin_395448911 天前
export_onnx.py_0130
pytorch·python·深度学习
工程师老罗1 天前
反向传播及其用法
pytorch
抠头专注python环境配置1 天前
基于Pytorch ResNet50 的珍稀野生动物识别系统(Python源码 + PyQt5 + 数据集)
pytorch·python
永恒的溪流1 天前
环境出问题,再修改
pytorch·python·深度学习
工程师老罗1 天前
Pytorch中的优化器及其用法
人工智能·pytorch·python
m0_462605221 天前
第G4周:CGAN|生成手势图像 | 可控制生成
pytorch
工程师老罗1 天前
Pytorch完整的模型训练流程
人工智能·pytorch·深度学习
工程师老罗1 天前
优化器、反向传播、损失函数之间是什么关系,Pytorch中如何使用和设置?
人工智能·pytorch·python
果粒蹬i2 天前
基于PyTorch的CNN-BiLSTM时序预测:从数据滑窗到模型部署
人工智能·pytorch·cnn