【人工智能导论:模型与算法】慕课笔记1 误差反向传播 CODE
作者:互联网
目标值 与 误差值,反了。 没发现原因 ~
import math def back_propagation(x0,x1,w0,w1,w2,y,N): # y = 1 / (1 + math.exp( -((x0 * w0) + (x1 * w1) + w2))) print("\n=============================NO",N,"=============================") N = N-1 f1 = x0 * w0 f2 = x1 * w1 f3 = f1 + f2 f4 = f3 + w2 f5 = -f4 f6= math.exp(f5) f7 = 1 + f6 f8 = 1/f7 # MSE # error = 0.5 * math.pow((y - f8),2) error = y - f8 print("RESULT, ERROR\n" ,f8, error) # print("----------------END---Forward-------------------------") bp1 = -(1/math.pow(f7,2)) * error bp2 = 1 * bp1 bp3 = math.exp(f5) * bp2 bp4 = -bp3 bp5 = bp4 bp6 = bp5 bp_w1 = bp6 * x1 # bp_x1 = bp6 * w1 bp_w0 = bp6 * x0 # bp_x0 = bp6 * w0 bp_w2 = bp5 w0 = w0 - bp_w0 w1 = w1 - bp_w1 w2 = w2 - bp_w2 # print(bp1,bp2,bp3) # print(bp4,bp5,bp6) print("----------------END---BP----Update w---------------------") print("x0:" , x0,"x1:" , x1,"y:" , y) # print("bp_w0:" , bp_w0,"bp_w1:" , bp_w1,"bp_w2:" , bp_w2) print("w0:" , w0,"w1:" , w1,"w2:" , w2) if (N > 0): back_propagation(x0,x1,w0,w1,w2,y,N) return
x0 = -1 x1 = -2 w0 = 2 w1 = -3 w2 = -3 y = 1.73 N = 10 back_propagation(x0,x1,w0,w1,w2,y,N)
=============================NO 10 ============================= RESULT, ERROR 0.7310585786300049 0.9989414213699951 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.1964038040505485 w1: -2.607192391898903 w2: -3.1964038040505485 =============================NO 9 ============================= RESULT, ERROR 0.45551225254229843 1.2744877474577017 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.5125033261603766 w1: -1.9749933476792467 w2: -3.5125033261603766 =============================NO 8 ============================= RESULT, ERROR 0.11154856232322807 1.618451437676772 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.6729007336654407 w1: -1.6541985326691184 w2: -3.6729007336654407 =============================NO 7 ============================= RESULT, ERROR 0.045764387022953316 1.6842356129770466 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.7464513161952797 w1: -1.50709736760944 w2: -3.7464513161952797 =============================NO 6 ============================= RESULT, ERROR 0.02992416510794365 1.7000758348920564 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.7958023236502387 w1: -1.4083953526995214 w2: -3.7958023236502387 =============================NO 5 ============================= RESULT, ERROR 0.022426856120331854 1.7075731438796682 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.833238973256924 w1: -1.3335220534861503 w2: -3.833238973256924 =============================NO 4 ============================= RESULT, ERROR 0.0179962126173677 1.7120037873826324 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.863494101589214 w1: -1.2730117968215704 w2: -3.863494101589214 =============================NO 3 ============================= RESULT, ERROR 0.015053680787498781 1.7149463192125012 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.8889217263926086 w1: -1.222156547214781 w2: -3.8889217263926086 =============================NO 2 ============================= RESULT, ERROR 0.012951208624576063 1.717048791375424 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.9108715763814192 w1: -1.1782568472371597 w2: -3.9108715763814192 =============================NO 1 ============================= RESULT, ERROR 0.01137126330526271 1.7186287366947373 ----------------END---BP----Update w--------------------- x0: -1 x1: -2 y: 1.73 w0: 2.9301923279002793 w1: -1.1396153441994392 w2: -3.9301923279002793
标签:慕课,导论,CODE,bp,w2,w1,w0,x0,x1 来源: https://www.cnblogs.com/hbuwyg/p/15547167.html