gpt4 book ai didi

python - 最速下降轨迹行为

转载 作者:行者123 更新时间:2023-12-01 00:51:43 25 4
gpt4 key购买 nike

我编写了对公式给出的二次形式执行最陡下降的代码:1/2 * (x1^2 + gamma * x2^2)。在数学上,我以博伊德的凸优化书中给出的方程作为指导,并希望重现给定的示例。我遇到的问题是,我的最陡下降路径的轨迹看起来有点奇怪,并且与书中显示的轨迹不匹配,即使它似乎会聚。

示例,我的代码输出的内容:

enter image description here

它应该是什么样子:

enter image description here

另一个:

enter image description here

对比它应该是什么样子:

enter image description here

这是一个可重现的示例,尽管有点长:

import numpy as np
import matplotlib.pyplot as plt

# Function value at a given point
def f(x1,x2):
return np.power(np.e,x1 + 3*x2 - 0.1) + np.power(np.e,x1 - 3*x2 - 0.1) + np.power(np.e,- x1 - 0.1)

# Partial Derivatives
def g(x1, x2):
g1 = np.power(np.e, x1 + 3 * x2 - 0.1) + np.power(np.e, x1 - 3 * x2 - 0.1) - np.power(np.e, - x1 - 0.1)
g2 = np.power(3*np.e, x1 + 3 * x2 - 0.1) - np.power(3 * np.e, x1 - 3 * x2 - 0.1)
return np.asarray([g1[0], g2[0]])

# Descent Parameters
alpha = 0.1
beta = 0.7

# Starting point
x0 = np.array([-1, 1], ndmin = 2, dtype = np.float64 ).T
f0 = f(x0[0], x0[1]) # Initial function value

# Calculate the minimum
xmin = np.array([0,0], ndmin = 2).T # Analytic minimum to the problem
fmin = f(0,0)

nangles = 1000
nopts = 1000
thetas = np.linspace(0, 2 * np.pi, nangles)
cont = np.zeros((5,2,nangles))
Delta = (f0 - fmin)/4

# function that plots the level curves or contour lines
for i in range(nangles):
ray = np.vstack([np.linspace(0,4,nopts) * np.cos(thetas[i]),
np.linspace(0,4,nopts) * np.sin(thetas[i])])

fray = f(ray[0], ray[1])

def get_ind(expression):
ind = np.nonzero(expression)[0].max(0)
return(ray[:,ind - 1] + ray[:,ind]) / 2

cont[0,:,i] = get_ind(fray < f0)
cont[1,:,i] = get_ind(fray < f0 - 3 * Delta)
cont[2,:,i] = get_ind(fray < f0 - 2 * Delta)
cont[3,:,i] = get_ind(fray < f0 - Delta)
cont[4,:,i] = get_ind(fray < f0 + Delta)

def plt_contour():
fig, ax = plt.subplots()
ax.plot(cont[0,0,:], cont[0,1,:], '--',
cont[1,0,:], cont[1,1,:], '--',
cont[2,0,:], cont[2,1,:], '--',
cont[3,0,:], cont[3,1,:], '--',
cont[4,0,:], cont[4,1,:], '--')
ax.axis('equal')
ax.axis('off')
return fig, ax

maxiters = 100
xs = np.zeros((2, maxiters))
fs = np.zeros((1, maxiters))
x = x0.copy()
Pnorm = np.diag([8,2])

for i in range(maxiters):
print(i)
fval = f(x[0],x[1])
xs[:,i:i + 1] = x
fs[:,i] = fval
if fval - fmin < 1e-10: #stopping criterion
break
# Backtracking Line Search
gval = g(x[0],x[1])
v = np.dot(-np.linalg.inv(Pnorm), gval) # I think this is the step that I am not doing right
s = 1
for k in range(10):
xnew = x + (s * v).reshape(2,1)
fxnew = f(xnew[0], xnew[1])
if fxnew < fval + s * alpha * gval.T @ v:
break
else:
s = s * beta
x = x + (s * v).reshape(2,1)

# Plot path
fig, ax = plt_contour()
fig.set_size_inches(5,5)
ax.plot( xs[0,0:i + 1], xs[1, 0:i + 1], 'o')
ax.plot( xs[0,0:i + 1], xs[1, 0:i + 1], '-')
plt.show()

我有一种预感,我做错的地方是最陡的下降步骤,即变量 v。也许我实际上并没有做我认为我正在做的事情。为任何帮助干杯。

最佳答案

g2计算是否正确? 3不应该在幂函数之外吗?

g2 = 3*np.power(np.e, x1 + 3 * x2 - 0.1) - 3*np.power(np.e, x1 - 3 * x2 - 0.1)

关于python - 最速下降轨迹行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56521298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com