【发布时间】:2019-11-10 23:33:57
【问题描述】:
我已经为非正则化逻辑回归成本函数和寻找梯度编写了一些代码,但无论我尝试什么,我的代码总是返回相同的 TypeError。
我已经尝试了我的代码的矢量化和 for 循环实现,但没有任何效果。我还想指出,评分者总是给我的成本函数打满分,而不是找到偏导数的代码。我的结果始终符合预期成本,但渐变部分没有返回任何内容。
它说这是成本: J(????)=1????∑????=1????[−????(????)log(ℎ????(????(? ???)))-(1-????(??????))log(1-ℎ????(????(??????)))]
这是偏导数: ∂??????(??????)∂????????=1??????∑????=1??????(ℎ????(????(? ???))-????(??????))??????(??????)????
(通过课程,我可以验证这是正确的)
def costFunction(theta, X, y):
# Initialize some useful values
m = y.size # number of training examples
# You need to return the following variables correctly
J = 0
grad = np.zeros(theta.shape)
# ====================== YOUR CODE HERE ============
for i in range(m):
hypothesis = sigmoid(np.dot(theta.T, X[i, :]))
J += y[i] * math.log(hypothesis) + (1 - y[i]) * math.log(1 - hypothesis)
for j in range(n):
grad = (hypothesis - y[i]) * X[i, j]
J = (-1 / m) * J
grad = (1 / m) * grad
# =============================================================
return J, grad
# Initialize fitting parameters
initial_theta = np.zeros(n+1)
cost, grad = costFunction(initial_theta, X, y)
print('Cost at initial theta (zeros): {:.3f}'.format(cost))
print('Expected cost (approx): 0.693\n')
print('Gradient at initial theta (zeros):')
#print('\t[{:.4f}, {:.4f}, {:.4f}]'.format(*grad))
print('Expected gradients (approx):\n\t[-0.1000, -12.0092, -11.2628]\n')
# Compute and display cost and gradient with non-zero theta
test_theta = np.array([-24, 0.2, 0.2])
cost, grad = costFunction(test_theta, X, y)
print('Cost at test theta: {:.3f}'.format(*cost))
print('Expected cost (approx): 0.218\n')
print('Gradient at test theta:')
print('\t[{:.3f}, {:.3f}, {:.3f}]'.format(*grad))
print('Expected gradients (approx):\n\t[0.043, 2.566, 2.647]')
我希望输出是:
Cost at initial theta (zeros): 0.693
Expected cost (approx): 0.693
Gradient at initial theta (zeros):
[-0.1000, -12.0092, -11.2628]
Expected gradients (approx):
[-0.1000, -12.0092, -11.2628]
但我得到以下信息:
Cost at initial theta (zeros): 0.693
Expected cost (approx): 0.693
Gradient at initial theta (zeros):
Expected gradients (approx):
[-0.1000, -12.0092, -11.2628]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-35-ab2a7b541269> in <module>()
15 cost, grad = costFunction(test_theta, X, y)
16
---> 17 print('Cost at test theta: {:.3f}'.format(*cost))
18 print('Expected cost (approx): 0.218\n')
19
TypeError: format() argument after * must be an iterable, not numpy.float64
【问题讨论】:
-
它不起作用。虽然它确实成功地删除了大部分勘误表,但我的分数没有改变(成本:30/30 | 梯度:0/30),我的梯度函数仍然没有为 grad 变量返回任何内容。我非常感谢您的帮助,但如果没有这个,我无法找到进步的方法。如果有人可以帮助我,我将非常感激。 ????
标签: python machine-learning logistic-regression