【问题标题】:My Python code for logistic regression gradient for Andrew Ng course does not work我的 Andrew Ng 课程的逻辑回归梯度 Python 代码不起作用
【发布时间】: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


【解决方案1】:

查看函数costFunction() 时,返回值J(您分配给cost)是一个标量。所以不能用星号*解包,应该直接传给字符串格式化方法:

print('Cost at test theta: {:.3f}'.format(cost)) # passing 'cost' without the star

【讨论】:

  • 非常感谢。我会试试这个,看看评分员是否正确评分。我还想说,这里写的代码(函数除外)不是我自己的。这是任务的一部分。这意味着可能存在错误或更好的方法来执行此操作。再次感谢您的有用回复,祝您有美好的一天:)。这是我用于作业的链接:github.com/dibgerge/ml-coursera-python-assignments
猜你喜欢
  • 2019-02-16
  • 2020-08-13
  • 2019-09-13
  • 2021-12-22
  • 2020-05-01
  • 2019-09-02
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多