简体   繁体   English

Python 中梯度下降算法的轮廓图

[英]Contour Plot of the Gradient Descent Algorithm in Python

I'm trying to apply gradient descent to a simple linear regression model, when plotting a 2D graph I get the intended result but when I switch into a contour plot I don't the intended plot, I would like to know where my mistake is.我正在尝试将梯度下降应用于简单的线性回归模型,在绘制 2D 图时我得到了预期的结果但是当我切换到等高线图时我没有预期的图,我想知道我的错误在哪里.

Here is the code:这是代码:

def J(b_0, b_1, x, y):
  return (1/len(y))*(y - b_0 - b_1*x)**2

def dJ_1(b_0, b_1, x, y):
  return (2/len(y))*np.sum(x*(b_1*x + b_0 - y))

def dJ_0(b_0, b_1, x, y):
  return (2/100)*np.sum((b_1*x + b_0 - y))

x = np.linspace(-1, 1, 100)
y = np.linspace(-2, 2, 100)

b_0 = 5
b_1 = 5
parameters_0 = [b_0]
parameters_1 = [b_1]
for i in range(99):
  b_1 -= 0.1*dJ_1(b_0, b_1, x, y)
  b_0 -= 0.1*dJ_0(b_0, b_1, x, y)
  parameters_0.append(b_0)
  parameters_1.append(b_1)

plt.figure(figsize=(4, 8))
plt.plot(np.linspace(-2, 7, 100), J(np.linspace(-2, 7, 100), parameters_1[-1], -1, -2))
plt.plot(np.array(parameters_0), J(np.array(parameters_0), parameters_1[-1], -1, -2), color="C1")
plt.plot(np.array(parameters_0), J(np.array(parameters_0), parameters_1[-1], -1, -2), '-o', color="C1")
plt.xlabel(r"$\beta_0$")
plt.ylabel(r"$J(\beta_0)$")
plt.show()

The first plot:第一个情节:

在此处输入图片说明

plt.figure(figsize=(4, 8))
plt.plot(np.linspace(-4, 7, 100), J(parameters_0[-1], np.linspace(-4, 7, 100), -1, -2))
plt.plot(np.array(parameters_1), J(parameters_0[-1], np.array(parameters_1), -1, -2), color="C1")
plt.plot(np.array(parameters_1), J(parameters_0[-1], np.array(parameters_1), -1, -2), '-o', color="C1")
plt.xlabel(r"$\beta_1$")
plt.ylabel(r"$J(\beta_1)$")
plt.show()

The second plot:第二个情节:

在此处输入图片说明

b_0 = np.linspace(-10, 10, 100)
b_1 = np.linspace(-10, 10, 100)
X, Y = np.meshgrid(b_0, b_1)
Z = J(X, Y, x=-1, y=-2)
fig,ax=plt.subplots(1,1)
cp = ax.contourf(X, Y, Z)
fig.colorbar(cp) 
ax.set_xlabel(r"$\beta_0$")
ax.set_ylabel(r"$\beta_1$")
plt.show()

The contour plot is:等高线图为:

在此处输入图片说明

Why do I get the above plot rather than a plot similar to this below one for example when the global minima of the cost function is at (0, 2)?例如,当成本函数的全局最小值为 (0, 2) 时,为什么我会得到上面的图而不是类似于下面的图? Thanks in advance.提前致谢。

在此处输入图片说明

Well I think there's no mistake there, you can see from the 2d plot that your gradient descent plot is a quadratic function, thus the way you see it from the contour is as if you see it from the sky to the valley.好吧,我认为那里没有错,您可以从 2d 图中看到您的梯度下降图是一个二次函数,因此您从等高线看到它的方式就像您从天空到山谷看到它一样。 As to why it doesn't look like a circle, well it's because it's just a 3d quadratic function.至于为什么它看起来不像一个圆,那是因为它只是一个 3d 二次函数。 I also once made something similar, and the gradient descent plot is just as what you plot.我也曾经做过类似的东西,梯度下降图和你绘制的一样。 Check it out here at the end of the page在页面末尾查看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM