Makine Öğrenimi A Probabilistic Perspective (Murphy) kitabının 8.3 bölümünde açıklandığı gibi newtons yöntemini kullanarak regresyon için degrade alçalma uygularım. Bu uygulamada iki boyutlu verilerle çalışıyorum. Aşağıdaki notasyonları kullanıyorum.
x = giriş veri noktaları m * 2
y = etiketli çıkışlar (m) H = Hessian matris
Hessian Matrix kullanarak gradyan iniş newton yöntemi
gradyan iniş güncelleme
kaybı olarak tanımlanır
giriş verilerine karşılık gelen işlevi benim durumumda , dizisidir ve H,
İşte python uygulamamdır. Ancak, bu her iterasyonda maliyet arttıkça çalışmaz.
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
İşte bu sorunu çözmek için/öneri yardıma mı ihtiyacınız
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)
kullanılan örnek değerlerdir.
Teşekkürler