python里反向传播算法详解


Posted in Python onNovember 22, 2020

反向传播的目的是计算成本函数C对网络中任意w或b的偏导数。一旦我们有了这些偏导数,我们将通过一些常数 α的乘积和该数量相对于成本函数的偏导数来更新网络中的权重和偏差。这是流行的梯度下降算法。而偏导数给出了最大上升的方向。因此,关于反向传播算法,我们继续查看下文。

我们向相反的方向迈出了一小步——最大下降的方向,也就是将我们带到成本函数的局部最小值的方向。

图示演示:

python里反向传播算法详解

反向传播算法中Sigmoid函数代码演示:

# 实现 sigmoid 函数
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
# sigmoid 导数的计算
return sigmoid(x)*(1-sigmoid(x))

反向传播算法中ReLU 函数导数函数代码演示:

def relu_derivative(x): # ReLU 函数的导数
d = np.array(x, copy=True) # 用于保存梯度的张量
d[x < 0] = 0 # 元素为负的导数为 0
d[x >= 0] = 1 # 元素为正的导数为 1
return d

实例扩展:

BP反向传播算法Python简单实现

import numpy as np

# "pd" 偏导
def sigmoid(x):
  return 1 / (1 + np.exp(-x))

def sigmoidDerivationx(y):
  return y * (1 - y)


if __name__ == "__main__":
  #初始化
  bias = [0.35, 0.60]
  weight = [0.15, 0.2, 0.25, 0.3, 0.4, 0.45, 0.5, 0.55]
  output_layer_weights = [0.4, 0.45, 0.5, 0.55]
  i1 = 0.05
  i2 = 0.10
  target1 = 0.01
  target2 = 0.99
  alpha = 0.5 #学习速率
  numIter = 10000 #迭代次数
  for i in range(numIter):
    #正向传播
    neth1 = i1*weight[1-1] + i2*weight[2-1] + bias[0]
    neth2 = i1*weight[3-1] + i2*weight[4-1] + bias[0]
    outh1 = sigmoid(neth1)
    outh2 = sigmoid(neth2)
    neto1 = outh1*weight[5-1] + outh2*weight[6-1] + bias[1]
    neto2 = outh2*weight[7-1] + outh2*weight[8-1] + bias[1]
    outo1 = sigmoid(neto1)
    outo2 = sigmoid(neto2)
    print(str(i) + ", target1 : " + str(target1-outo1) + ", target2 : " + str(target2-outo2))
    if i == numIter-1:
      print("lastst result : " + str(outo1) + " " + str(outo2))
    #反向传播
    #计算w5-w8(输出层权重)的误差
    pdEOuto1 = - (target1 - outo1)
    pdOuto1Neto1 = sigmoidDerivationx(outo1)
    pdNeto1W5 = outh1
    pdEW5 = pdEOuto1 * pdOuto1Neto1 * pdNeto1W5
    pdNeto1W6 = outh2
    pdEW6 = pdEOuto1 * pdOuto1Neto1 * pdNeto1W6
    pdEOuto2 = - (target2 - outo2)
    pdOuto2Neto2 = sigmoidDerivationx(outo2)
    pdNeto1W7 = outh1
    pdEW7 = pdEOuto2 * pdOuto2Neto2 * pdNeto1W7
    pdNeto1W8 = outh2
    pdEW8 = pdEOuto2 * pdOuto2Neto2 * pdNeto1W8

    # 计算w1-w4(输出层权重)的误差
    pdEOuto1 = - (target1 - outo1) #之前算过
    pdEOuto2 = - (target2 - outo2) #之前算过
    pdOuto1Neto1 = sigmoidDerivationx(outo1)  #之前算过
    pdOuto2Neto2 = sigmoidDerivationx(outo2)  #之前算过
    pdNeto1Outh1 = weight[5-1]
    pdNeto2Outh2 = weight[7-1]

    pdEOuth1 = pdEOuto1 * pdOuto1Neto1 * pdNeto1Outh1 + pdEOuto2 * pdOuto2Neto2 * pdNeto1Outh1
    pdOuth1Neth1 = sigmoidDerivationx(outh1)
    pdNeth1W1 = i1
    pdNeth1W2 = i2
    pdEW1 = pdEOuth1 * pdOuth1Neth1 * pdNeth1W1
    pdEW2 = pdEOuth1 * pdOuth1Neth1 * pdNeth1W2
    pdNeto1Outh2 = weight[6-1]
    pdNeto2Outh2 = weight[8-1]
    pdOuth2Neth2 = sigmoidDerivationx(outh2)
    pdNeth2W3 = i1
    pdNeth2W4 = i2
    pdEOuth2 = pdEOuto1 * pdOuto1Neto1 * pdNeto1Outh2 + pdEOuto2 * pdOuto2Neto2 * pdNeto2Outh2
    pdEW3 = pdEOuth2 * pdOuth2Neth2 * pdNeth2W3
    pdEW4 = pdEOuth2 * pdOuth2Neth2 * pdNeth2W4
    #权重更新
    weight[1-1] = weight[1-1] - alpha * pdEW1
    weight[2-1] = weight[2-1] - alpha * pdEW2
    weight[3-1] = weight[3-1] - alpha * pdEW3
    weight[4-1] = weight[4-1] - alpha * pdEW4
    weight[5-1] = weight[5-1] - alpha * pdEW5
    weight[6-1] = weight[6-1] - alpha * pdEW6
    weight[7-1] = weight[7-1] - alpha * pdEW7
    weight[8-1] = weight[8-1] - alpha * pdEW8
    # print(weight[1-1])
    # print(weight[2-1])
    # print(weight[3-1])
    # print(weight[4-1])
    # print(weight[5-1])
    # print(weight[6-1])
    # print(weight[7-1])
    # print(weight[8-1])

到此这篇关于python里反向传播算法详解的文章就介绍到这了,更多相关python里反向传播算法是什么内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
Python 搭建Web站点之Web服务器与Web框架
Nov 06 Python
Python入门_浅谈数据结构的4种基本类型
May 16 Python
Python模拟脉冲星伪信号频率实例代码
Jan 03 Python
python删除某个字符
Mar 19 Python
使用pandas把某一列的字符值转换为数字的实例
Jan 29 Python
PyQt5组件读取参数的实例
Jun 25 Python
python3 自动打印出最新版本执行的mysql2redis实例
Apr 09 Python
Python基于yaml文件配置logging日志过程解析
Jun 23 Python
详解Python中list[::-1]的几种用法
Nov 16 Python
详解Python爬虫爬取博客园问题列表所有的问题
Jan 18 Python
python自动化调用百度api解决验证码
Apr 13 Python
Python中json.dumps()函数的使用解析
May 17 Python
python动态规划算法实例详解
Nov 22 #Python
python全栈开发语法总结
Nov 22 #Python
scrapy在python爬虫中搭建出错的解决方法
Nov 22 #Python
一篇文章教你用python画动态爱心表白
Nov 22 #Python
python中scrapy处理项目数据的实例分析
Nov 22 #Python
python eventlet绿化和patch原理
Nov 21 #Python
python 实用工具状态机transitions
Nov 21 #Python
You might like
php实现修改新闻时删除图片的方法
2015/05/12 PHP
php实现登录tplink WR882N获取IP和重启的方法
2016/07/20 PHP
php  单例模式详细介绍及实现源码
2016/11/05 PHP
yii2多图上传组件的使用教程
2018/05/10 PHP
基于jQuery的ajax功能实现web service的json转化
2009/08/29 Javascript
jquery checkbox全选、取消全选实现代码
2010/03/05 Javascript
动态显示可输入的字数提示还可以输入的字数
2014/04/01 Javascript
jQuery插件编写步骤详解
2016/06/03 Javascript
NodeJS与HTML5相结合实现拖拽多个文件上传到服务器的实现方法
2016/07/26 NodeJs
Spring shiro + bootstrap + jquery.validate 实现登录、注册功能
2017/06/02 jQuery
nodejs操作mongodb的填删改查模块的制作及引入实例
2018/01/02 NodeJs
详解使用mpvue开发github小程序总结
2018/07/25 Javascript
加快Vue项目的开发速度的方法
2018/12/12 Javascript
详解React项目中碰到的IE问题
2019/03/14 Javascript
js中Generator函数的深入讲解
2019/04/07 Javascript
原生js+css调节音量滑块
2020/01/15 Javascript
Javascript原生ajax请求代码实例
2020/02/20 Javascript
微信小程序文章详情功能完整实例
2020/06/03 Javascript
Python计算已经过去多少个周末的方法
2015/07/25 Python
Python WXPY实现微信监控报警功能的代码
2017/10/20 Python
详解python和matlab的优势与区别
2019/06/28 Python
django的csrf实现过程详解
2019/07/26 Python
Jupyter notebook如何修改平台字体
2020/05/13 Python
浅谈tensorflow模型保存为pb的各种姿势
2020/05/25 Python
树莓派4B安装Tensorflow的方法步骤
2020/07/16 Python
18-35岁旅游团的全球领导者:Contiki
2017/02/08 全球购物
在加拿大在线租赁和购买电子游戏:Game Access
2019/09/02 全球购物
Deux par Deux官方网站:设计师童装
2020/01/03 全球购物
世界上最大的皮肤科医生拥有和经营的美容网站:LovelySkin
2021/01/03 全球购物
工地门卫岗位职责范本
2014/07/01 职场文书
分公司总经理岗位职责
2014/08/03 职场文书
退学证明范本3篇
2014/10/29 职场文书
2015年国庆晚会主持词
2015/07/01 职场文书
文明礼貌主题班会
2015/08/14 职场文书
nginx负载功能+nfs服务器功能解析
2022/02/28 Servers
Redis中有序集合的内部实现方式的详细介绍
2022/03/16 Redis