python里反向传播算法详解

2022-07-27,,,

反向传播的目的是计算成本函数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 # 元素为负的导数为 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里反向传播算法详解.doc》

下载本文的Word格式文档,以方便收藏与打印。