簡體   English   中英

Hessian矩陣的梯度下降牛頓法

[英]gradient descent newton method using Hessian Matrix

我正在使用牛頓法實現梯度下降以進行回歸,如《機器學習概率論》(墨菲)書第8.3節中所述。 我正在此實現中處理二維數據。 我正在使用以下符號。
x =輸入數據點m * 2
y =對應於輸入數據的標記輸出(m)
H = Hessian矩陣定義為

梯度下降更新

損失函數定義為

就我而言 數組,H為

這是我的python實現。 但是,這不起作用,因為每次迭代中的成本都在增加。

def loss(x,y,theta):
   m,n = np.shape(x)
   cost_list = []
   for i in xrange(0,n): 
     x_0 = x[:,i].reshape((m,1)) 
     predicted = np.dot(x_0, theta[i])    
     error = predicted - y
     cost = np.sum(error ** 2) /  m
     cost_list.append(cost)

   cost_list = np.array(cost_list).reshape((2,1))
   return cost_list


def NewtonMethod(x,y,theta,maxIterations):
   m,n = np.shape(x)
   xTrans  = x.transpose()
   H       = 2 * np.dot(xTrans,x) / m
   Hinv    = np.linalg.inv(H)
   thetaPrev = np.zeros_like(theta)
   best_iter = maxIterations
   for i in range(0,maxIterations):
     cost = loss(x,y,theta)
     theta  = theta - np.dot(Hinv,cost))
     if(np.allclose(theta,thetaPrev,rtol=0.001,atol=0.001)):
        break;
     else:
       thetaPrev = theta
       best_iter = i

   return theta

這是我使用的樣本值

import numpy as np

x = np.array([[-1.7, -1.5],[-1.0 , -0.3],[ 1.7 ,  1.5],[-1.2, -0.7 ][  0.6,  0.1]])  
y = np.array([ 0.3 ,  0.07, -0.2,  0.07,  0.03 ])
theta = np.zeros(2)
NewtonMethod(x,y,theta,100)

需要幫助/建議來解決此問題。
謝謝

您正在有效地使用1的步長。嘗試減小步長,看看是否有幫助。 也就是說,代替

在此處輸入圖片說明

做這個:

在此處輸入圖片說明

值小於1。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM