当前位置:网站首页>Pytorch(三) —— 函数优化
Pytorch(三) —— 函数优化
2022-07-01 04:35:00 【CyrusMay】
Pytorch(三) —— 函数优化
1.优化方法
import torch
import torch.nn.functional as F
from torch import nn
model # 搭建模型
data # 准备数据
target # 数据标签
epochs # 迭代次数
loss = nn.CrossEntropyLoss() # 损失函数
opt = torch.optim.Adam(model.parameters(),lr=0.001) # 创建优化器
for i in range(epochs):
outputs = model(x)
loss_value = loss(outputs,target)
opt.zero_grad() # 梯度清零
loss_value.backward() # 反向传播计算梯度
opt.step() # 梯度更新
2. 优化实列
2.1 优化函数
import torch
import torch.nn.functional as F
from torch import nn
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
def f(x):
return (x[0]**2+x[1]-11)**2 + (x[0]+x[1]**2-7)**2
x = np.linspace(-6,6,100)
y = np.linspace(-6,6,100)
x,y = np.meshgrid(x,y)
z = f([x,y])
fig = plt.figure()
ax = fig.gca(projection="3d")
ax.plot_surface(x,y,z)
plt.show()
2.2 优化计算过程
import torch
from torch import nn
import torch.nn.functional as F
def f(x):
return (x[0]**2+x[1]-11)**2 + (x[0]+x[1]**2-7)**2
x = torch.tensor([0,0],dtype=torch.float64,requires_grad=True) # 初始值
opt = torch.optim.Adam([x],lr=0.001)
epochs = 10000
for i in range(epochs):
y = f(x)
opt.zero_grad()
y.backward()
opt.step()
if (i+1) % 1000 == 0:
print("step:{},x:{},y:{}".format(i+1,x.tolist(),y.item()))
step:1000,x:[1.2701438632350366, 1.1183999805060867],y:88.53216019860564
step:2000,x:[2.3323811541207116, 1.9535729950538283],y:13.766112350520348
step:3000,x:[2.85199573044325, 2.114162286626475],y:0.6711343725212155
step:4000,x:[2.9819641054212, 2.0271570475493847],y:0.01492722663566651
step:5000,x:[2.9991264478402497, 2.0014778659773924],y:3.98608554435595e-05
step:6000,x:[2.999994001590949, 2.0000102031302087],y:1.9012429241084185e-09
step:7000,x:[2.9999999988119166, 2.0000000020209616],y:7.529268955707826e-17
step:8000,x:[2.9999999999999893, 2.0000000000000124],y:4.1809627976713626e-27
step:9000,x:[2.9999999999999933, 2.0000000000000093],y:1.8238464128709793e-27
step:10000,x:[2.999999999999996, 2.0000000000000053],y:6.310887241768094e-28
by CyrusMay 2022 06 29
只因 世界 再大 不过 你和我
用最小回忆 堆成宇宙
——————五月天(因为你 所以我)——————
边栏推荐
- Shell之一键自动部署Redis任意版本
- Concurrent mode of different performance testing tools
- Embedded System Development Notes 81: Using Dialog component to design prompt dialog box
- 2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
- 细数软件研发效能的七宗罪
- MySQL function variable stored procedure
- 如何看待智慧城市建设中的改变和机遇?
- Codeforces Round #771 (Div. 2) ABCD|E
- 2022 question bank and answers for safety production management personnel of hazardous chemical production units
- Recommend the best product development process in the Internet industry!
猜你喜欢
[recommended algorithm] C interview question of a small factory
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
One job hopping up 8K, three times in five years
Common thread methods and daemon threads
2022 polymerization process test questions and simulation test
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
Custom components in applets
VR线上展览所具备应用及特色
Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
随机推荐
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
About the transmission pipeline of stage in spark
Rule method: number of effective triangles
2022年化工自动化控制仪表操作证考试题库及答案
Maixll-Dock 快速上手
Common interview questions ①
OdeInt與GPU
Applications and features of VR online exhibition
Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I
What is uid? What is auth? What is a verifier?
[human version] Web3 privacy game in the dark forest
Concurrent mode of different performance testing tools
2022 t elevator repair new version test questions and t elevator repair simulation test question bank
[leetcode skimming] February summary (updating)
VIM简易使用教程
2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers
[learn C and fly] S1E20: two dimensional array
MySQL advanced -- you will have a new understanding of MySQL