pytorch方法测试详解——归一化(BatchNorm2d)


Posted in Python onJanuary 15, 2020

测试代码:

import torch

import torch.nn as nn

m = nn.BatchNorm2d(2,affine=True) #权重w和偏重将被使用
input = torch.randn(1,2,3,4)
output = m(input)

print("输入图片:")
print(input)
print("归一化权重:")
print(m.weight)
print("归一化的偏重:")
print(m.bias)

print("归一化的输出:")
print(output)
print("输出的尺度:")
print(output.size())

# i = torch.randn(1,1,2)
print("输入的第一个维度:")
print(input[0][0])
firstDimenMean = torch.Tensor.mean(input[0][0])
firstDimenVar= torch.Tensor.var(input[0][0],False) #Bessel's Correction贝塞尔校正不会被使用

print(m.eps)
print("输入的第一个维度平均值:")
print(firstDimenMean)
print("输入的第一个维度方差:")
print(firstDimenVar)

bacthnormone = \
  ((input[0][0][0][0] - firstDimenMean)/(torch.pow(firstDimenVar+m.eps,0.5) ))\
        * m.weight[0] + m.bias[0]
print(bacthnormone)

输出为:

输入图片:

tensor([[[[-2.4308, -1.0281, -1.1322, 0.9819],
     [-0.4069, 0.7973, 1.6296, 1.6797],
     [ 0.2802, -0.8285, 2.0101, 0.1286]],


     [[-0.5740, 0.1970, -0.7209, -0.7231],
     [-0.1489, 0.4993, 0.4159, 1.4238],
     [ 0.0334, -0.6333, 0.1308, -0.2180]]]])

归一化权重:

Parameter containing:
tensor([ 0.5653, 0.0322])

归一化的偏重:

Parameter containing:
tensor([ 0., 0.])

归一化的输出:

tensor([[[[-1.1237, -0.5106, -0.5561, 0.3679],
     [-0.2391, 0.2873, 0.6510, 0.6729],
     [ 0.0612, -0.4233, 0.8173, -0.0050]],


     [[-0.0293, 0.0120, -0.0372, -0.0373],
     [-0.0066, 0.0282, 0.0237, 0.0777],
     [ 0.0032, -0.0325, 0.0084, -0.0103]]]])

输出的尺度:

torch.Size([1, 2, 3, 4])

输入的第一个维度:

tensor([[-2.4308, -1.0281, -1.1322, 0.9819],
    [-0.4069, 0.7973, 1.6296, 1.6797],
    [ 0.2802, -0.8285, 2.0101, 0.1286]])
1e-05

输入的第一个维度平均值:

tensor(0.1401)

输入的第一个维度方差:

tensor(1.6730)
tensor(-1.1237)

结论:

输出的计算公式如下

pytorch方法测试详解——归一化(BatchNorm2d)

注意torch中方差实现的方法是没有使用Bessel's correction 贝塞尔校正的方差,所以在自己写的方差中不要用错了。(贝塞尔校正,即样本方差和总体方差之间区别和校正。)

以上这篇pytorch方法测试详解——归一化(BatchNorm2d)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
利用Psyco提升Python运行速度
Dec 24 Python
Python2.x利用commands模块执行Linux shell命令
Mar 11 Python
tensorflow学习笔记之简单的神经网络训练和测试
Apr 15 Python
pycharm 将django中多个app放到同个文件夹apps的处理方法
May 30 Python
分享vim python缩进等一些配置
Jul 02 Python
tensorflow 用矩阵运算替换for循环 用tf.tile而不写for的方法
Jul 27 Python
python去掉 unicode 字符串前面的u方法
Oct 21 Python
Django中数据库的数据关系:一对一,一对多,多对多
Oct 21 Python
python3.7 使用pymssql往sqlserver插入数据的方法
Jul 08 Python
我们为什么要减少Python中循环的使用
Jul 10 Python
Python中常见的数制转换有哪些
May 27 Python
python必学知识之文件操作(建议收藏)
May 30 Python
Python 中@property的用法详解
Jan 15 #Python
Python字符串中删除特定字符的方法
Jan 15 #Python
计算pytorch标准化(Normalize)所需要数据集的均值和方差实例
Jan 15 #Python
pytorch 图像中的数据预处理和批标准化实例
Jan 15 #Python
pytorch实现特殊的Module--Sqeuential三种写法
Jan 15 #Python
python实现删除列表中某个元素的3种方法
Jan 15 #Python
python opencv根据颜色进行目标检测的方法示例
Jan 15 #Python
You might like
在wamp集成环境下升级php版本(实现方法)
2013/07/01 PHP
PHP内核探索之变量
2015/12/22 PHP
thinkPHP实现递归循环栏目并按照树形结构无限极输出的方法
2016/05/19 PHP
使用composer命令加载vendor中的第三方类库 的方法
2019/07/09 PHP
PhpStorm 2020.3:新增开箱即用的PHP 8属性(推荐)
2020/10/30 PHP
理解JAVASCRIPT中hasOwnProperty()的作用
2013/06/05 Javascript
元素未显示设置width/height时IE中使用currentStyle获取为auto
2014/05/04 Javascript
javascript中局部变量和全局变量的区别详解
2015/02/27 Javascript
jQuery中cookie插件用法实例分析
2015/12/04 Javascript
Bootstrap php制作动态分页标签
2016/12/23 Javascript
详谈AngularJs 控制器、数据绑定、作用域
2017/07/09 Javascript
vue加载完成后的回调函数方法
2018/09/07 Javascript
Vue利用History记录上一页面的数据方法实例
2018/11/02 Javascript
微信小程序实现图片上传
2019/05/23 Javascript
JS实现简单tab选项卡切换
2019/10/25 Javascript
vue点击Dashboard不同内容 跳转到同一表格的实例
2020/11/13 Javascript
vue导入.md文件的步骤(markdown转HTML)
2020/12/31 Vue.js
[03:11]完美世界DOTA2联赛PWL DAY8集锦
2020/11/09 DOTA
python使用rsa加密算法模块模拟新浪微博登录
2014/01/22 Python
简单介绍Python的轻便web框架Bottle
2015/04/08 Python
Python学习之Django的管理界面代码示例
2018/02/10 Python
Python matplotlib绘制饼状图功能示例
2019/09/10 Python
详解Django3中直接添加Websockets方式
2020/02/12 Python
python GUI库图形界面开发之PyQt5中QWebEngineView内嵌网页与Python的数据交互传参详细方法实例
2020/02/26 Python
利用django model save方法对未更改的字段依然进行了保存
2020/03/28 Python
CSS3中的注音对齐属性ruby-align用法指南
2016/07/01 HTML / CSS
哥德堡通行证:Gothenburg Pass
2019/12/09 全球购物
经贸日语专业个人求职信
2013/12/13 职场文书
网络研修随笔感言
2014/02/17 职场文书
房地产活动策划方案
2014/05/14 职场文书
政府法律服务方案
2014/06/14 职场文书
小学生竞选班干部演讲稿(5篇)
2014/09/12 职场文书
解除劳动合同协议书
2014/09/17 职场文书
党员专题组织生活会发言材料
2014/10/17 职场文书
解决Git推送错误non-fast-forward的方法
2022/06/25 Servers
小程序实现侧滑删除功能
2022/06/25 Javascript