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

从上面的结果可以看出来,经过最大池化处理的部分明显比原图像更模糊,但是和原图像一对比至少还看得出来原来的特征。
边栏推荐
- Seven crimes of counting software R & D Efficiency
- Concurrent mode of different performance testing tools
- One click shell to automatically deploy any version of redis
- Difference between cookie and session
- Web server: how to choose a good web server these five aspects should be paid attention to
- Internet winter, how to spend three months to make a comeback
- Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
- What is uid? What is auth? What is a verifier?
- 2022 t elevator repair question bank and simulation test
猜你喜欢

Maixll dock quick start

2022 gas examination question bank and online simulation examination

How to use maixll dock

2022年聚合工艺考试题及模拟考试

Simple implementation of slf4j

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

MySQL winter vacation self-study 2022 12 (5)
![[send email with error] 535 error:authentication failed](/img/58/8cd22fed1557077994cd78fd29f596.png)
[send email with error] 535 error:authentication failed

Pytorch(三) —— 函数优化

CF1638E colorful operations
随机推荐
Tcp/ip explanation (version 2) notes / 3 link layer / 3.4 bridge and switch / 3.4.2 multiple registration protocol (MRP)
2022 hoisting machinery command registration examination and hoisting machinery command examination registration
C language games (I) -- guessing games
VR线上展览所具备应用及特色
Seven crimes of counting software R & D Efficiency
Simple implementation of slf4j
Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I
[pat (basic level) practice] - [simple simulation] 1064 friends
Advanced application of ES6 modular and asynchronous programming
使用WinMTR软件简单分析跟踪检测网络路由情况
Ospfb notes - five messages [ultra detailed] [Hello message, DD message, LSR message, LSU message, lsack message]
How to view the changes and opportunities in the construction of smart cities?
CUDA development and debugging tool
[difficult] sqlserver2008r2, can you recover only some files when recovering the database?
CF1638E. Colorful operations Kodori tree + differential tree array
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
一些小知识点
网站服务器:好用的网站服务器怎么选这五方面要关注
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling