【问题标题】:Simple gradient boosting algorithm简单的梯度提升算法
【发布时间】:2011-11-15 12:17:01
【问题描述】:

我正在尝试为 R 中的回归实现一个简单的梯度提升算法。这是我目前想出的,但错误并没有像我预期的那样平稳。有什么建议吗?

data("OrchardSprays")
niter  <- 10
learn  <- 0.05
y      <- OrchardSprays$decrease
yhat   <- rep(0,nrow(OrchardSprays))
weight <- rep(1,nrow(OrchardSprays))
loss   <- function(y,yhat) (y - yhat)^2

for (i in seq(niter))
{
    model  <- lm(decrease~.,weights=weight,data=OrchardSprays)
    yhat   <- yhat + weight * (predict(model) - yhat) / i
    error  <- mean(loss(y,yhat))
    weight <- weight + learn * (loss(y,yhat) - error) / error
    cat(i,"error:",error,"\n")
}

输出:

1 error: 319.5881 
2 error: 318.6175 
3 error: 317.9368 
4 error: 317.6112 
5 error: 317.6369 
6 error: 317.9772 
7 error: 318.5833 
8 error: 319.4047 
9 error: 320.3939 
10 error: 321.5086 

【问题讨论】:

    标签: r machine-learning


    【解决方案1】:

    我承认我已经很久没有写过权重优化器了,所以我可能会跑题。我会首先在每次迭代中记录yhat 向量。查看这些值是否在向零振荡或消失(因为我不确定除以 i 是帮助还是伤害)。
    同样,查看 lm() 每次迭代的 R^2 值。如果它们非常接近 1,您可能只是遇到了当前规定的 lm() 灵敏度限制。

    如果您能提供算法的来源,我们会根据您正在实施的方程式检查代码,这将很有帮助。

    更新:快速浏览一下 wikipedia 会得出以下结论:“有几个开源 R 包可用:gbm、[6] mboost、gbev。”我强烈建议您研究这些软件包,包括它们的源代码,看看它们是否能满足您的需求。

    【讨论】:

    • 该算法是用于回归的 Friedmans 梯度提升算法,其正式定义为递归拟合残差,但我需要通过调整案例权重来实现它。我很确定我所拥有的与用于增强树的适应很接近,这正是我正在寻找的。​​span>
    • @darckeen,只是吹毛求疵。它不能是梯度提升算法——因为重新加权的线性函数仍然是线性的。您的治疗更像是加强治疗。
    【解决方案2】:

    您是否尝试过在每个步骤中随机抽样数据,从而只向当前学习者展示一半示例?我认为如果你每次都使用完整的样本,你会得到一种令人讨厌的过度拟合。另外,我不确定提升线性模型(方差低)是否有很大帮助。

    【讨论】:

    • 我只是想保持简单而不使用二次采样,但你提到的过度拟合问题就是问题所在,如果算法最终得到正确实施,它应该几乎完美地拟合数据,但事实并非如此。
    【解决方案3】:

    不确定这是否有帮助,但如果您降低起始权重并增加迭代次数,误差将更接近于零。但是,它仍然没有稳定(错误在迭代 103 处开始回升)。我还添加了以下语句:weight &lt;- ifelse(weight &lt; 0.0, 0.0, weight) 以补偿lm 函数产生的missing or negative weights not allowed 错误。

    data("OrchardSprays")
    niter  <- 105
    learn  <- 0.05
    y      <- OrchardSprays$decrease
    yhat   <- rep(0.0,nrow(OrchardSprays))
    weight <- rep(0.2,nrow(OrchardSprays))
    loss   <- function(y,yhat) (y - yhat)^2
    
    error  <- mean(loss(y,yhat))
    cat("initial error:",error,"\n")
    
    for (i in seq(niter))
    {   
        model  <- lm(decrease~.,weights=weight,data=OrchardSprays)
        yhat   <- yhat + weight * (predict(model) - yhat) / i
        error  <- mean(loss(y,yhat))
        weight <- weight + learn * (loss(y,yhat) - error) / error
        weight <- ifelse(weight < 0.0, 0.0, weight)
        cat(i,"error:",error,"\n")
    }
    

    输出:

    initial error: 3308.922
    
    1 error: 2232.762 
    2 error: 1707.971 
    3 error: 1360.834 
    4 error: 1110.503 
    5 error: 921.2804 
    6 error: 776.4314 
    7 error: 663.5947 
    8 error: 574.2603 
    9 error: 502.2455 
    10 error: 443.2639 
    11 error: 394.2983 
    12 error: 353.1736 
    13 error: 318.2869 
    14 error: 288.4326 
    15 error: 262.6827 
    16 error: 240.3086 
    17 error: 220.7289 
    18 error: 203.4741 
    19 error: 188.1632 
    20 error: 174.4876 
    21 error: 162.1971 
    22 error: 151.0889 
    23 error: 140.9982 
    24 error: 131.7907 
    25 error: 123.3567 
    26 error: 115.6054 
    27 error: 108.4606 
    28 error: 101.8571 
    29 error: 95.73825 
    30 error: 90.05343 
    31 error: 84.75755 
    32 error: 79.81715 
    33 error: 75.19618 
    34 error: 70.86006 
    35 error: 66.77859 
    36 error: 62.92584 
    37 error: 59.28014 
    38 error: 55.8239 
    39 error: 52.54784 
    40 error: 49.44272 
    41 error: 46.49915 
    42 error: 43.71022 
    43 error: 41.07119 
    44 error: 38.57908 
    45 error: 36.23237 
    46 error: 34.03907 
    47 error: 32.00558 
    48 error: 30.12923 
    49 error: 28.39891 
    50 error: 26.80582 
    51 error: 25.33449 
    52 error: 23.97077 
    53 error: 22.70327 
    54 error: 21.52714 
    55 error: 20.43589 
    56 error: 19.42552 
    57 error: 18.48629 
    58 error: 17.60916 
    59 error: 16.78986 
    60 error: 16.02315 
    61 error: 15.30303 
    62 error: 14.62663 
    63 error: 13.99066 
    64 error: 13.39205 
    65 error: 12.82941 
    66 error: 12.30349 
    67 error: 11.811 
    68 error: 11.34883 
    69 error: 10.91418 
    70 error: 10.50448 
    71 error: 10.11723 
    72 error: 9.751116 
    73 error: 9.405197 
    74 error: 9.076175 
    75 error: 8.761231 
    76 error: 8.458107 
    77 error: 8.165144 
    78 error: 7.884295 
    79 error: 7.615498 
    80 error: 7.356618 
    81 error: 7.106186 
    82 error: 6.86324 
    83 error: 6.627176 
    84 error: 6.39777 
    85 error: 6.17544 
    86 error: 5.961616 
    87 error: 5.756781 
    88 error: 5.561157 
    89 error: 5.375131 
    90 error: 5.19945 
    91 error: 5.034539 
    92 error: 4.880956 
    93 error: 4.739453 
    94 error: 4.610629 
    95 error: 4.495216 
    96 error: 4.393571 
    97 error: 4.306144 
    98 error: 4.233587 
    99 error: 4.176799 
    100 error: 4.136802 
    101 error: 4.114575 
    102 error: 4.111308 
    103 error: 4.1278 
    104 error: 4.164539 
    105 error: 4.221389
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-15
      • 2015-07-12
      • 1970-01-01
      • 1970-01-01
      • 2019-06-27
      • 2019-12-04
      • 1970-01-01
      • 2020-12-21
      相关资源
      最近更新 更多