Как векторизовать логистическую регрессию? - PullRequest
1 голос
/ 27 марта 2019

Я пытаюсь реализовать регуляризованную логистическую регрессию, используя python для класса ML курса, но у меня много проблем с векторизацией.Используя этот репозиторий:

Я пробовал много разных способов, но никогда не получал правильный градиент или стоимость, вот моя текущая реализация:

h = utils.sigmoid( np.dot(X, theta) )
J = (-1/m) * ( y.T.dot( np.log(h) ) + (1 - y.T).dot( np.log( 1 - h ) ) ) + ( lambda_/(2*m) ) * np.sum( np.square(theta[1:]) )
grad = ((1/m) * (h - y).T.dot( X )).T + grad_theta_reg

Вот результаты:

Cost         : 0.693147

Ожидаемый

cost: 2.534819

Градиенты:

[-0.100000, -0.030000, -0.080000, -0.130000]

Ожидаемые градиенты:

[0.146561, -0.548558, 0.724722, 1.398003]

Любая помощь от кого-то, кто знает, что происходитбудет высоко ценится.

1 Ответ

0 голосов
/ 27 марта 2019

Ниже рабочий фрагмент векторизованной версии логистической регрессии. Вы можете увидеть больше здесь https://github.com/hzitoun/coursera_machine_learning_matlab_python

Главная

theta_t = np.array([[-2], [-1], [1], [2]])

data =  np.arange(1, 16).reshape(3, 5).T

X_t = np.c_[np.ones((5,1)), data/10]

y_t =  (np.array([[1], [0], [1], [0], [1]]) >= 0.5) * 1

lambda_t = 3

J, grad = lrCostFunction(theta_t, X_t, y_t, lambda_t), lrGradient(theta_t, X_t, y_t, lambda_t, flattenResult=False)

print('\nCost: f\n', J)
print('Expected cost: 2.534819\n')
print('Gradients:\n')
print(' f \n', grad)
print('Expected gradients:\n')
print(' 0.146561\n -0.548558\n 0.724722\n 1.398003\n')

lrCostFunction

from sigmoid import sigmoid
import numpy as np

def lrCostFunction(theta, X, y, reg_lambda):

     """LRCOSTFUNCTION Compute cost and gradient for logistic regression with 
       regularization
       J = LRCOSTFUNCTION(theta, X, y, lambda) computes the cost of using
       theta as the parameter for regularized logistic regression and the
       gradient of the cost w.r.t. to the parameters. 
     """

     m, n = X.shape #number of training examples
     theta = theta.reshape((n,1))

     prediction = sigmoid(X.dot(theta))

     cost_y_1 = (1 - y) * np.log(1 - prediction)
     cost_y_0 = -1 * y * np.log(prediction)

     J = (1.0/m) * np.sum(cost_y_0 - cost_y_1) + (reg_lambda/(2.0 * m)) * np.sum(np.power(theta[1:], 2))

return J

lrGradient

from sigmoid import sigmoid
import numpy as np

def lrGradient(theta, X,y, reg_lambda, flattenResult=True):
     m,n = X.shape     
     theta = theta.reshape((n,1))
     prediction = sigmoid(np.dot(X, theta))
     errors = np.subtract(prediction, y)
     grad = (1.0/m) * np.dot(X.T, errors)

     grad_with_regul = grad[1:] + (reg_lambda/m) * theta[1:]
     firstRow = grad[0, :].reshape((1,1))
     grad = np.r_[firstRow, grad_with_regul]

     if  flattenResult:    
         return grad.flatten()

return grad

Надеюсь, это помогло!

...