我的代码产生了不同的结果,而在我的机器学习作业中,同样的代码却期望得到不同的结果?

我的代码

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]

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注