pytorch 对 maxpooling实现的理解

  • Post author:
  • Post category:其他




1.源码

	def maxpool(input):
		if pool_type == 'max':
			return torch.max(input, 2)[0].contiguous()



2.测试输入

x2=torch.rand((2,2,2,3))

随机值为

tensor([[[[0.9475, 0.5645, 0.1647],
          [0.4556, 0.1263, 0.9549]],

         [[0.7462, 0.6263, 0.5288],
          [0.2702, 0.1502, 0.1599]]],


        [[[0.8772, 0.2693, 0.8170],
          [0.4525, 0.6906, 0.9137]],

         [[0.1647, 0.6652, 0.8407],
          [0.0336, 0.9208, 0.7451]]]])



3.torch.max()的原理解释



torch.max(input, dim, keepdim=False, out=None)



按维度dim 返回最大值,并且返回索引。

参考:

Pytorch笔记torch.max()


对一个维度为


(2,3)


的二维数组,如


[[0.1,0.2,0.3],[0.4,0.5,0.6]]


,


torch.max(x,0)


表示返回每一列中最大值的那个元素;


torch.max(x,1)


表示返回每一行中最大值的那个元素



torch.max(input, 2)[0]


表示返回 value;



torch.max(input, 2)[1]


表示返回 indices;

例子见:

Python-torch.max()

torch.contiguous()方法首先拷贝了一份张量在内存中的地址,然后将地址按照形状改变后的张量的语义进行排列。



4.测试输出

y=maxpool(x2)

输出y

tensor([[[0.9475, 0.5645, 0.9549],
         [0.7462, 0.6263, 0.5288]],

        [[0.8772, 0.6906, 0.9137],
         [0.1647, 0.9208, 0.8407]]])



版权声明:本文为goodgoodstudy___原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。