NJU1healer的个人博客分享 http://blog.sciencenet.cn/u/NJU1healer

博文

torch.mean()

已有 9389 次阅读 2020-10-23 23:00 |个人分类:Pytorch|系统分类:科研笔记

参考博客:https://blog.csdn.net/Apikaqiu/article/details/104379960

mean()函数的参数:dim=0,按行求平均值,返回的形状是(1,列数);dim=1,按列求平均值,返回的形状是(行数,1),默认不设置dim的时候,返回的是所有元素的平均值。

x=torch.arange(12).view(4,3)

'''

注意:在这里使用的时候转一下类型,否则会报RuntimeError: Can only calculate the mean of floating types. Got Long instead.的错误。

查看了一下x元素类型是torch.int64,根据提示添加一句x=x.float()转为tensor.float32就行

'''

x=x.float()

x_mean=torch.mean(x)

x_mean0=torch.mean(x,dim=0,keepdim=True)

x_mean1=torch.mean(x,dim=1,keepdim=True)

print('x:')

print(x)

print('x_mean0:')

print(x_mean0)

print('x_mean1:')

print(x_mean1)

print('x_mean:')

print(x_mean)

点滴分享,福泽你我!Add oil!



https://blog.sciencenet.cn/blog-3428464-1255548.html

上一篇:Python 中的几种矩阵乘法 np.dot, np.multiply, *
下一篇:Python函数中参数前带*和**是什么意思?
收藏 IP: 103.149.249.*| 热度|

0

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...
扫一扫,分享此博文

全部作者的其他最新博文

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-18 20:38

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部