【问题标题】:Gradient descent for ridge regression岭回归的梯度下降
【发布时间】:2021-04-30 17:35:48
【问题描述】:

我正在尝试编写一个代码,该代码使用梯度下降返回岭回归的参数。岭回归定义为

其中,L 是损失(或成本)函数。 w 是损失函数的参数(同化 b)。 x 是数据点。 y 是每个向量 x 的标签。 lambda 是一个正则化常数。 b 是截距参数(被同化到 w 中)。所以,L(w,b) = 数字

我应该实现的梯度下降算法如下所示:

哪里∇ 是 L 相对于 w 的梯度。 η

是步长。 t 是时间或迭代计数器。

我的代码:

def ridge_regression_GD(x,y,C):
    x=np.insert(x,0,1,axis=1) # adding a feature 1 to x at beggining nxd+1
    w=np.zeros(len(x[0,:])) # d+1
    t=0
    eta=1
    summ = np.zeros(1)
    grad = np.zeros(1)
    losses = np.array([0])
    loss_stry = 0
    while eta > 2**-30:
        for i in range(0,len(y)): # here we calculate the summation for all rows for loss and gradient
            summ=summ+((y[i,]-np.dot(w,x[i,]))*x[i,])
            loss_stry=loss_stry+((y[i,]-np.dot(w,x[i,]))**2)
        losses=np.insert(losses,len(losses),loss_stry+(C*np.dot(w,w)))
        grad=((-2)*summ)+(np.dot((2*C),w))
        eta=eta/2
        w=w-(eta*grad)
        t+=1
        summ = np.zeros(1)
        loss_stry = 0
    b=w[0]
    w=w[1:]
    return w,b,losses

输出应该是截距参数b,向量w和每次迭代的损失,loss。

我的问题是,当我运行代码时,w 和损失的值都在增加,两者的顺序都是 10^13。

如果您能帮助我,将不胜感激。如果您需要更多信息或说明,请提出要求。

注意:此帖子已从交叉验证论坛中删除。如果有更好的论坛可以发布,请告诉我。

【问题讨论】:

    标签: python numpy machine-learning gradient-descent


    【解决方案1】:

    在我检查您的代码后,发现您的岭回归实现是正确的,增加w 的值导致您获得的损失增加的问题是由于参数的极端和不稳定的更新值(即abs(eta*grad)太大),所以我将学习率和权重衰减率调整到适当的范围,并改变你衰减学习率的方式,然后一切都按预期工作:

    import numpy as np
    
    sample_num = 100
    x_dim = 10
    x = np.random.rand(sample_num, x_dim)
    w_tar = np.random.rand(x_dim)
    b_tar = np.random.rand(1)[0]
    y = np.matmul(x, np.transpose([w_tar])) + b_tar
    C = 1e-6
    
    def ridge_regression_GD(x,y,C):
        x = np.insert(x,0,1,axis=1) # adding a feature 1 to x at beggining nxd+1
        x_len = len(x[0,:])
        w = np.zeros(x_len) # d+1
        t = 0
        eta = 3e-3
        summ = np.zeros(x_len)
        grad = np.zeros(x_len)
        losses = np.array([0])
        loss_stry = 0
    
        for i in range(50):
            for i in range(len(y)): # here we calculate the summation for all rows for loss and gradient
                summ = summ + (y[i,] - np.dot(w, x[i,])) * x[i,]
                loss_stry += (y[i,] - np.dot(w, x[i,]))**2
                
            losses = np.insert(losses, len(losses), loss_stry + C * np.dot(w, w))
            grad = -2 * summ + np.dot(2 * C,w)
            w -= eta * grad
    
            eta *= 0.9
            t += 1
            summ = np.zeros(1)
            loss_stry = 0
    
        return w[1:], w[0], losses
    
    w, b, losses = ridge_regression_GD(x, y, C)
    print("losses: ", losses)
    print("b: ", b)
    print("b_tar: ", b_tar)
    print("w: ", w)
    print("w_tar", w_tar)
    
    x_pre = np.random.rand(3, x_dim)
    y_tar = np.matmul(x_pre, np.transpose([w_tar])) + b_tar
    y_pre = np.matmul(x_pre, np.transpose([w])) + b
    print("y_pre: ", y_pre)
    print("y_tar: ", y_tar)
    

    输出:

    losses: [   0 1888 2450 2098 1128  354   59    5    1    1    1    1    1    1
        1    1    1    1    1    1    1    1    1    1    1    1    1    1
        1    1    1    1    1    1    1    1    1    1    1    1    1    1
        1    1    1    1    1    1    1    1    1]
    b:  1.170527138363387
    b_tar:  0.894306608050021
    w:  [0.7625987  0.6027163  0.58350218 0.49854847 0.52451963 0.59963663
     0.65156702 0.61188389 0.74257133 0.67164963]
    w_tar [0.82757802 0.76593551 0.74074476 0.37049698 0.40177269 0.60734677
     0.72304859 0.65733725 0.91989305 0.79020028]
    y_pre:  [[3.44989377]
     [4.77838804]
     [3.53541958]]
    y_tar:  [[3.32865041]
     [4.74528037]
     [3.42093559]]
    

    从输出的损失变化中可以看出,学习率eta = 3e-3 仍然是 2 位,所以损失会在最初的几个训练集上升,但当学习率下降到适当的值时开始下降。

    【讨论】:

    • 非常感谢,我正在想出了什么问题。不认为步长如此重要,但我认为它非常有意义,因为松动起初增加太多(表明 w 过度调整),然后减少并几乎保持不变。跨度>
    • @immb31 很高兴为您提供帮助:)
    猜你喜欢
    • 2021-02-20
    • 2017-09-24
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2017-06-20
    相关资源
    最近更新 更多