Convolution

2019-10-10  本文已影响0人  wzNote

https://nicehuster.github.io/2018/08/31/python_bn_conv/

对图像(不同的数据窗口数据)和滤波矩阵(一组固定的权重:因为每个神经元的权重固定,所以又可以看做一个恒定的滤波器filter)做内积(逐个元素相乘再求和)的操作就是所谓的『卷积』操作,也是卷积神经网络的名字来源。

python实现

def conv_forward_naive(x,w,b,conv_param):
    
    """
    Input:
    - x: Input data with shape (N, C, H, W)
    - w: Filter weights with shape (F, C, HH, WW)
    - b: Biases, of shape (F,)
    - conv_param: A dictionary with the following keys:
        - 'stride': The number of pixels between adjacent receptive fields in the
      horizontal and vertical directions.
        - 'pad': The number of pixels that will be used to zero-pad the input.
    Returns a tuple of:
    - out: Output data, of shape (N, F, H', W') where H' and W' are given by
        H' = 1 + (H + 2 * pad - HH) / stride
        W' = 1 + (W + 2 * pad - WW) / stride
    - cache: (x, w, b, conv_param)
    """
    out = None
    N, C, H, W=x.shape
    F, _, HH, WW=w.shape
    S,P = conv_param['stride'],conv_param['pad']
    Ho =int( 1 + (H + 2 * P - HH) / S)
    Wo = int(1 + (W + 2 * P - WW) / S)
    out=np.zeros((N,F,Ho,Wo))
    
    x_pad=np.zeros((N,C,H+2*P,W+2*P))
    x_pad[:,:,P:P+H,P:P+W]=x
    
    
    for i in range(Ho):
        for j in range(Wo):
            x_pad_mask=x_pad[:,:,i*S:i*S+HH,j*S:j*S+WW]
            for f in range(F):
                out[:,f,i,j]=np.sum(x_pad_mask*w[f,:,:,:],axis=(1,2,3))#逐通道相加
                
    out = out + (b)[None, :, None, None]
                
    
    cache=(x,w,b,conv_param)
    
    return out,cache
上一篇 下一篇

猜你喜欢

热点阅读