P18 神经网络-最大池化的使用

#P18 神经网络-最大池化的使用


#池化层Pooling layers,也被称为下采样层。

#Floor是向下取整,即不保留;Ceiling是向上取整,即保留。

# 最大池化使得运行后尺寸大大减小,可以提高运行速度,缩小文件尺寸和减少内存需求
# 对图片来说相当于降低像素了,变得模糊了一些,但是仍然保留了输入图像的信息,可以大大减小神经网络训练的数据量,可以加快训练,减少训练时间。
# 因此一般许多网络都会先一层卷积,再一层池化,然后一层非线性激活等。


#nn_maxpool.py
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

# 最大池化使得运行后尺寸大大减小,可以提高运行速度,缩小文件尺寸和减少内存需求
# 对图片来说相当于降低像素了,变得模糊了一些,但是仍然保留了输入图像的信息,可以大大减小神经网络训练的数据量,可以加快训练,减少训练时间。
# 因此一般许多网络都会先一层卷积,再一层池化,然后一层非线性激活等。
######################################################################################


input = torch.tensor([[1, 2, 0, 3, 1],
                      [0, 1, 2, 3, 1],
                      [1, 2, 1, 0, 0],
                      [5, 2, 3, 1, 1],
                      [2, 1, 0, 1, 1]], dtype=torch.float32)
#不加dtype会报错,long是长整型。最大池化无法对long数据类型进行计算,需要使用浮点数。因此用dtype=torch.float32。例如将1认为是1.0
print(input.shape)#<class 'torch.Tensor'>

input = torch.reshape(input, (-1, 1, 5, 5))#shape中第一个数不知道是多少的时候写-1即可,会自动通过后面三个值进行计算。#第二个数为1,因为input只写了一个二维数据,表示只有一层。
# 最大池化的input需要是4维的。shape为(batch_size, channel, H_in, W_in)
print(input.shape)

class West(nn.Module):#子项(父项)
    def __init__(self):#初始化子项
        super(West, self).__init__()#初始化父项
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)#ceil_mode=True时结果是[2,3,5,1]。ceil_mode=False时结果是[2]。
        # kernel_size=3表示池化核为3*3大小
        # ceil_mode=True表示保留不完整计算的结果。
        # ceil_mode=False表示不保留不完整计算的结果。

    def forward(self, input):
        output = self.maxpool1(input)#调用并输入计算
        return output

west = West()#赋值,调用创建好的神经网络
output = west(input)
print(output)

##############################################################################

dataset = torchvision.datasets.CIFAR10("./data_nn_maxpool", train=False, download=True,
                                       transform=torchvision.transforms.ToTensor())
#transform=torchvision.transforms.ToTensor()直接将数据变为tensor数据类型

dataloader = DataLoader(dataset, batch_size=64)# batch_size=X是在数据集中随机不重复地取X个

class West(nn.Module):#子项(父项)
    def __init__(self):#初始化子项
        super(West, self).__init__()#初始化父项
        self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=False)
        # torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
        # kernel_size=3表示池化核为3*3大小
        # ceil_mode=True表示保留不完整计算的结果。
        # ceil_mode=False表示不保留不完整计算的结果。
        # stride默认值为kernel_size,表示横向和纵向的步进大小。
        # dilation表示层是否包含空洞,默认值为1,包含一个空洞。

    def forward(self, input):
        output = self.maxpool1(input)
        return output

west = West()#赋值,调用创建好的神经网络

writer = SummaryWriter("./logs_maxpool")#创建日志

step = 0
for data in dataloader:#训练设置,来源于载入的数据dataloader
    imgs, targets = data#赋值
    writer.add_images("input", imgs, step)#记录输入数据
    output = west(imgs)#括号内输入数据,训练后输出 #这是最核心的一步
    #最大池化不会改变channel,不需要用reshape,可以直接在tensorboard中显示3通道RGB图
    writer.add_images("output", output, step)#记录输出数据
    step = step + 1

writer.close()#关闭写入日志



#MaxPool2d附官方网址和说明

#MaxPool2d网址
https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html#torch.nn.MaxPool2d

#MaxPool2d说明
classtorch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
Parameters
kernel_size (Union[int, Tuple[int, int]]) – the size of the window to take a max over
stride (Union[int, Tuple[int, int]]) – the stride of the window. Default value is kernel_size
padding (Union[int, Tuple[int, int]]) – Implicit negative infinity padding to be added on both sides
dilation (Union[int, Tuple[int, int]]) – a parameter that controls the stride of elements in the window
return_indices (bool) – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later
ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape

#池化层Pooling layers,也被称为下采样层。

#Floor是向下取整,即不保留;Ceiling是向上取整,即保留。

# 最大池化使得运行后尺寸大大减小,可以提高运行速度,缩小文件尺寸和减少内存需求
# 对图片来说相当于降低像素了,变得模糊了一些,但是仍然保留了输入图像的信息,可以大大减小神经网络训练的数据量,可以加快训练,减少训练时间。
# 因此一般许多网络都会先一层卷积,再一层池化,然后一层非线性激活等。

#MaxPool2d附官方网址和说明

#MaxPool2d网址
https://pytorch.org/docs/stable/generated/torch.nn.MaxPool2d.html#torch.nn.MaxPool2d

#MaxPool2d说明
classtorch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
Parameters
kernel_size (Union[int, Tuple[int, int]]) – the size of the window to take a max over
stride (Union[int, Tuple[int, int]]) – the stride of the window. Default value is kernel_size
padding (Union[int, Tuple[int, int]]) – Implicit negative infinity padding to be added on both sides
dilation (Union[int, Tuple[int, int]]) – a parameter that controls the stride of elements in the window
return_indices (bool) – if True, will return the max indices along with the outputs. Useful for torch.nn.MaxUnpool2d later
ceil_mode (bool) – when True, will use ceil instead of floor to compute the output shape

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西西与东东

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值