二、卷积神经网络
【补1】np.pad(array, pad_width, mode, **kwargs)
对矩阵进行填充,此处用0将图像矩阵进行填充,所用代码为
x_pad[m][n]=np.pad(x[m][n], pad, 'constant', constant_values=0)
当pad为单独一个数时,意味着在原数组上下左右角上填充pad*pad大小的数组,其余位置自动补齐
example:
a=np.ones((2,3))
>>> a
array([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> np.pad(a,2,'constant',constant_values=0)
array([[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 1., 1., 1., 0., 0.],
[ 0., 0., 1., 1., 1., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0.]])
【补2】np.where(condition, [x, y])用于寻找满足特定条件的数组的索引
example:
>>> x=np.arange(9.).reshape(3,3)
>>> x
array([[ 0., 1., 2.],
[ 3., 4., 5.],
[ 6., 7., 8.]])
>>> np.where(x>5)
(array([2, 2, 2], dtype=int64), array([0, 1, 2], dtype=int64))
前面为索引的第一个值,后面的数组对应索引的第二个值,此函数用于max pool反向传播时找到最大值位置用的
网络结构:conv--> relu --> 2*2 max pool --> batch_normalization --> affine-->relu-->affine-->softmax
【layers.py】
1.卷积操作(前向+反向)
def conv_forward_naive(x, w, b, conv_param):
"""
A naive implementation of the forward pass for a convolutional layer.
The input consists of N data points, each with C channels, height H and width
W. We convolve each input with F different filters, where each filter spans
all C channels and has height HH and width HH.
Input:
- x: Input data of shape (N, C, H, W)
- w: Filter weights of 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