0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

【连载】深度学习笔记11:利用numpy搭建一个卷积神经网络

人工智能实训营 2018-10-30 18:50 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

前两个笔记笔者集中探讨了卷积神经网络中的卷积原理,对于二维卷积和三维卷积的原理进行了深入的剖析,对 CNN 的卷积、池化、全连接、滤波器、感受野等关键概念进行了充分的理解。本节内容将继续秉承之前 DNN 的学习路线,在利用 Tensorflow 搭建神经网络之前,先尝试利用 numpy 手动搭建卷积神经网络,以期对卷积神经网络的卷积机制、前向传播和反向传播的原理和过程有更深刻的理解。

单步卷积过程

在正式搭建 CNN 之前,我们先依据前面笔记提到的卷积机制的线性计算的理解,利用 numpy 定义一个单步卷积过程。代码如下:

def conv_single_step(a_slice_prev, W, b):
  s = a_slice_prev * W  # Sum over all entries of the volume s.
  Z = np.sum(s)  # Add bias b to Z. Cast b to a float() so that Z results in a scalar value.
  Z = float(Z + b)  
return Z

在上述的单步卷积定义中,我们传入了一个前一层输入的要进行卷积的区域,即感受野 a_slice_prev ,滤波器 W,即卷积层的权重参数,偏差 b,对其执行 Z=Wx+b 的线性计算即可实现一个单步的卷积过程。

CNN前向传播过程:卷积

正如 DNN 中一样,CNN 即使多了卷积和池化过程,模型仍然是前向传播和反向传播的训练过程。CNN 的前向传播包括卷积和池化两个过程,我们先来看如何利用 numpy 基于上面定义的单步卷积实现完整的卷积过程。卷积计算并不难,我们在单步卷积中就已经实现了,难点在于如何实现滤波器在输入图像矩阵上的的扫描和移动过程。


这其中我们需要搞清楚一些变量和参数,以及每一个输入输出的 shape,这对于我们执行卷积和矩阵相乘至关重要。首先我们的输入是原始图像矩阵,也可以是前一层经过激活后的图像输出矩阵,这里以前一层的激活输出为准,输入像素的 shape 我们必须明确,然后是滤波器矩阵和偏差,还需要考虑步幅和填充,在此基础上我们基于滤波器移动和单步卷积搭建定义如下前向卷积过程:

def conv_forward(A_prev, W, b, hparameters):  
""" Arguments: A_prev -- output activations of the previous layer, numpy array of shape (m, n_H_prev, n_W_prev, n_C_prev) W -- Weights, numpy array of shape (f, f, n_C_prev, n_C) b -- Biases, numpy array of shape (1, 1, 1, n_C) hparameters -- python dictionary containing "stride" and "pad" Returns: Z -- conv output, numpy array of shape (m, n_H, n_W, n_C) cache -- cache of values needed for the conv_backward() function """ # 前一层输入的shape (m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape

# 滤波器权重的shape (f, f, n_C_prev, n_C) = W.shape
# 步幅参数 stride = hparameters['stride']
# 填充参数 pad = hparameters['pad']
# 计算输出图像的高宽 n_H = int((n_H_prev + 2 * pad - f) / stride + 1) n_W = int((n_W_prev + 2 * pad - f) / stride + 1)
# 初始化输出 Z = np.zeros((m, n_H, n_W, n_C))
# 对输入执行边缘填充 A_prev_pad = zero_pad(A_prev, pad)
for i in range(m): a_prev_pad = A_prev_pad[i, :, :, :] for h in range(n_H): for w in range(n_W): for c in range(n_C): # 滤波器在输入图像上扫描 vert_start = h * stride vert_end = vert_start + f horiz_start = w * stride horiz_end = horiz_start + f
# 定义感受野 a_slice_prev = a_prev_pad[vert_start : vert_end, horiz_start : horiz_end, :] # 对感受野执行单步卷积 Z[i, h, w, c] = conv_single_step(a_slice_prev, W[:,:,:,c], b[:,:,:,c])
assert(Z.shape == (m, n_H, n_W, n_C)) cache = (A_prev, W, b, hparameters)
return Z, cache

这样,卷积神经网络前向传播中一个完整的卷积计算过程就被我们定义好了。通常而言,我们也会对卷积后输出加一个 relu 激活操作,正如前面的图2所示,这里我们就省略不加了。

CNN前向传播过程:池化

池化简单而言就是取局部区域最大值,池化的前向传播跟卷积过程类似,但相对简单一点,无需执行单步卷积那样的乘积运算。同样需要注意的是各参数和输入输出的 shape,因此我们定义如下前向传播池化过程:

def pool_forward(A_prev, hparameters, mode = "max"):  
""" Arguments: A_prev -- Input data, numpy array of shape (m, n_H_prev, n_W_prev, n_C_prev) hparameters -- python dictionary containing "f" and "stride" mode -- the pooling mode you would like to use, defined as a string ("max" or "average") Returns: A -- output of the pool layer, a numpy array of shape (m, n_H, n_W, n_C) cache -- cache used in the backward pass of the pooling layer, contains the input and hparameters """ # 前一层输入的shape (m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape
# 步幅和权重参数 f = hparameters["f"] stride = hparameters["stride"]
# 计算输出图像的高宽 n_H = int(1 + (n_H_prev - f) / stride) n_W = int(1 + (n_W_prev - f) / stride) n_C = n_C_prev
# 初始化输出 A = np.zeros((m, n_H, n_W, n_C)) for i in range(m): for h in range(n_H): for w in range(n_W): for c in range (n_C): # 树池在输入图像上扫描 vert_start = h * stride vert_end = vert_start + f horiz_start = w * stride horiz_end = horiz_start + f
# 定义池化区域 a_prev_slice = A_prev[i, vert_start:vert_end, horiz_start:horiz_end, c]
# 选择池化类型 if mode == "max": A[i, h, w, c] = np.max(a_prev_slice)
elif mode == "average": A[i, h, w, c] = np.mean(a_prev_slice) cache = (A_prev, hparameters)
assert(A.shape == (m, n_H, n_W, n_C))
return A, cache

由上述代码结构可以看出,前向传播的池化过程的代码结构和卷积过程非常类似。

CNN反向传播过程:卷积

定义好前向传播之后,难点和关键点就在于如何给卷积和池化过程定义反向传播过程。卷积层的反向传播向来是个复杂的过程,Tensorflow 中我们只要定义好前向传播过程,反向传播会自动进行计算。但利用 numpy 搭建 CNN 反向传播就还得我们自己定义了。其关键还是在于准确的定义损失函数对于各个变量的梯度:
640?wx_fmt=png

640?wx_fmt=png

640?wx_fmt=png
由上述梯度计算公式和卷积的前向传播过程,我们定义如下卷积的反向传播函数:

def conv_backward(dZ, cache):  """
  Arguments:
  dZ -- gradient of the cost with respect to the output of the conv layer (Z), numpy array of shape (m, n_H, n_W, n_C)
  cache -- cache of values needed for the conv_backward(), output of conv_forward()

  Returns:
  dA_prev -- gradient of the cost with respect to the input of the conv layer (A_prev),
        numpy array of shape (m, n_H_prev, n_W_prev, n_C_prev)
  dW -- gradient of the cost with respect to the weights of the conv layer (W)
     numpy array of shape (f, f, n_C_prev, n_C)
  db -- gradient of the cost with respect to the biases of the conv layer (b)
     numpy array of shape (1, 1, 1, n_C)
  """
  # 获取前向传播中存储的cache
  (A_prev, W, b, hparameters) = cache  
# 前一层输入的shape (m, n_H_prev, n_W_prev, n_C_prev) = A_prev.shape
# 滤波器的 shape (f, f, n_C_prev, n_C) = W.shape
# 步幅和权重参数 stride = hparameters['stride'] pad = hparameters['pad']
# dZ 的shape (m, n_H, n_W, n_C) = dZ.shape
# 初始化 dA_prev, dW, db dA_prev = np.zeros((m, n_H_prev, n_W_prev, n_C_prev)) dW = np.zeros((f, f, n_C_prev, n_C)) db = np.zeros((1, 1, 1, n_C))
# 对A_prev 和 dA_prev 执行零填充 A_prev_pad = zero_pad(A_prev, pad) dA_prev_pad = zero_pad(dA_prev, pad)
for i in range(m): # select ith training example from A_prev_pad and dA_prev_pad a_prev_pad = A_prev_pad[i,:,:,:] da_prev_pad = dA_prev_pad[i,:,:,:]
for h in range(n_H): for w in range(n_W): for c in range(n_C): # 获取当前感受野 vert_start = h * stride vert_end = vert_start + f horiz_start = w * stride horiz_end = horiz_start + f
# 获取当前滤波器矩阵 a_slice = a_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :]
# 梯度更新 da_prev_pad[vert_start:vert_end, horiz_start:horiz_end, :] += W[:,:,:,c] * dZ[i, h, w, c] dW[:,:,:,c] += a_slice * dZ[i, h, w, c] db[:,:,:,c] += dZ[i, h, w, c] dA_prev[i, :, :, :] = da_prev_pad[pad:-pad, pad:-pad, :]
assert(dA_prev.shape == (m, n_H_prev, n_W_prev, n_C_prev))
return dA_prev, dW, db
CNN反向传播过程:池化

反向传播中的池化操作跟卷积也是类似的。再此之前,我们需要根据滤波器为最大池化和平均池化分别创建一个 mask 和一个 distribute_value :

def create_mask_from_window(x):  
""" Creates a mask from an input matrix x, to identify the max entry of x. Arguments: x -- Array of shape (f, f) Returns: mask -- Array of the same shape as window, contains a True at the position corresponding to the max entry of x. """ mask = (x == np.max(x))
return mask
def distribute_value(dz, shape):  
""" Distributes the input value in the matrix of dimension shape Arguments: dz -- input scalar shape -- the shape (n_H, n_W) of the output matrix for which we want to distribute the value of dz Returns: a -- Array of size (n_H, n_W) for which we distributed the value of dz """ (n_H, n_W) = shape
# Compute the value to distribute on the matrix average = dz / (n_H * n_W)
# Create a matrix where every entry is the "average" value a = np.full(shape, average)
return a

然后整合封装最大池化的反向传播过程:

def pool_backward(dA, cache, mode = "max"):  
""" Arguments: dA -- gradient of cost with respect to the output of the pooling layer, same shape as A cache -- cache output from the forward pass of the pooling layer, contains the layer's input and hparameters mode -- the pooling mode you would like to use, defined as a string ("max" or "average") Returns: dA_prev -- gradient of cost with respect to the input of the pooling layer, same shape as A_prev """ # Retrieve information from cache (A_prev, hparameters) = cache
# Retrieve hyperparameters from "hparameters" stride = hparameters['stride'] f = hparameters['f']
# Retrieve dimensions from A_prev's shape and dA's shape m, n_H_prev, n_W_prev, n_C_prev = A_prev.shape m, n_H, n_W, n_C = dA.shape
# Initialize dA_prev with zeros dA_prev = np.zeros((m, n_H_prev, n_W_prev, n_C_prev))
for i in range(m): # select training example from A_prev a_prev = A_prev[i,:,:,:]
for h in range(n_H): for w in range(n_W): for c in range(n_C): # Find the corners of the current "slice" vert_start = h * stride vert_end = vert_start + f horiz_start = w * stride horiz_end = horiz_start + f
# Compute the backward propagation in both modes. if mode == "max": a_prev_slice = a_prev[vert_start:vert_end, horiz_start:horiz_end, c] mask = create_mask_from_window(a_prev_slice) dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += np.multiply(mask, dA[i,h,w,c]) elif mode == "average": # Get the value a from dA da = dA[i,h,w,c]
# Define the shape of the filter as fxf shape = (f,f)
# Distribute it to get the correct slice of dA_prev. i.e. Add the distributed value of da. dA_prev[i, vert_start: vert_end, horiz_start: horiz_end, c] += distribute_value(da, shape)
# Making sure your output shape is correct assert(dA_prev.shape == A_prev.shape)
return dA_prev

这样卷积神经网络的整个前向传播和反向传播过程我们就搭建好了。可以说是非常费力的操作了,但我相信,经过这样一步步的根据原理的手写,你一定会对卷积神经网络的原理理解更加深刻了。

本文由《自兴动脑人工智能》项目部 凯文 投稿。



声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 神经网络
    +关注

    关注

    42

    文章

    4829

    浏览量

    106891
  • 人工智能
    +关注

    关注

    1813

    文章

    49785

    浏览量

    261924
  • 机器学习
    +关注

    关注

    66

    文章

    8541

    浏览量

    136283
  • 深度学习
    +关注

    关注

    73

    文章

    5591

    浏览量

    123971
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    自动驾驶中常提的卷积神经网络啥?

    在自动驾驶领域,经常会听到卷积神经网络技术。卷积神经网络,简称为CNN,是种专门用来处理网格状数据(比如图像)的
    的头像 发表于 11-19 18:15 1876次阅读
    自动驾驶中常提的<b class='flag-5'>卷积</b><b class='flag-5'>神经网络</b>是<b class='flag-5'>个</b>啥?

    CNN卷积神经网络设计原理及在MCU200T上仿真测试

    数的提出很大程度的解决了BP算法在优化深层神经网络时的梯度耗散问题。当x&gt;0 时,梯度恒为1,无梯度耗散问题,收敛快;当x&lt;0 时,该层的输出为0。 CNN
    发表于 10-29 07:49

    NMSIS神经网络库使用介绍

    (q7_t) 和 16 位整数 (q15_t)。 卷积神经网络示例: 本示例中使用的 CNN 基于来自 Caffe 的 CIFAR-10 示例。神经网络由 3
    发表于 10-29 06:08

    构建CNN网络模型并优化的般化建议

    整个模型非常巨大。所以要想实现轻量级的CNN神经网络模型,首先应该避免尝试单层神经网络。 2)减少卷积核的大小:CNN神经网络是通过权值共享的方式,
    发表于 10-28 08:02

    从零开始利用NMSIS库搭建神经网络

    环境:Vivado2021.1、NucleiStudio_IDE_202102-win64 内容:从零开始利用NMSIS库搭建神经网络,这节主讲基本的NMSIS库
    发表于 10-24 13:47

    在Ubuntu20.04系统中训练神经网络模型的些经验

    模型。 我们使用MNIST数据集,训练卷积神经网络(CNN)模型,用于手写数字识别。旦模型被训练并保存,就可以用于对新图像进行推理和预
    发表于 10-22 07:03

    CICC2033神经网络部署相关操作

    在完成神经网络量化后,需要将神经网络部署到硬件加速器上。首先需要将所有权重数据以及输入数据导入到存储器内。 在仿真环境下,可将其存于文件,并在 Verilog 代码中通过 read
    发表于 10-20 08:00

    如何在机器视觉中部署深度学习神经网络

    图 1:基于深度学习的目标检测可定位已训练的目标类别,并通过矩形框(边界框)对其进行标识。 在讨论人工智能(AI)或深度学习时,经常会出现“神经网络
    的头像 发表于 09-10 17:38 723次阅读
    如何在机器视觉中部署<b class='flag-5'>深度</b><b class='flag-5'>学习</b><b class='flag-5'>神经网络</b>

    基于FPGA搭建神经网络的步骤解析

    本文的目的是在神经网络已经通过python或者MATLAB训练好的神经网络模型,将训练好的模型的权重和偏置文件以TXT文件格式导出,然后通过python程序将txt文件转化为coe
    的头像 发表于 06-03 15:51 926次阅读
    基于FPGA<b class='flag-5'>搭建</b><b class='flag-5'>神经网络</b>的步骤解析

    BP神经网络卷积神经网络的比较

    BP神经网络卷积神经网络在多个方面存在显著差异,以下是对两者的比较: 、结构特点 BP神经网络 : BP
    的头像 发表于 02-12 15:53 1383次阅读

    如何优化BP神经网络学习

    优化BP神经网络学习率是提高模型训练效率和性能的关键步骤。以下是些优化BP神经网络学习率的方法:
    的头像 发表于 02-12 15:51 1468次阅读

    BP神经网络的优缺点分析

    BP神经网络(Back Propagation Neural Network)作为种常用的机器学习模型,具有显著的优点,同时也存在些不容忽视的缺点。以下是对BP
    的头像 发表于 02-12 15:36 1632次阅读

    BP神经网络深度学习的关系

    ),是种多层前馈神经网络,它通过反向传播算法进行训练。BP神经网络由输入层、或多个隐藏层和输出层组成,通过逐层递减的方式调整
    的头像 发表于 02-12 15:15 1385次阅读

    深度学习入门:简单神经网络的构建与实现

    深度学习中,神经网络是核心模型。今天我们用 Python 和 NumPy 构建简单的
    的头像 发表于 01-23 13:52 861次阅读

    人工神经网络的原理和多种神经网络架构方法

    在上篇文章中,我们介绍了传统机器学习的基础知识和多种算法。在本文中,我们会介绍人工神经网络的原理和多种神经网络架构方法,供各位老师选择。 01 人工
    的头像 发表于 01-09 10:24 2299次阅读
    人工<b class='flag-5'>神经网络</b>的原理和多种<b class='flag-5'>神经网络</b>架构方法