反向传播算法如何在python项目中实现
创始人
2024-11-20 08:41:49
0次

反向传播的目的是计算成本函数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):
    #正向传播
    neth2 = i1*weight[1-1] + i2*weight[2-1] + bias[0]
    neth3 = i1*weight[3-1] + i2*weight[4-1] + bias[0]
    outh2 = sigmoid(neth2)
    outh3 = sigmoid(neth3)
    neto1 = outh2*weight[5-1] + outh3*weight[6-1] + bias[1]
    neto2 = outh3*weight[7-1] + outh3*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 = outh2
    pdEW5 = pdEOuto1 * pdOuto1Neto1 * pdNeto1W5
    pdNeto1W6 = outh3
    pdEW6 = pdEOuto1 * pdOuto1Neto1 * pdNeto1W6
    pdEOuto2 = - (target2 - outo2)
    pdOuto2Neto2 = sigmoidDerivationx(outo2)
    pdNeto1W7 = outh2
    pdEW7 = pdEOuto2 * pdOuto2Neto2 * pdNeto1W7
    pdNeto1W8 = outh3
    pdEW8 = pdEOuto2 * pdOuto2Neto2 * pdNeto1W8

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

    pdEOuth2 = pdEOuto1 * pdOuto1Neto1 * pdNeto1Outh2 + pdEOuto2 * pdOuto2Neto2 * pdNeto1Outh2
    pdOuth2Neth2 = sigmoidDerivationx(outh2)
    pdNeth2W1 = i1
    pdNeth2W2 = i2
    pdEW1 = pdEOuth2 * pdOuth2Neth2 * pdNeth2W1
    pdEW2 = pdEOuth2 * pdOuth2Neth2 * pdNeth2W2
    pdNeto1Outh3 = weight[6-1]
    pdNeto2Outh3 = weight[8-1]
    pdOuth3Neth3 = sigmoidDerivationx(outh3)
    pdNeth3W3 = i1
    pdNeth3W4 = i2
    pdEOuth3 = pdEOuto1 * pdOuto1Neto1 * pdNeto1Outh3 + pdEOuto2 * pdOuto2Neto2 * pdNeto2Outh3
    pdEW3 = pdEOuth3 * pdOuth3Neth3 * pdNeth3W3
    pdEW4 = pdEOuth3 * pdOuth3Neth3 * pdNeth3W4
    #权重更新
    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])

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...