python中的梯度下降函数 - 损失功能的错误或权重

发布于 2025-02-09 02:59:46 字数 1268 浏览 0 评论 0原文

我正在使用梯度功能进行练习,但我仍然无法获得预期的结果。也就是说,我收到2条错误消息:

  1. 损失功能的错误输出错误。检查您如何实现矩阵乘法。

  2. 重量矩阵theta的错误值。检查您如何更新权重矩阵。

应用功能时(请参见下文),我注意到每次迭代时的成本都会下降,但仍然不会收敛到练习中所需的结果。我已经在公式上尝试了几种改编,但尚未解决。

# gradientDescent

def渐变渐变(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

Im working with the gradient function for an exercise but i still couldn't get the expected outcome. That is, i receive 2 error messages:

  1. Wrong output for the loss function. Check how you are implementing the matrix multiplications.

  2. Wrong values for weight's matrix theta. Check how you are updating the matrix of weights.

When applying the function (see below) i notice that the cost decreases at each iteration but still it does not converge to the desired outcome in the exercise. I already tried several adaptations on the formula but couldn't solve it yet.

# 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2025-02-16 02:59:46

问题在于,我错误地应用了成本函数的公式和计算权重的公式:

The issue is that i wrongly applied the formula of the cost function and the formula for calculating the weights:

????=−1/????×(????????⋅????????????(????)+(1−????)????⋅????????????(1−????))

????=????−????/????×(????????⋅(????−????))

The solution is:

 J =  (-1/m) * (np.dot(y.T, np.log(h)) +  (np.dot((1-y).T, np.log(1-h)))
 theta = theta - (alpha/m) * gradient
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文