当前位置:网站首页>12.优化问题实战
12.优化问题实战
2022-07-27 05:13:00 【派大星的最爱海绵宝宝】
himmelblau函数:
该函数用于检测优化器的效果。可以通过求解这个函数,来看一下我们设计的优化器,或者我们设计的梯度下降法则,能否很好的找到这个解,我们的解是已知的。可以对比两个的差距。
已知最小值:
f(3.0,2.0)=0
f(-2.805118,3.131312)=0
f(-3.779310,-3.283186)=0
f(3.584428,-1.848126)=0
都是全局最小值。
import torch
from matplotlib import pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
def himmelblau(x):
return (x[0]**2+x[1]-11)**2+(x[0]+x[1]**2-7)**2
# 可视化代码,打印出函数图像
#生成x和y轴数据列表
x = np.arange(-6,6,0.1)
y = np.arange(-6,6,0.1)
print('x,y range:',x.shape,y.shape)
#对x和y数据进行网格化
X,Y = np.meshgrid(x,y)
print('X,Y maps:',X.shape,Y.shape)
Z = himmelblau([X,Y])
fig=plt.figure('himmelblau')
ax =fig.add_subplot(projection='3d')
ax.plot_surface(X,Y,Z)
ax.view_init(60,-30)
ax.set_xlabel('x')
ax.set_xlabel('y')
plt.show()
#x可初始化为[1.,0.],[-4,0.],[4,0.]
x=torch.tensor([0.,0.],requires_grad=True)
optimizer = torch.optim.Adam([x],lr=1e-3)
for step in range(20000):
pred = himmelblau(x)
optimizer.zero_grad()
pred.backward()
optimizer.step()
if step % 2000 == 0:
print('step {}: x={},f(x)={}'.format(step,x.tolist(),pred.item()))
变换x的初始值,可以得到其他三个极小值。
optimizer = torch.optim.Adam([x],lr=1e-3)
优化器的目标是x,这一步会自动完成:
x‘=x-0.001▽x
y‘=y-0.001▽y
只要执行一次optimizer.step(),就会更新一次以上两个的过程。
optimizer.zero_grad()将梯度信息清零。
pred.backward()生成x和y的梯度信息▽x和▽y。
不停循环循环,直到找到合适的x和y。
修改x为:x=torch.tensor([-4,0.],requires_grad=True)

边栏推荐
- vscode打造golang开发环境以及golang的debug单元测试
- 【高并发】面试官
- You can't even do a simple function test well. What do you take to talk about salary increase with me?
- 本地ORACLE报ORA-12514: TNS:监听程序当前无法识别请求服务
- GBASE 8C——SQL参考6 sql语法(5)
- CAP原则
- Day 6.重大医疗伤害事件网络舆情能量传播过程分析*———以“魏则西事件”为例
- Which futures company has a low handling fee and a high refund?
- MySQL二级索引中的主键——MySQL存在大量相同数据分页查询优化
- Day 15. Deep learning radiomics can predict axillary lymphnode status in early-stage breast cancer
猜你喜欢

You can't even do a simple function test well. What do you take to talk about salary increase with me?

使用Docker部署Redis进行高可用主从复制

Graph node deployment

基于深度神经网络的社交媒体用户级心理压力检测

Read and understand the advantages of the LAAS scheme of elephant swap

When opening futures accounts, you should discuss the policy in detail with the customer manager
基于PG-Oracle和MySQL的三库通用sql代码开发

定点一键查询GUI编程的设计与开发

如果面试官问你 JVM,额外回答“逃逸分析”技术会让你加分

Choose a qualified futures company to open an account
随机推荐
Read and understand the advantages of the LAAS scheme of elephant swap
定点一键查询GUI编程的设计与开发
jenkins构建镜像自动化部署
Day 9. Graduate survey: A love–hurt relationship
Inno setup package jar + H5 + MySQL + redis into exe
未来刷脸支付是能够占据市场很多的份额
Count the quantity in parallel after MySQL grouping
机器人编程与交叉学科的融合延伸
golang封装mysql涉及到的包以及sqlx和gorm的区别
Day10. Work organization and mental health problems in PhD students
MySQL如何执行查询语句
The business logic of face brushing changed significantly, and merchants vied for war smoke to rise again
MySQL查询操作索引优化实践
ES对比两个索引的数据差
你真的了解 Session 和 Cookie 吗?
GBASE 8C——SQL参考6 sql语法(12)
1024 | 正式称为码农的第四年,初心犹在,继续前进
Mysql5.7版本如何实现主从同步
Getaverse, a distant bridge to Web3
You can't even do a simple function test well. What do you take to talk about salary increase with me?