链式法则
MLP反向传播梯度推导
很详细
minma:
f(3,2) = 0
f(−2.805118,3.131312) = 0
f(−3.779310,−3.283186) = 0
f(3.584428,−1.848126) = 0
可视化:
import numpy as np
import matplotlib.pyplot as plt
import torch
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
def himmelblau(x):
return (x[0]**2 + x[1] -11)**2 + (x[0] + x[1] ** 2 - 7) ** 2
x = np.arange(-6, 6, 0.1)
y = np.arange(-6, 6, 0.1)
print('x,y range:', x.shape, y.shape)
X, Y = np.meshgrid(x, y)
print('X,Y range:', X.shape, Y.shape)
Z = himmelblau([X,Y])
fig = plt.figure('himmelblau')
ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z)
ax.view_init(60, -30)
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.show()
优化(找到上述函数的最小值):
x = torch.tensor([0.,0.],requires_grad=True)
optimizer = torch.optim.Adam([x], lr=1e-3) # 建立梯度更新式 x:=x-grad x
for step in range(20000):
pred = himmelblau(x) # 得到预测值
optimizer.zero_grad() # 清零梯度值
pred.backward() # 得到 x 的梯度
optimizer.step() # 更新 x 梯度
if step % 2000 == 0:
print('step {}: x = , f(x) = {}'
.format(step, x.tolist(), pred.item()))