练习:卷积和池化过程中注意事项

元素化相乘

注意的是,我们在使用滤波器进行卷积的时候,是元素化相乘的,所以,要使用*,而不要使用np.multiply

卷积切片和池化切片

我们基于图像学习来讨论这个问题。

对于多维参数输入而言,比如说,我们是A=(4,5,5,3)表示的的有4个输入图像,每个图像是5×5,因为是RGB图像,所以是有3层。

对于卷积而言,我们通常是三维卷积,所以,卷积切片代码这样写:

  for i in range(m):                               # loop over the batch of training examples
        a_prev_pad = A_prev_pad[i]                               # Select ith training example's padded activation
        for h in range(n_H):                           # loop over vertical axis of the output volume
            for w in range(n_W):                       # loop over horizontal axis of the output volume
                for c in range(n_C):                   # loop over channels (= #filters) of the output volume

                    # Find the corners of the current "slice" (≈4 lines)
                    vert_start = h*stride
                    vert_end = vert_start + f
                    horiz_start = w*stride
                    horiz_end = horiz_start + f

                    # Use the corners to define the (3D) slice of a_prev_pad (See Hint above the cell). (≈1 line)
                    a_slice_prev = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]

但是池化不是三维池化,所以最后一行代码我们不是写成:而是写成c的形式,表示对某一通道进行切片操作,我们举一个例子

A_prev = np.random.randn(10,4,4,3)
A_prev = A_prev[1,1:3,1:3,2]
print(A_prev)
print(A_prev.shape)

输出效果如下:

[[ 1.07125243 -1.03918232]
 [ 1.2066079   1.06897162]]
(2, 2)

下面是池化层代码

   for i in range(m):                         # loop over the training examples
        for h in range(n_H):                     # loop on the vertical axis of the output volume
            for w in range(n_W):                 # loop on the horizontal axis of the output volume
                for c in range (n_C):            # loop over the channels of the output volume

                    # Find the corners of the current "slice" (≈4 lines)
                    vert_start = h*stride
                    vert_end = vert_start + f
                    horiz_start = w*stride
                    horiz_end = horiz_start + f

                    # Use the corners to define the current slice on the ith training example of A_prev, channel c. (≈1 line)

                    # 不切片的我们写成i的形式进行保留,池化是一个二维池化,而不是三维池化,所以我们这里写成c的形式,不进行切片
                    a_prev_slice = A_prev[i, vert_start:vert_end, horiz_start:horiz_end, c]

你可能感兴趣的:(深度学习)