2016-12-29 92 views
0

我试图在玩具问题上从头开始实现渐变下降算法。我的代码总是返回NaN的载体:渐变下降和线性回归 - 代码不收敛

from sklearn.linear_model import LinearRegression 
import numpy as np 
import matplotlib.pyplot as plt 

np.random.seed(45) 
x = np.linspace(0, 1000, num=1000) 
y = 3*x + 2 + np.random.randn(len(x)) 

# sklearn output - This works (returns intercept = 1.6, coef = 3) 
lm = LinearRegression() 
lm.fit(x.reshape(-1, 1), y.reshape(-1, 1)) 
print("Intercept = {:.2f}, Coef = {:.2f}".format(lm.coef_[0][0], lm.intercept_[0])) 

# BGD output 
theta = np.array((0, 0)).reshape(-1, 1) 
X = np.hstack([np.ones_like(x.reshape(-1, 1)), x.reshape(-1, 1)]) # [1, x] 
Y = y.reshape(-1, 1) # Column vector 
alpha = 0.05 
for i in range(100): 
    # Update: theta <- theta - alpha * [X.T][X][theta] - [X.T][Y] 
    h = np.dot(X, theta) # Hypothesis 
    loss = h - Y 
    theta = theta - alpha*np.dot(X.T, loss) 
theta 

sklearn部分运行正常,所以我必须做一些错误的for循环。我尝试了各种不同的alpha值,并没有一个汇聚。

问题是theta在整个循环中越来越大,最终变得太大,python无法存储。

这里的成本函数的等高线图:

J = np.dot((np.dot(X, theta) - y).T, (np.dot(X, theta) - y)) 
plt.contour(J) 

enter image description here

显然,没有最低这里的。我哪里错了?

感谢

回答