[英]Gradient descent function in python - error in loss function or weights
我正在使用梯度函数进行练习,但我仍然无法获得预期的结果。 也就是说,我收到 2 条错误消息:
损失函数的错误输出。 检查您如何实现矩阵乘法。
权重矩阵 theta 的值错误。 检查您如何更新权重矩阵。
应用该函数时(见下文),我注意到每次迭代的成本都会降低,但它仍然没有收敛到练习中的预期结果。 我已经在公式上尝试了几次修改,但还没有解决。
# gradientDescent
def gradientDescent(x, y, theta, alpha, num_iters):
Input:
x: matrix of features which is (m,n+1)
y: corresponding labels of the input matrix x, dimensions (m,1)
theta: weight vector of dimension (n+1,1)
alpha: learning rate
num_iters: number of iterations you want to train your model for
Output:
J: the final cost
theta: your final weight vector
Hint: you might want to print the cost to make sure that it is going down.
### START CODE HERE ###
# get 'm', the number of rows in matrix x
m = len(x)
for i in range(0, num_iters):
# get z, the dot product of x and theta
# z = predictins
z = np.dot(x, theta)
h = sigmoid(z)
loss = z - y
# calculate the cost function
J = (-1/m) * np.sum(loss)
print("Iteration %d | Cost: %f" % (i, J))#
gradient = np.dot(x.T, loss)
#update theta
theta = theta - (1/m) * alpha * gradient
### END CODE HERE ###
J = float(J)
return J, theta
问题是我错误地应用了成本函数的公式和计算权重的公式:
𝐽=−1/𝑚×(𝐲𝑇⋅𝑙𝑜𝑔(𝐡)+(1−𝐲)𝑇⋅𝑙𝑜𝑔(1−𝐡))
𝜃=𝜃−𝛼/𝑚×(𝐱𝑇⋅(𝐡−𝐲))
解决方案是:
J = (-1/m) * (np.dot(y.T, np.log(h)) + (np.dot((1-y).T, np.log(1-h)))
theta = theta - (alpha/m) * gradient
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.