当前位置:网站首页>神经网络-最大池化的使用
神经网络-最大池化的使用
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回车一查看:
效果如下:

从上面的结果可以看出来,经过最大池化处理的部分明显比原图像更模糊,但是和原图像一对比至少还看得出来原来的特征。
边栏推荐
- One click shell to automatically deploy any version of redis
- JS rotation chart
- Question bank and answers for chemical automation control instrument operation certificate examination in 2022
- Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address
- Mallbook: how can hotel enterprises break the situation in the post epidemic era?
- 2022年化工自动化控制仪表操作证考试题库及答案
- [send email with error] 535 error:authentication failed
- The design points of voice dialogue system and the importance of multi round dialogue
- Shell之一键自动部署Redis任意版本
- Programs and processes, process management, foreground and background processes
猜你喜欢

Dede collection plug-in does not need to write rules

2022 polymerization process test questions and simulation test

Knowledge supplement: redis' basic data types and corresponding commands

Daily question - line 10

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

扩展-Fragment

Offline installation of Wireshark 2.6.10

TCP server communication flow

Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services

Shell之一键自动部署Redis任意版本
随机推荐
Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
How to do the performance pressure test of "Health Code"
What is uid? What is auth? What is a verifier?
Openresty rewrites the location of 302
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
Seven crimes of counting software R & D Efficiency
Caijing 365 stock internal reference | the first IPO of Beijing stock exchange; the subsidiary of the recommended securities firm for gambling and gambling, with a 40% discount
【LeetCode】100. Same tree
2022 t elevator repair question bank and simulation test
Maixll-Dock 快速上手
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
After many job hopping, the monthly salary is equal to the annual salary of old colleagues
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism
The design points of voice dialogue system and the importance of multi round dialogue
Concurrent mode of different performance testing tools
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
扩展-Fragment
js 图片路径转换base64格式