2016-12-29 38 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

顯然,沒有最低這裏的。我哪裏錯了?

感謝

回答