B站小土堆学习视频
https://www.bilibili.com/video/BV1hE411t7RN?p=19&spm_id_from=pageDriver&vd_source=9607a6d9d829b667f8f0ccaaaa142fcb
https://pytorch.org/docs/stable/nn.html#pooling-layers
doc–>pytorch–>torch.nn–>Pooling layers
池化核可以理解为卷积核,跟卷积核的使用方式一致,计算公式不太一致
池化和卷积不同 一是核的使用不同,二是步长不同【如果步长是1 就达不到降采样的目的了】
ceil允许有出界操作,floor不允许出界操作
最大池化是对池化核覆盖的区域取最大值操作,核里面可以没有数值
import torch
from torch import nn
from torch.nn import MaxPool2d
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) # RuntimeError: "max_pool2d" not implemented for 'Long'
print(input.shape)
input = torch.reshape(input, (-1,1,5,5))
print(input.shape)
print(input)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.maxpool2d1 = MaxPool2d(3, ceil_mode=True)
self.maxpool2d2 = MaxPool2d(3, ceil_mode=False)
def forward(self, input):
output1 = self.maxpool2d1(input)
output2 = self.maxpool2d2(input)
return output1, output2
tudui = Tudui()
out1, out2 = tudui(input)
print(out1)
print(out2)
***.conda\envs\pytorch\lib\site-packages\torch\nn\functional.py:780: UserWarning: Note that order of the arguments: ceil_mode and return_indices will changeto match the args list in nn.MaxPool2d in a future release.
warnings.warn("Note that order of the arguments: ceil_mode and return_indices will change"
torch.Size([5, 5])
torch.Size([1, 1, 5, 5])
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.]]]])
tensor([[[[2., 3.],
[5., 1.]]]])
tensor([[[[2.]]]])
Process finished with exit code 0
import torch
from torch import nn
import torchvision
from torch.nn import MaxPool2d
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
test_set = torchvision.datasets.CIFAR10('./dataset', train=False, transform=torchvision.transforms.ToTensor(), download=True)
dataloader = DataLoader(test_set, batch_size=64)
class Tudui(nn.Module):
def __init__(self):
super(Tudui, self).__init__()
self.maxpool2d = MaxPool2d(3, ceil_mode=True)
def forward(self, input):
output = self.maxpool2d(input)
return output
writer = SummaryWriter('logs')
step = 1
tudui = Tudui()
for data in dataloader:
imgs, targets = data
writer.add_images('input: ', imgs, step)
output = tudui(imgs)
writer.add_images('output: ', output, step)
step += 1
writer.close()
.conda\envs\pytorch\lib\site-packages\torch\nn\functional.py:780: UserWarning: Note that order of the arguments: ceil_mode and return_indices will changeto match the args list in nn.MaxPool2d in a future release.
warnings.warn("Note that order of the arguments: ceil_mode and return_indices will change"
Process finished with exit code 0
\learn_pytorch> tensorboard --logdir=logs
TensorFlow installation not found - running with reduced feature set.
Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
TensorBoard 2.14.0 at http://localhost:6006/ (Press CTRL+C to quit)