Exercise 2 - First two grad values are incorrect
Hi everyone.
I'm having a problem in the exercise 2. I implement my costFunction that works fine with initial_theta set to numpy.zeros(). Then I test with test_theta set to numpy.ones(). The first two values are incorrect but the others 26 are the same as the expected. I got as the first two values: 0.43079084 and 0.07660616 instead of 0.346045 and 0.161352
As accuracy I also get a wrong value: 79.66 instead of 83.1
This is my costFunction
def costFunctionReg(theta, X, y, lambda_):
m, n = X.shape
theta = theta.reshape((n, 1))
h_theta = sigmoid(X @ theta)
J = (1 / m) * (-y.T @ numpy.log(h_theta) - (1 - y).T @ numpy.log(1 - h_theta)) + (lambda_ / (2 * m)) * (theta[1:theta.size]).T @ theta[1:theta.size]
thetaZero = theta
thetaZero[1] = 0
grad = ((1 / m) * (h_theta - y).T @ X) + lambda_ / m * thetaZero.T
grad = grad.reshape((n, 1))
return J, grad
Where I'm wrong? I implement the same (i think) code in octave and I got the correct result.
Thanks.
𝐽(𝜃)'s regularization part is sum of j=1 to n, so 1st theta parameter (theta0) is not penalized . You should use thetaZero for J calculation . def costFunctionReg(theta, X, y, lambda_): m, n = X.shape theta = theta.reshape((n, 1))
h_theta = sigmoid(X @ theta)
thetaZero = theta
thetaZero[1] = 0
J = (1 / m) * (-y.T @ numpy.log(h_theta) - (1 - y).T @ numpy.log(1 - h_theta)) + (lambda_ / (2 * m)) * (theta[1:thetaZero.size]).T @ theta[1:thetaZero.size]
grad = ((1 / m) * (h_theta - y).T @ X) + lambda_ / m * thetaZero.T
grad = grad.reshape((n, 1))
return J, grad