基于Hessian矩阵的梯度下降牛顿法

2024-10-01 17:35:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用牛顿方法实现回归的梯度下降,如机器学习概率透视(墨菲)一书8.3节所述。我在这个实现中使用二维数据。我使用以下符号。
x=输入数据点m*2
y=对应于输入数据的标记输出(m)
H=黑森矩阵定义为

梯度下降更新

其中损失函数定义为

对我来说 数组,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

下面是我使用的示例值

^{pr2}$

需要帮助/建议来解决此问题。
谢谢


Tags: 数据infor定义defnpdotlist

热门问题