我的代码
def lrCostFunction(theta, X, y, lambda_): m = y.size if y.dtype == bool: y = y.astype(int) tempt = theta tempt[0] = 0 J = 0 grad = np.zeros(theta.shape) hx = X.dot(theta.T) h = sigmoid(hx) J = (1/m) * np.sum(-y.dot(np.log(h)) - (1-y).dot(np.log(1-h))) J = J + (lambda_/(2*m)) * np.sum(np.square(tempt)) grad = ((1/m) * (h - y) .dot(X)) + (lambda_/m) * tempt return J, grad# rand_indices = np.random.choice(m, 100, replace=False)# sel = X[rand_indices, :]\theta_t = np.array([-2, -1, 1, 2], dtype=float)X_t = np.concatenate([np.ones((5, 1)), np.arange(1, 16).reshape(5, 3, order='F')/10.0], axis=1)y_t = np.array([1, 0, 1, 0, 1])lambda_t = 3cost, gradient = lrCostFunction(theta_t, X_t, y_t, lambda_t)print("J= ", cost, "\nGrad= ", gradient)
输出:
J= 3.0857279966152817 Grad= [ 0.35537648 -0.49170896 0.88597928 1.66366752]
而作业要求从相同输入得到以下结果:
print('Cost : {:.6f}'.format(J))print('Expected cost: 2.534819')print('-----------------------')print('Gradients:')print(' [{:.6f}, {:.6f}, {:.6f}, {:.6f}]'.format(*grad))print('Expected gradients:')print(' [0.146561, -0.548558, 0.724722, 1.398003]');
我甚至在网上搜索了答案,每个人都和我有相同的代码,他们声称他们的结果与预期相同。我甚至将他们的代码复制到我的Pycharm IDE中,但我再次得到了相同的结果。如果您想阅读问题,输入是相同的,问题是“向量化正则化逻辑回归”
链接: PYTHON ASSIGNMENT OF ANDREW NG ML COURSE
链接到其中一个声称使用与我相同的代码并得到预期结果的人的解决方案:
链接: ONE OF THE GUYS CLAIMING TO HAVE THE EXPECTED RESULT FROM SAME CODE AS MINE
这也发生在我上次作业的一部分上,真的很令人沮丧,所以我正在寻求帮助。
回答:
你的代码是正确的。问题在于当你更改tempt[0]
的值时,你也更改了theta[0]
。对theta
进行复制可以确保初始向量不会被更改。
def lrCostFunction(theta, X, y, lambda_): m = y.size if y.dtype == bool: y = y.astype(float) J = 0 grad = np.zeros(theta.shape) hx = X.dot(theta.T) h = sigmoid(hx) tempt = np.copy(theta) # theta的副本 tempt[0] = 0 J = (1/m) * np.sum(-y.dot(np.log(h)) - (1-y).dot(np.log(1-h))) J = J + (lambda_/(2*m)) * np.sum(np.square(tempt)) grad = ((1/m) * (h - y) .dot(X)) + (lambda_/m) * tempt print(theta, tempt) return J, gradcost, gradient = lrCostFunction(theta_t, X_t, y_t, lambda_t)print("J= ", cost, "\nGrad= ", gradient)# 输出:# [-2. -1. 1. 2.] [ 0. -1. 1. 2.]# J= 2.534819396109744 # Grad= [ 0.14656137 -0.54855841 0.72472227 1.39800296]