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
只因 世界 再大 不过 你和我
用最小回忆 堆成宇宙
——————五月天(因为你 所以我)——————