2016-09-29 87 views
3

我試圖在python中實現漸變下降,並且隨着每次迭代,我的損失/成本不斷增加。Python漸變下降 - 成本不斷增加

我見過幾個人張貼關於這一點,在這裏看到一個答案:gradient descent using python and numpy

我相信我的實現是相似的,但不能看到我在做什麼錯得到一個爆炸性的成本值:

Iteration: 1 | Cost: 697361.660000 
Iteration: 2 | Cost: 42325117406694536.000000 
Iteration: 3 | Cost: 2582619233752172973298548736.000000 
Iteration: 4 | Cost: 157587870187822131053636619678439702528.000000 
Iteration: 5 | Cost: 9615794890267613993157742129590663647488278265856.000000 

我對我在網上找到的數據集(LA心臟數據)測試此:http://www.umass.edu/statdata/statdata/stat-corr.html

導入代碼:

dataset = np.genfromtxt('heart.csv', delimiter=",") 

x = dataset[:] 
x = np.insert(x,0,1,axis=1) # Add 1's for bias 
y = dataset[:,6] 
y = np.reshape(y, (y.shape[0],1)) 

梯度下降:

def gradientDescent(weights, X, Y, iterations = 1000, alpha = 0.01): 
    theta = weights 
    m = Y.shape[0] 
    cost_history = [] 

    for i in xrange(iterations): 
     residuals, cost = calculateCost(theta, X, Y) 
     gradient = (float(1)/m) * np.dot(residuals.T, X).T 
     theta = theta - (alpha * gradient) 

     # Store the cost for this iteration 
     cost_history.append(cost) 
     print "Iteration: %d | Cost: %f" % (i+1, cost) 

計算成本:

def calculateCost(weights, X, Y): 
    m = Y.shape[0] 
    residuals = h(weights, X) - Y 
    squared_error = np.dot(residuals.T, residuals) 

    return residuals, float(1)/(2*m) * squared_error 

計算假設:

def h(weights, X): 
    return np.dot(X, weights) 

要實際運行它:

gradientDescent(np.ones((x.shape[1],1)), x, y, 5) 
+0

我最好的辦法是微不足道的簽署問題,因爲它看起來好像走錯了方向。 –

回答

3

假設您對梯度的推導是正確的,那麼您使用的是:=-,您應該使用:-=。而不是更新theta的,你重新分配給- (alpha * gradient)

EDIT(上述問題被固定在代碼後):

我跑什麼就什麼,我相信代碼是正確的數據集,並能得到通過設置alpha=1e-7來表現行爲的成本。如果你運行1e6迭代,你應該看到它收斂。這個數據集的方法對學習速率非常敏感。

+0

我認爲在將事情轉移到堆棧溢出時,這是一個小小的錯字。我通過用'theta = theta - (alpha * gradient)'替換該行來使代碼更加明確,而且我仍然遇到同樣的問題 – Simon

1

一般來說,如果你的成本在增加,那麼你應該檢查的第一件事就是看你的學習速度是否過高。在這種情況下,費率導致成本函數跳過最優值並向上增加到無窮大。嘗試不同的學習率的小數值。當我面對你描述的問題時,我通常會反覆嘗試1/10的學習率,直到我能找到J(w)減少的比率。

另一個問題可能是您的派生實施中的錯誤。調試的一個好方法是進行梯度檢查以比較分析梯度與數字梯度。