时间:2021-05-22
反向传播的目的是计算成本函数C对网络中任意w或b的偏导数。一旦我们有了这些偏导数,我们将通过一些常数 α的乘积和该数量相对于成本函数的偏导数来更新网络中的权重和偏差。这是流行的梯度下降算法。而偏导数给出了最大上升的方向。因此,关于反向传播算法,我们继续查看下文。
我们向相反的方向迈出了一小步——最大下降的方向,也就是将我们带到成本函数的局部最小值的方向。
图示演示:
反向传播算法中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 # 元素为负的导数为 0d[x >= 0] = 1 # 元素为正的导数为 1return 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里反向传播算法是什么内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
实现神经网络的权重和偏置更新,很重要的一部就是使用BackPropagation(反向传播)算法。具体来说,反向传播算法就是用误差的反向传播来计算w(权重)和b
本文实例讲述了Python实现的三层BP神经网络算法。分享给大家供大家参考,具体如下:这是一个非常漂亮的三层反向传播神经网络的python实现,下一步我准备试着
使用TensorFlow的一个优势是,它可以维护操作状态和基于反向传播自动地更新模型变量。TensorFlow通过计算图来更新变量和最小化损失函数来反向传播误差
自定义Autograd函数对于浅层的网络,我们可以手动的书写前向传播和反向传播过程。但是当网络变得很大时,特别是在做深度学习时,网络结构变得复杂。前向传播和反向
Python命令启动Web服务器实例详解利用Python自带的包可以建立简单的web服务器。在DOS里cd到准备做服务器根目录的路径下,输入命令:python-