class torch.nn.MaxPool2d(kernel_size, stride=None, padding=0, dilation=1, return_indices=False, ceil_mode=False)
参数:#
- kernel_size(int or tuple) - max pooling的窗口大小
- stride(int or tuple, optional) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(int or tuple, optional) - 输入的每一条边补充0的层数
- dilation(int or tuple, optional) – mask元素间的距离
- return_indices - 如果等于True,会返回输出最大值的序号,对于上采样操作会有帮助
- ceil_mode - 如果等于True,计算输出信号大小的时候,会使用向上取整,代替默认的向下取整的操作
input:#
- input(batch_size,channels,H_in,W_in)
output:
- output(batch_size,channels,H_out,W_out)
也就是说输入输出都是个4维张量
上池化:
这个主要用于误差反向传播从池化层到卷积层,如果是Rule激活函数池化层到卷积层的误差传播就是在原有误差的基础上上池化
class torch.nn.MaxUnpool2d(kernel_size, stride=None, padding=0)
参数:
- kernel_size(int or tuple) - max pooling的窗口大小
- stride(int or tuple, optional) - max pooling的窗口移动的步长。默认值是kernel_size
- padding(int or tuple, optional) - 输入的每一条边补充0的层数
输入参数:
input:
张量tensor,包含(batch_size,channels,H_in,W_in)
indices:
Maxpool1d的索引号
output_size:
一个指定输出大小的torch.Size
输出:
output
:(batch_size,channels,H_out,W_out)
也就是说池化层上采样输入与输出的张量中都包含了批次样本数目,通道数,输出的尺寸
其实DNN网络除了全连接层都包含了批次样本数目,通道数目,样本的维数(这里除掉通道),三维池化层输入的是个5维张量,输出的也是个5维张量,以此类推
import torch
from torch.autograd import Variable
import torch.nn as nn
import numpy as np
m = nn.MaxPool2d(2,1,return_indices=True)
print('模拟二维池化和上池化')
#模拟定义输入图像矩阵
r = np.random.randint(10,size=(1,4,3,3))
print(r)
#torch.set_default_dtype(torch.float64)
#numpy数组转张量
r = torch.from_numpy(r)
#int 转float
newr = r.float()
print(newr)
#OpData,indices = m(newr)
print('查看池化后元组')
a = m(newr)
print(a)
print('查看最大池化后数据')
#print(OpData)
#print('查看池化后索引')
print(indices)
print(a[0])
print('查看池化后索引')
print(a[1])
print('在原有最大池化的基础上上池化')
upsample = nn.MaxUnpool2d(2,1)
print(upsample(a[0],a[1]))
模拟二维池化和上池化
[[[[7 8 0]
[6 8 2]
[8 7 7]]
[[0 2 7]
[9 9 3]
[1 0 7]]
[[2 1 1]
[0 5 9]
[0 3 8]]
[[7 5 6]
[6 5 8]
[6 1 4]]]]
tensor([[[[7., 8., 0.],
[6., 8., 2.],
[8., 7., 7.]],
[[0., 2., 7.],
[9., 9., 3.],
[1., 0., 7.]],
[[2., 1., 1.],
[0., 5., 9.],
[0., 3., 8.]],
[[7., 5., 6.],
[6., 5., 8.],
[6., 1., 4.]]]], dtype=torch.float32)
查看池化后元组
(tensor([[[[8., 8.],
[8., 8.]],
[[9., 9.],
[9., 9.]],
[[5., 9.],
[5., 9.]],
[[7., 8.],
[6., 8.]]]], dtype=torch.float32), tensor([[[[1, 1],
[4, 4]],
[[3, 4],
[3, 4]],
[[4, 5],
[4, 5]],
[[0, 5],
[3, 5]]]]))
查看最大池化后数据
tensor([[[[4, 4],
[6, 4]],
[[1, 1],
[6, 4]],
[[4, 5],
[4, 5]],
[[3, 5],
[3, 5]]]])
tensor([[[[8., 8.],
[8., 8.]],
[[9., 9.],
[9., 9.]],
[[5., 9.],
[5., 9.]],
[[7., 8.],
[6., 8.]]]], dtype=torch.float32)
查看池化后索引
tensor([[[[1, 1],
[4, 4]],
[[3, 4],
[3, 4]],
[[4, 5],
[4, 5]],
[[0, 5],
[3, 5]]]])
在原有最大池化的基础上上池化
tensor([[[[0., 8., 0.],
[0., 8., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[9., 9., 0.],
[0., 0., 0.]],
[[0., 0., 0.],
[0., 5., 9.],
[0., 0., 0.]],
[[7., 0., 0.],
[6., 0., 8.],
[0., 0., 0.]]]], dtype=torch.float32)
网友评论