当前位置:网站首页>12. Optimization problem practice
12. Optimization problem practice
2022-07-27 05:59:00 【Pie star's favorite spongebob】
himmelblau function :
This function is used to detect the effect of the optimizer . You can solve this function , Take a look at the optimizer we designed , Or the gradient descent rule we designed , Can we find this solution well , Our solution is known . The difference between the two can be compared .
The minimum value is known :
f(3.0,2.0)=0
f(-2.805118,3.131312)=0
f(-3.779310,-3.283186)=0
f(3.584428,-1.848126)=0
Are global minimum values .
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
# Visual code , Print out the function image
# Generate x and y Axis data list
x = np.arange(-6,6,0.1)
y = np.arange(-6,6,0.1)
print('x,y range:',x.shape,y.shape)
# Yes x and y Grid the data
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 Can be initialized to [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()))
Transformation x The initial value of the , You can get the other three minima .
optimizer = torch.optim.Adam([x],lr=1e-3)
The goal of the optimizer is x, This step will be completed automatically :
x‘=x-0.001▽x
y‘=y-0.001▽y
Just do it once optimizer.step(), The above two processes will be updated once .
optimizer.zero_grad() Clear the gradient information .
pred.backward() Generate x and y Gradient information ▽x and ▽y.
Non stop cycle , Until you find the right x and y.
modify x by :x=torch.tensor([-4,0.],requires_grad=True)

边栏推荐
- 难道Redis真的变慢了吗?
- Rk3399 GPIO port how to find which GPIO port it is
- Inno setup package jar + H5 + MySQL + redis into exe
- Jenkins build image automatic deployment
- 11.感知机的梯度推导
- MySQL索引分析除了EXPLAIN还有什么方法
- Gbase 8C - SQL reference 6 SQL syntax (4)
- GBASE 8C——SQL参考6 sql语法(3)
- Gbase 8C - SQL reference 6 SQL syntax (6)
- 【mysql学习】8
猜你喜欢

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter

3.分类问题---手写数字识别初体验

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

DDD领域驱动设计笔记

Dimitra and ocean protocol interpret the secrets behind agricultural data

新冠时空分析——Global evidence of expressed sentiment alterations during the COVID-19 pandemic

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

Minio8.x version setting policy bucket policy

一张照片攻破人脸识别系统:能点头摇头张嘴,网友
随机推荐
3.分类问题---手写数字识别初体验
How to not overwrite the target source data when dBSwitch data migrates data increments
使用Docker部署Redis进行高可用主从复制
Digital image processing Chapter 5 - image restoration and reconstruction
Gbase 8C - SQL reference 6 SQL syntax (7)
12.优化问题实战
9.高阶操作
GBASE 8C——SQL参考6 sql语法(5)
vscode打造golang开发环境以及golang的debug单元测试
Brief analysis of application process creation process of activity
13.逻辑回归
Day 6. Analysis of the energy transmission process of network public opinion in major medical injury events * -- Taking the "Wei Zexi incident" as an example
Day 7. Towards Preemptive Detection of Depression and Anxiety in Twitter
Gbase 8C - SQL reference 6 SQL syntax (11)
Move protocol launched a beta version, and you can "0" participate in p2e
Gbase 8C - SQL reference 6 SQL syntax (10)
西瓜书学习第五章---神经网络
Day10. Work organization and mental health problems in PhD students
GBASE 8C——SQL参考4 字符集支持
Cap principle