(26)盘古自研框架BackPropagation
我们的盘古自研框架BackPropagation也是同样的Back Propagation梯度下降算法求导计算过程。我们在Forward Propagation前向传播计算使用的是Sigmoid激活函数,即f(x)= 1 / ( 1 +e^(-x) ),因此在Back Propagation反向传播是对Sigmoid进行求导。求导公式为:df(x) /dx = f(x) * ( 1 - f(x))。 如图所示:
图 1- 42梯度求导
循环遍历每一个权重,从前往后依次更新权重。例如:计算更新输入层的第一个输入节点到第一层隐藏层的N[1][1]节点(节点索引值4)的权重(The weight from 1 at layers[0] to 4 at layers[1]),梯度计算为derivative = weight_to_node_error* (weight_to_node_value * (1 - weight_to_node_value
)) * weight_from_node_value , 其他的神经元节点也同样进行求导计算,更新Weight权重的时候derivative乘以learning_rate学习率参数,通过weights[j].set_value(weights[j].get_value() - derivative *learning_rate) 更新权重,这是下次计算的时候能够更加准确的关键,因为把上面的计算成果运用在了调整Neuron Network的Weights上。
我们这里是4条数据,把所有的数据训练一遍,然后进行更新。如果是1亿条数据,可能10万条数据进行更新,10万条数据进行更新,在一个Epoch中运行,这样就实现了整个Back Propagation。如果理解了本节的Back Propagation,也能理解RNN、CNN等深度学习算法,它们基于Back Propagation的过程进行了封装,在Back Propagation的引擎上打造出一个框架算法,CNN就是在本节Back Propagation的基础上,前面加了一个步骤,把图片的数据变成矩阵(0、1)或(0,255)的数字,加了一个预处理的过程,其引擎核心和我们的Back Propagation算法是一样的,包括推荐系统、RNN等都是这样,算法在这个引擎上进行了封装。所以,Back Propagation算法的实现非常难,是最重要的基石基础。
Create_AI_Framework_In5Classes(Day3)版本的BackPropagation.py代码如下:
# -*- coding: utf-8 -*-
from service.ForwardPropagation import ForwardPropagation
#完成Deep Learning Framework中最为核心的功能:Back Propagation:
#第一步: 从误差的结果出发,从最右侧到最左侧(不包含Input Layer)遍历整个Neuron Network构成的Chain;
#第二步:在遍历的时候计算每个Neuron对误差结果应该负的责任;
#第三步:进行Derivative计算;
#第四步:通过Gradient Desendent来调整Weights的值以减少预测的误差
class BackPropagation:
def applyBackPragation(instances, nodes, weights, learning_rate):
num_of_features = len(instances[0]) - 1 #记录输入的Features的个数,instance的最后一列是Real Result
#循环遍历所有的Training Dataset 完成一个Epoch 并进行每个节点所负责的Error的记录
for i in range(len(instances)):
#使用Forward Propagation从Input Layer出发,经过Hidden Layers,最后获得Output
nodes = ForwardPropagation.applyForwardPropagation(nodes, weights, instances[i])
predicted_value = nodes[len(nodes) - 1].get_value() #记录该次Forward Propagation最终的误差
actual_value = instances[i][num_of_features] #获得当前instance的Real Value
minor_error = predicted_value - actual_value #计算预测值和真实值之间的误差
nodes[len(nodes)-1].set_minor_error(minor_error) #把该误差值设置进Output Layer中的输出节点中
#因为输出节点已经计算完误差,所以会减掉2;
#因为Input Layer不参与计算,所以range的三个参数中的第二个参数是num_of_features
#该循环遍历是从Output Layer的前面的一个Hidden Layer开始的,或者说是从最后一个Hidden Layer开始的
for j in range(len(nodes)-2, num_of_features, -1):
target_index = nodes[j].get_index() #从最后一个Hidden Layer的最后一个Neuron开始计算,然后依次向前
sum_minor_error = 0 #存储当前Neuron应该为误差所要负的责任
#循环遍历所有的Weights以获得以target_index为出发点的所有Weights
for k in range(len(weights)):
#如果当前的Weight是以target_index所在的Neuron为出发节点,则说明该Weight需要多结果负(直接)责任
if weights[k].get_from_index() == target_index:
affecting_theta = weights[k].get_value() #获得当前Weight的Value
affected_minor_error = 1 #初始化当前Neuron对结果影响的Value
target_minor_error_index = weights[k].get_to_index() #计算当前Neuron所影响的下一个Layer中具体的Neuron的ID
for m in range(len(nodes)):
if nodes[m].get_index() == target_minor_error_index:
affected_minor_error = nodes[m].get_minor_error()
#获得当前Weight的触发Neuron对结果负责任的具体的值
updated_minor_error = affecting_theta * affected_minor_error
#把对下一个Layer中具体误差负责任的所有误差都累加到当前Neuron并保存到当前的Neuron中
sum_minor_error = sum_minor_error + updated_minor_error
#保存当前的Neuron对下一个Layer的所有的Neurons所造成的Loss影响的总和
nodes[j].set_minor_error(sum_minor_error)
#这里是对我们在ForwardPropagation使用的是Sigmoid Activation,所以这里是对Sigmoid进行求导
# 然后更新Weights!
for j in range(len(weights)):
weight_from_node_value = 0
weight_to_node_value = 0
weight_to_node_error = 0
for k in range(len(nodes)):
if nodes[k].get_index() == weights[j].get_from_index():
weight_from_node_value = nodes[k].get_value()
if nodes[k].get_index() == weights[k].get_to_index():
weight_to_node_value == nodes[k].get_value()
weight_to_node_error = nodes[k].get_minor_error()
#进行求导,因为我们在ForwardPropagation使用的是Sigmoid Activation,所以这里是对Sigmoid进行求导
# Forward Propagation中的Sigmoid代码:target_neuron_output = 1 / (1 + math.exp(- target_neuron_input))
derivative = weight_to_node_error * (weight_to_node_value * (1 - weight_to_node_value
)) * weight_from_node_value
#更新Weight,这是下次计算的时候能够更加准确的关键,因为把上面的计算成果运用在了调整Neuron Network的Weights上
weights[j].set_value(weights[j].get_value() - derivative * learning_rate)
return nodes, weights
在Spyder中运行Neuron_Network_Entry.py代码,运行结果如下:
+1 V1 V2
Hidden layer creation: 1 N[1][1] N[1][2] N[1][3] N[1][4] N[1][5] N[1][6] N[1][7] N[1][8]
Hidden layer creation: 2 N[2][1] N[2][2] N[2][3] N[2][4]
Hidden layer creation: 3 N[3][1] N[3][2]
Output layer: Output
The weight from 1 at layers[0] to 4 at layers[1] : 0.47748147399057483
The weight from 1 at layers[0] to 5 at layers[1] : -0.02110707479923124
The weight from 1 at layers[0] to 6 at layers[1] : 0.7733341095569151
The weight from 1 at layers[0] to 7 at layers[1] : 0.5078644670528534
The weight from 1 at layers[0] to 8 at layers[1] : 0.38637130020873656
The weight from 1 at layers[0] to 9 at layers[1] : -0.02973261521013215
The weight from 1 at layers[0] to 10 at layers[1] : 0.7863564215821441
The weight from 1 at layers[0] to 11 at layers[1] : 0.584129337660958
The weight from 2 at layers[0] to 4 at layers[1] : 0.9662745148343004
The weight from 2 at layers[0] to 5 at layers[1] : -1.0144235052237622
The weight from 2 at layers[0] to 6 at layers[1] : -0.35999827192892087
The weight from 2 at layers[0] to 7 at layers[1] : 0.9452891790847016
The weight from 2 at layers[0] to 8 at layers[1] : 0.8694648449853173
The weight from 2 at layers[0] to 9 at layers[1] : -0.9507722030992092
The weight from 2 at layers[0] to 10 at layers[1] : 0.8597852393070331
The weight from 2 at layers[0] to 11 at layers[1] : 0.36650281313095845
The weight from 4 at layers[1] to 13 at layers[2] : 0.2712530656158141
The weight from 4 at layers[1] to 14 at layers[2] : 0.5925551419309834
The weight from 4 at layers[1] to 15 at layers[2] : 0.5579557294706121
The weight from 4 at layers[1] to 16 at layers[2] : -0.8978389396196884
The weight from 5 at layers[1] to 13 at layers[2] : 0.7277740116885274
The weight from 5 at layers[1] to 14 at layers[2] : 0.15578162972785603
The weight from 5 at layers[1] to 15 at layers[2] : -0.22357710192008196
The weight from 5 at layers[1] to 16 at layers[2] : 0.3453610415981725
The weight from 6 at layers[1] to 13 at layers[2] : 0.550351356582435
The weight from 6 at layers[1] to 14 at layers[2] : 0.5060748969250854
The weight from 6 at layers[1] to 15 at layers[2] : 0.04721947834762541
The weight from 6 at layers[1] to 16 at layers[2] : -0.6677890147939624
The weight from 7 at layers[1] to 13 at layers[2] : -0.05961305347426327
The weight from 7 at layers[1] to 14 at layers[2] : -0.16481629338107584
The weight from 7 at layers[1] to 15 at layers[2] : 0.8813206653228318
The weight from 7 at layers[1] to 16 at layers[2] : -0.8983196364466726
The weight from 8 at layers[1] to 13 at layers[2] : -0.6736643251519103
The weight from 8 at layers[1] to 14 at layers[2] : -0.6541251761441318
The weight from 8 at layers[1] to 15 at layers[2] : -0.20541741096197408
The weight from 8 at layers[1] to 16 at layers[2] : -0.37321077993018303
The weight from 9 at layers[1] to 13 at layers[2] : -0.7878361744196614
The weight from 9 at layers[1] to 14 at layers[2] : 0.33140821180812097
The weight from 9 at layers[1] to 15 at layers[2] : 0.2563864547388408
The weight from 9 at layers[1] to 16 at layers[2] : -1.012102683292297
The weight from 10 at layers[1] to 13 at layers[2] : -0.5448559798520363
The weight from 10 at layers[1] to 14 at layers[2] : -0.11627365788742017
The weight from 10 at layers[1] to 15 at layers[2] : -0.7739561742471903
The weight from 10 at layers[1] to 16 at layers[2] : -0.7129691921855293
The weight from 11 at layers[1] to 13 at layers[2] : -0.4221276548861337
The weight from 11 at layers[1] to 14 at layers[2] : 0.7386904678796575
The weight from 11 at layers[1] to 15 at layers[2] : 0.1229494814415244
The weight from 11 at layers[1] to 16 at layers[2] : -0.38677878819130174
The weight from 13 at layers[2] to 18 at layers[3] : 0.17354990244958124
The weight from 13 at layers[2] to 19 at layers[3] : -0.9227375886179872
The weight from 14 at layers[2] to 18 at layers[3] : 0.30595351638798696
The weight from 14 at layers[2] to 19 at layers[3] : 0.5872177123875555
The weight from 15 at layers[2] to 18 at layers[3] : -0.7038694786418751
The weight from 15 at layers[2] to 19 at layers[3] : -0.770278231422793
The weight from 16 at layers[2] to 18 at layers[3] : 0.7888640549115773
The weight from 16 at layers[2] to 19 at layers[3] : 0.8623348624881537
The weight from 18 at layers[3] to 20 at layers[4] : -0.2648177640124265
The weight from 19 at layers[3] to 20 at layers[4] : -0.798885807955121
Congratulations! Back Propagation is completed!!!
Prediction: 0.3866415296079101 while real value is: 0
Prediction: 0.38814326765954466 while real value is: 1
Prediction: 0.38695158367757343 while real value is: 1
Prediction: 0.3879446913610613 while real value is: 0
如图所示是阿尔法狗的深度学习示意图,阿尔法狗的根据人的经验输入数据,进行监督学习,这个思路和我们的Back Propagation思路完全一样的,阿尔法狗的核心有个步骤,第一个步骤是深度学习,第二个是增强学习,从思想层面,本章节在Demo级别,我们已经完成了阿尔法狗深度学习80%的工作量。
图 1- 43阿尔法狗深度学习
(26)盘古自研框架BackPropagation相关推荐
- java spring源码_剑指Java自研框架,决胜Spring源码
无论是Spring框架源码的学习,还是框架设计,入门门槛都太高,理解起来晦涩.课程想抓住这两个痛点,在自研框架和Spring框架的穿插讲解中让大家逐渐熟悉Spring框架的脉络.通过从0搭建一个较为完 ...
- 自研框架(Webx)整合Zuul网关工作总结
写在前面,最近被分配了一个技术任务,简单描述为自研框架(类比Spring)整合一个微服务网关,并且能用就行. 有人可能会问,想用微服务网关,不是直接引入zuul或者gateway相关的依赖,然后配置一 ...
- 【答读者问26】量化投资框架哪家强?backtrader vs zipline vs 聚宽 vs 米筐
云子量化免费阅读传送链接 今天有读者咨询一个backtrader与聚宽米筐对比的问题,想要了解下backtrader与米筐聚宽各自的优缺点. 先不谈这个问题,我们回顾下初衷,我们想要用这些框架做什么呢 ...
- 借鉴开源框架自研日志收集系统
踏浪无痕 岂安科技高级架构师 十余年数据研发经验,擅长数据处理领域工作,如爬虫.搜索引擎.大数据应用高并发等.担任过架构师,研发经理等岗位.曾主导开发过大型爬虫,搜索引擎及大数据广告DMP系统目前负责 ...
- 2020,国产AI开源框架“亮剑”TensorFlow、PyTorch
「AI技术生态论」 人物访谈栏目是CSDN发起的百万人学AI倡议下的重要组成部分.通过对AI生态专家.创业者.行业KOL的访谈,反映其对于行业的思考.未来趋势的判断.技术的实践,以及成长的经历. 20 ...
- 2021 Facebook 博士奖研金名单出炉:13位华人学者获选
点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 转自 | 机器之心 今年,Facebook 从来自全球百余所大学的 ...
- 清华「计图」、旷视「天元」纷纷重磅开源,国产深度学习框架迎来高光时刻...
来源:CSDN 本文约3141字,建议阅读7分钟. 本文介绍清华开源的深度学习框架 Jittor(计图) 引起了广泛关注,支撑旷视诸多业务和战绩的深度学习框架 MegEngine(天元) 也将在近日开 ...
- 自研Spring容器,带你解析ioc内部原理
spring设计架构 " Test:测试模块 Core container :核心模块 Beans :将对象设置成Beans,Core核心工具类,Context上下文将Beans和Core组 ...
- 国产深度学习框架迎来高光时刻,继清华 Jittor开源后,旷视「天元」纷纷重磅开源!...
关注上方"深度学习技术前沿",选择"星标公众号", 资源干货,第一时间送达! 1956年,美国的达特茅斯会议成为人工智能开启的标志性事件.同年,中国第一批自主制 ...
最新文章
- SAP Spartacus里unit list tree的页面显示和后台响应数据的对应关系
- Java——IO流(序列流)
- 算法基础之搜索和经典排序
- java 析构方法_java析构方法详解
- python改变字符颜色_Python字符串为颜色
- 手机任务栏html,任务栏是什么
- Android面试总结(持续更新修改)
- 蓝牙「5.0」和「4.2」的区别???
- 你应该知道的Android 7.0
- 解决使用vscode写typescript变量报错无法重新声明块范围变量
- HTTP协议与HTTPS协议详解(含常见面试题)
- 计算机桌面黑屏有鼠标,电脑屏幕黑屏但有鼠标指针是怎么回事?
- 9.3 开发经验和屁股的关系——《逆袭大学》连载
- 新手入门必读:一款手游的开发流程是怎样的?
- PDF转Word方法小罗列
- 免费DSP开发板,你想要吗?
- HDOJ 4525 威威猫吃鸡腿
- 计算道路超高lisp_基于Visual LISP的地铁轨道综合图辅助设计程序开发
- 全球及中国水果罐头行业竞争形势及产销需求分析报告2022-2027年
- nba2kol服务器不稳定,nba2kol常见客户端问题有哪些
热门文章
- Android build.gradle配置详解
- C语言电话簿程序设计,2010电话簿管理程序-c语言程序设计-毕业论文.doc
- 靠追热点出圈,网易传媒打造“爆款制造机2.0”
- 全面解析四大主流音频技术
- 圆周率π的几种计算方法与分析
- Linux桌面 失败,ubuntu无法进入桌面,安装ubuntu-desktop失败的解决办法
- 响应式扩展_响应式和无限扩展的JS动画
- 【破茧成蝶-用户体验设计】读书笔记
- excel基础-固定某一列的输入内容
- python-字典练习3 -数字重复统计