用numpy实现MaxPooling

本文详细介绍了如何使用Python和NumPy实现神经网络中的池化层。通过具体代码展示了池化过程,包括输入特征图的接收、池化操作的执行以及输出结果的保存。此过程对于理解深度学习中池化层的作用及其实现方式具有重要价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import numpy as np


def pooling(feature_map, size=2, stride=2):
    channel=feature_map.shape[0]
    height=feature_map.shape[1]
    width=feature_map.shape[2]
    padding_height=np.uint16(round((height-size+1)/stride))
    padding_width=np.uint16(round((width-size+1)/stride))
    print(padding_height,padding_width)

    pool_out = np.zeros((channel,padding_height,padding_width),dtype=np.uint8)
    
    for map_num in range(channel):  
        out_height = 0  
        for r in np.arange(0,height, stride):  
            out_width = 0  
            for c in np.arange(0, width, stride):  
                pool_out[map_num,out_height, out_width] = np.max(feature_map[map_num,r:r+size,c:c+size])  
                out_width=out_width+1
            out_height=out_height+1
    return pool_out

if __name__ == "__main__":
    input=np.fromfile('./file.bin',dtype=np.uint8)
    input=input.reshape((16,608,608))
    output=pooling(input,2,2)

    print(output.dtype)
    print(output.shape)
    output.tofile('./out.bin')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值