Deep Learning Notebook 1: 从零搭建感知机模型

 本文从零开始搭建简单的神经网络模型,明确神经网络模型搭建的步骤,并通过实际编程实现了模型

本文主要介绍从零搭建感知机模型的基本过程,具体步骤如下:

  • 定义网络结构(制定输入层、隐藏层和输出层的大小)

  • 初始化模型参数

  • 循环操作:执行前向传播、计算损失、执行反向传播、权值更新

      接下来便是按照以上步骤进行一个简单地神经网路模型的搭建,由于本文搭建的模型是一个最简单地感知机模型,因此网络结构定义部分就不需要特别的定义了。

01

定义损失函数

   首先,我们采用numpy定义Sigmoid损失函数,具体定义如下:

708ead3c4387b74f8974af5eba5a68b6.png

在无需定义网络结构的情形下,第二步我们就可以直接对模型进行初始化。

02

初始化模型参数

      模型参数主要包括权值w和偏置b,采用numpy对模型参数进行初始化,具体如下:

9ea353674ed15e94041a55fd0555f951.png

       接下来便要进入模型的主体部分,执行最后一步大的循环操作,这个循环操作中包括前向传播和计算损失、反向传播和权值更新。这也是神经网络每一次需要迭代的部分。

03

循环操作

        前向传播简单而言就是计算预测 y 的过程,而反向传播则是根据预测值和实际值之间的误差不断往回推更新权值和偏置的过程,该过程的具体示意如下图所示。

88bf05b6a54e57126e628ba8f1663d52.png

前向传播和反向传播

   下面我们来定义一个大的前向传播函数,预测值 y 为模型从输入到经过激活函数处理后的输出的结果。损失函数我们采用交叉熵损失,利用numpy定义如下函数:

1dac5bf5ebfbcddb87fee1aeec234dd6.png

       在上面的前向传播函数中,我们先是通过激活函数直接表示了感知机输出的预测值,然后通过定义的交叉熵损失函数计算了损失,最后根据损失函数计算了权值 w 和偏置 b 的梯度,将参数梯度结果以字典和损失一起作为函数的输出进行返回。以上便是前向传播代码的思路。

       接下来执行循环操作的第二步:反向传播,计算每一步的当前损失并根据损失对权值进行更新,同样定义一个函数 backward_propagation:

046c0bc08bd950be273096abd8b28b51.png

      在上述函数中,我们先是建立了一个损失列表容器,然后将前一步定义的前向传播函数放进去执行迭代操作,计算每一步的当前损失函数和梯度,利用梯度下降法对权值进行更新,并用字典封装迭代结束时的参数和梯度进行返回。

       如上所示,一个简单的神经网路模型(感知机)就搭建起来了,通常模型搭建好之后我们还需要对测试数据进行预测,所以我们也定义一个预测函数 predict,将模型的概率输出转化为0/1值。

503a49873adb8372cf03defeb5649f8a.png

        到这里整个模型就基本写完了,但是我们定义了这么多函数,调用起来太麻烦,所以需要对这些函数进行一个简单地封装,具体如下:

228d493e76b4a08c75f0b73f6008e6bc.png

如此,便完成了一个简单地神经网路模型的搭建。

欢迎关注

fe30a65f87a9b139045f433b01efbc22.jpeg

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Discover the practical aspects of implementing deep-learning solutions using the rich Python ecosystem. This book bridges the gap between the academic state-of-the-art and the industry state-of-the-practice by introducing you to deep learning frameworks such as Keras, Theano, and Caffe. The practicalities of these frameworks is often acquired by practitioners by reading source code, manuals, and posting questions on community forums, which tends to be a slow and a painful process. Deep Learning with Python allows you to ramp up to such practical know-how in a short period of time and focus more on the domain, models, and algorithms. This book briefly covers the mathematical prerequisites and fundamentals of deep learning, making this book a good starting point for software developers who want to get started in deep learning. A brief survey of deep learning architectures is also included. Deep Learning with Python also introduces you to key concepts of automatic differentiation and GPU computation which, while not central to deep learning, are critical when it comes to conducting large scale experiments. What You Will Learn Leverage deep learning frameworks in Python namely, Keras, Theano, and Caffe Gain the fundamentals of deep learning with mathematical prerequisites Discover the practical considerations of large scale experiments Take deep learning models to production Who This Book Is For Software developers who want to try out deep learning as a practical solution to a particular problem. Software developers in a data science team who want to take deep learning models developed by data scientists to production.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值