深度学习指——链式法则+MLP(多层感知机)反向传播梯度推到

链式法则

学习记录:学习记录链接
学习记录
在这里插入图片描述

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()))

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值