当前位置:网站首页>神经网络-最大池化的使用
神经网络-最大池化的使用
2022-07-01 04:35:00 【booze-J】
文章
池化层的官方文档中介绍了很多种的池化方法,但是最常用的还是MaxPool2d,这里我们也用MaxPool2d来讲解,其他的类似,关键还是要学会看官方文档
概述:
最大池化目的就是为了保留输入的特征,但是同时把数据量减少,最大池化之后数据量就减少了,对于整个网路来说,进行计算的参数就变少了,就会训练的更快。
就相当于在网上看视频,视频又有1080P的,720P的,360P的,懂吧,1080P就相当于输入视频,720P的就相当于经过最大池化后的视频,720P也可以满足需求,网不行的时候不就可以看720P的呗。
什么是最大池化?
最大池化操作相当于核在图像上移动的时候,筛选出被核覆盖区域的最大值,注意核的移动步长是kernel_size。
说到MaxPool2d接口的使用这里,关键就是学会如何传参。
torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
参数说明
- kernel_size – the size of the window to take a max over
- stride – the stride of the window. Default value is kernel_size
- padding – implicit zero padding to be added on both sides
- dilation – a parameter that controls the stride of elements in the window
- return_indices – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later
- ceil_mode – when True, will use ceil instead of floor to compute the output shape
拎出几个常用的和大家说一下:
- kernel_size传入的是常数的时候,则会生成一个大小为kernel_size X kernel_size大小的核,kernel_size传入的是元组的时候,则会生成一个规定高和宽的一个核。
- 说到Ceil_model参数,有些情况下,核无法全覆盖在图像上,只覆盖到了部分图像,另一部分已经到图像外面去了,Ceil_model就是决定这个时候,对于只覆盖了部分的这一块要不要进行最大池化操作,取出最大值,若Ceil_model为True,则经行最大池化操作,取出最大值,否则不进行最大池化操作,不取出最大值。可以看下图对Ceil_model参数进行理解:
过程:
结果:
MaxPool2d使用的示例代码如下:
import torch
import torchvision
from torch import nn
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
dataset = torchvision.datasets.CIFAR10("CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
# 注意dataset中transform参数接收的是个对象,所以要加上括号,还有就是之后使用神经网络进行运算的时候需要的数据类型是tensor类型,所以transforms参数要加上。
dataloader = DataLoader(dataset,batch_size=64)
# 搭建神经网络
class Booze(nn.Module):
# 继承nn.Module的初始化
def __init__(self):
super(Booze, self).__init__()
self.maxpool1 = MaxPool2d(kernel_size=3,ceil_mode=True)
# 重写forward函数
def forward(self,x):
output = self.maxpool1(x)
return output
obj = Booze()
# 使用tensorboard可视化过程
writer = SummaryWriter("logs")
step = 0
for data in dataloader:
imgs,targets = data
writer.add_images("input",imgs,step)
output = obj(imgs)
writer.add_images("output",output,step)
step+=1
writer.close()
上述代码运行完,在pycharm下面的terminal窗口输入tensorboard --logdir=logs回车一查看:
效果如下:

从上面的结果可以看出来,经过最大池化处理的部分明显比原图像更模糊,但是和原图像一对比至少还看得出来原来的特征。
边栏推荐
- 测量三相永磁同步电机的交轴直轴电感
- Codeworks round 449 (Div. 1) C. Kodori tree template
- Openresty rewrites the location of 302
- [2020 overview] overview of link prediction based on knowledge map embedding
- Applications and features of VR online exhibition
- Summary of testing experience - Testing Theory
- 什么是uid?什么是Auth?什么是验证器?
- [learn C and fly] S1E20: two dimensional array
- JS rotation chart
- Daily question - line 10
猜你喜欢

2022 t elevator repair question bank and simulation test

LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target

Internet winter, how to spend three months to make a comeback

CF1638E. Colorful operations Kodori tree + differential tree array

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

js 图片路径转换base64格式

2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers

Knowledge supplement: redis' basic data types and corresponding commands

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

The design points of voice dialogue system and the importance of multi round dialogue
随机推荐
Dual contractual learning: text classification via label aware data augmentation reading notes
Shell analysis server log command collection
Learn Chapter 20 of vue3 (keep alive cache component)
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
2022 gas examination question bank and online simulation examination
软件研发的十大浪费:研发效能的另一面
Programs and processes, process management, foreground and background processes
离线安装wireshark2.6.10
RDF query language SPARQL
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
做网站数据采集,怎么选择合适的服务器呢?
What are permissions? What are roles? What are users?
Maixll-Dock 使用方法
[learn C and fly] S1E20: two dimensional array
Introduction to JVM stack and heap
How to do the performance pressure test of "Health Code"
Cmake selecting compilers and setting compiler options
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
2022危险化学品生产单位安全生产管理人员题库及答案