栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

BN层(Pytorch讲解)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

BN层(Pytorch讲解)

# -*- coding: utf-8 -*-
"""Untitled13.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1UmI0eZXtftAp8hd9A3cj4DhffZ7kw_x1
"""

from torch import nn
import torch

"""## 2D的BN层"""

rgb = torch.randn(1, 3, 2, 2) # (batchsize,channel,w,h)
print(rgb)
print(rgb.shape)

conv = nn.Conv2d(3, 2, 1)

x = conv(rgb)

print(x)
print(x.shape)

bn = nn.BatchNorm2d(2)

res = bn(x)

print(res)
print(res.shape)



mean = x.mean(dim=0, keepdim=True).mean(dim=2, keepdim=True).mean(dim=3, keepdim=True)
var = ((x - mean) ** 2).mean(dim=0, keepdim=True).mean(dim=2, keepdim=True).mean(dim=3, keepdim=True)

x_hat = (x - mean) / torch.sqrt(var + 0.00001)
x_hat



bn2 = nn.BatchNorm1d(12)

# With Learnable Parameters
m = nn.BatchNorm1d(2,affine=False)
input = torch.randn(2, 2)
output = m(input)

input

input.shape

output

print(output.shape)

mean = input.mean(dim=0)
var = ((input - mean) ** 2).mean(dim=0)

mean

var

test = (input-mean)/ torch.sqrt(var + 1e-5)

test

m = nn.Linear(20, 30)
input = torch.randn(128, 20)
output = m(input)
print(output.size())

bn_layer = nn.BatchNorm1d(30,affine=False)

o2 = bn_layer(output)

o2.shape


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/744027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号