BP算法=反向传播(求导)+梯度下降(优化参数求w)
# 反向传播 # 定义函数 def J(w): return((w*x) ** 2 + 1)** 2 x = 1 J(0) # 函数求导 def J_prime(w): return x*(2*w*x)*2*((w*x)**2+1) # 梯度下降法求得w,w=0时取得最小值 epoch = 10 lr = 0.1 w=1
for i in range(epoch): w = w - lr*J_prime(w) print(w)
0.19999999999999996 0.11679999999999996 0.06944263454719998 0.04153163201131724 0.0248903244330708 0.014928026556183478 0.008955485273445873 0.00537300386953337 0.003223740276053923 0.0019342307645422627
def forward(x, w): y = w * x z = y ** 2 cost = (z + 1) ** 2 cache = {'y': y, 'z': z} return cost, cache cost, cache = forward(x, w) cache
def backward(x, w, cache): z = cache['z'] y = cache['y'] dz = 2 * (z+1) dy = dz * 2 * y dw = dy * x grads = {'dw': dw} return grads backward(x, w, cache)
Out[]:{'dw': 8}
import numpy as np
data = 1 + 0.1*np.random.randn(10)
x = 1
w = 1
epoch = 10
lr = 0.1
for i in range(epoch):
for x in data:
# 计算导数
cost, caches = forward(x, w)
grads = backward(x, w, caches)
# 梯度下降
w = w - lr * grads['dw']
print(w, cost)