梯度提升树

1 Boosting

  Boosting是一类将弱学习器提升为强学习器的算法。这类算法的工作机制类似:先从初始训练集中训练出一个基学习器,再根据基学习器的表现对训练样本分布进行调整,使得先前基学习器做错的训练样本在后续受到更多关注。
然后基于调整后的样本分布来训练下一个基学习器;如此重复进行,直至基学习器的数目达到事先指定的值T,最终将这T个基学习器进行加权结合。

  Boost算法是在算法开始时,为每一个样本赋上一个相等的权重值,也就是说,最开始的时候,大家都是一样重要的。
在每一次训练中得到的模型,会使得数据点的估计有所差异,所以在每一步结束后,我们需要对权重值进行处理,而处理的方式就是通过增加错分点的权重,这样使得某些点如果老是被分错,那么就会被“严重关注”,也就被赋上一个很高的权重。
然后等进行了N次迭代,将会得到N个简单的基分类器(basic learner),最后将它们组合起来,可以对它们进行加权(错误率越大的基分类器权重值越小,错误率越小的基分类器权重值越大)、或者让它们进行投票等得到一个最终的模型。

  梯度提升(gradient boosting)属于Boost算法的一种,也可以说是Boost算法的一种改进,它与传统的Boost有着很大的区别,它的每一次计算都是为了减少上一次的残差(residual),而为了减少这些残差,可以在残差减少的梯度(Gradient)方向上建立一个新模型。所以说,在Gradient Boost中,每个新模型的建立是为了使得先前模型残差往梯度方向减少,
与传统的Boost算法对正确、错误的样本进行加权有着极大的区别。

  梯度提升算法的核心在于,每棵树是从先前所有树的残差中来学习。利用的是当前模型中损失函数的负梯度值作为提升树算法中的残差的近似值,进而拟合一棵回归(分类)树。

2 梯度提升

  根据参考文献【1】的介绍,梯度提升算法的算法流程如下所示:

1.1

  在上述的流程中,F(x)表示学习器,psi表示损失函数,第3行的y_im表示负梯度方向,第4行的R_lm表示原数据改变分布后的数据。

  在MLlib中,提供的损失函数有三种。如下图所示。

1.3

  第一个对数损失用于分类,后两个平方误差和绝对误差用于回归。

3 随机梯度提升

  有文献证明,注入随机性到上述的过程中可以提高函数估计的性能。受到Breiman的影响,将随机性作为一个考虑的因素。在每次迭代中,随机的在训练集中抽取一个子样本集,然后在后续的操作中用这个子样本集代替全体样本。
这就形成了随机梯度提升算法。它的流程如下所示:

1.2

4 实例

  下面的代码是分类的例子。

  1. import org.apache.spark.mllib.tree.GradientBoostedTrees
  2. import org.apache.spark.mllib.tree.configuration.BoostingStrategy
  3. import org.apache.spark.mllib.tree.model.GradientBoostedTreesModel
  4. import org.apache.spark.mllib.util.MLUtils
  5. // 准备数据
  6. val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
  7. // Split the data into training and test sets (30% held out for testing)
  8. val splits = data.randomSplit(Array(0.7, 0.3))
  9. val (trainingData, testData) = (splits(0), splits(1))
  10. // 训练模型
  11. // The defaultParams for Classification use LogLoss by default.
  12. val boostingStrategy = BoostingStrategy.defaultParams("Classification")
  13. boostingStrategy.numIterations = 3 // Note: Use more iterations in practice.
  14. boostingStrategy.treeStrategy.numClasses = 2
  15. boostingStrategy.treeStrategy.maxDepth = 5
  16. // Empty categoricalFeaturesInfo indicates all features are continuous.
  17. boostingStrategy.treeStrategy.categoricalFeaturesInfo = Map[Int, Int]()
  18. val model = GradientBoostedTrees.train(trainingData, boostingStrategy)
  19. // 用测试数据评价模型
  20. val labelAndPreds = testData.map { point =>
  21. val prediction = model.predict(point.features)
  22. (point.label, prediction)
  23. }
  24. val testErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / testData.count()
  25. println("Test Error = " + testErr)
  26. println("Learned classification GBT model:\n" + model.toDebugString)

  下面的代码是回归的例子。

  1. import org.apache.spark.mllib.tree.GradientBoostedTrees
  2. import org.apache.spark.mllib.tree.configuration.BoostingStrategy
  3. import org.apache.spark.mllib.tree.model.GradientBoostedTreesModel
  4. import org.apache.spark.mllib.util.MLUtils
  5. // 准备数据
  6. val data = MLUtils.loadLibSVMFile(sc, "data/mllib/sample_libsvm_data.txt")
  7. // Split the data into training and test sets (30% held out for testing)
  8. val splits = data.randomSplit(Array(0.7, 0.3))
  9. val (trainingData, testData) = (splits(0), splits(1))
  10. // 训练模型
  11. // The defaultParams for Regression use SquaredError by default.
  12. val boostingStrategy = BoostingStrategy.defaultParams("Regression")
  13. boostingStrategy.numIterations = 3 // Note: Use more iterations in practice.
  14. boostingStrategy.treeStrategy.maxDepth = 5
  15. // Empty categoricalFeaturesInfo indicates all features are continuous.
  16. boostingStrategy.treeStrategy.categoricalFeaturesInfo = Map[Int, Int]()
  17. val model = GradientBoostedTrees.train(trainingData, boostingStrategy)
  18. // 用测试数据评价模型
  19. val labelsAndPredictions = testData.map { point =>
  20. val prediction = model.predict(point.features)
  21. (point.label, prediction)
  22. }
  23. val testMSE = labelsAndPredictions.map{ case(v, p) => math.pow((v - p), 2)}.mean()
  24. println("Test Mean Squared Error = " + testMSE)
  25. println("Learned regression GBT model:\n" + model.toDebugString)

5 源码分析

5.1 训练分析

  梯度提升树的训练从run方法开始。

  1. def run(input: RDD[LabeledPoint]): GradientBoostedTreesModel = {
  2. val algo = boostingStrategy.treeStrategy.algo
  3. algo match {
  4. case Regression =>
  5. GradientBoostedTrees.boost(input, input, boostingStrategy, validate = false)
  6. case Classification =>
  7. // Map labels to -1, +1 so binary classification can be treated as regression.
  8. val remappedInput = input.map(x => new LabeledPoint((x.label * 2) - 1, x.features))
  9. GradientBoostedTrees.boost(remappedInput, remappedInput, boostingStrategy, validate = false)
  10. case _ =>
  11. throw new IllegalArgumentException(s"$algo is not supported by the gradient boosting.")
  12. }
  13. }

  在MLlib中,梯度提升树只能用于二分类和回归。所以,在上面的代码中,将标签映射为-1,+1,那么二分类也可以被当做回归。整个训练过程在GradientBoostedTrees.boost中实现。
GradientBoostedTrees.boost的过程分为三步,第一步,初始化参数;第二步,训练第一棵树;第三步,迭代训练后续的树。下面分别介绍这三步。

  • 初始化参数
  1. // 初始化梯度提升参数
  2. // 迭代次数,默认为100
  3. val numIterations = boostingStrategy.numIterations
  4. // 基学习器
  5. val baseLearners = new Array[DecisionTreeModel](numIterations)
  6. // 基学习器权重
  7. val baseLearnerWeights = new Array[Double](numIterations)
  8. // 损失函数,分类时,用对数损失,回归时,用误差平方损失
  9. val loss = boostingStrategy.loss
  10. val learningRate = boostingStrategy.learningRate
  11. // Prepare strategy for individual trees, which use regression with variance impurity.
  12. // 回归时,使用方差计算不纯度
  13. val treeStrategy = boostingStrategy.treeStrategy.copy
  14. val validationTol = boostingStrategy.validationTol
  15. treeStrategy.algo = Regression
  16. treeStrategy.impurity = Variance
  17. // 缓存输入数据
  18. val persistedInput = if (input.getStorageLevel == StorageLevel.NONE) {
  19. input.persist(StorageLevel.MEMORY_AND_DISK)
  20. true
  21. } else {
  22. false
  23. }
  24. // Prepare periodic checkpointers
  25. val predErrorCheckpointer = new PeriodicRDDCheckpointer[(Double, Double)](
  26. treeStrategy.getCheckpointInterval, input.sparkContext)
  27. val validatePredErrorCheckpointer = new PeriodicRDDCheckpointer[(Double, Double)](
  28. treeStrategy.getCheckpointInterval, input.sparkContext)
  • 训练第一棵树(即第一个基学习器)
  1. //通过训练数据训练出一颗决策树,具体信息请参考随机森林的分析
  2. val firstTreeModel = new DecisionTree(treeStrategy).run(input)
  3. val firstTreeWeight = 1.0
  4. baseLearners(0) = firstTreeModel
  5. baseLearnerWeights(0) = firstTreeWeight
  6. var predError: RDD[(Double, Double)] = GradientBoostedTreesModel.
  7. computeInitialPredictionAndError(input, firstTreeWeight, firstTreeModel, loss)
  8. predErrorCheckpointer.update(predError)

  这里比较关键的是通过GradientBoostedTreesModel.computeInitialPredictionAndError计算初始的预测和误差。

  1. def computeInitialPredictionAndError(
  2. data: RDD[LabeledPoint],
  3. initTreeWeight: Double,
  4. initTree: DecisionTreeModel,
  5. loss: Loss): RDD[(Double, Double)] = {
  6. data.map { lp =>
  7. val pred = initTreeWeight * initTree.predict(lp.features)
  8. val error = loss.computeError(pred, lp.label)
  9. (pred, error)
  10. }
  11. }

  根据选择的损失函数的不同,computeError的实现不同。

  1. //对数损失的实现
  2. override private[mllib] def computeError(prediction: Double, label: Double): Double = {
  3. val margin = 2.0 * label * prediction
  4. // The following is equivalent to 2.0 * log(1 + exp(-margin)) but more numerically stable.
  5. 2.0 * MLUtils.log1pExp(-margin)
  6. }
  7. //误差平方损失
  8. override private[mllib] def computeError(prediction: Double, label: Double): Double = {
  9. val err = label - prediction
  10. err * err
  11. }
  • 迭代训练后续树
  1. var validatePredError: RDD[(Double, Double)] = GradientBoostedTreesModel.
  2. computeInitialPredictionAndError(validationInput, firstTreeWeight, firstTreeModel, loss)
  3. if (validate) validatePredErrorCheckpointer.update(validatePredError)
  4. var bestValidateError = if (validate) validatePredError.values.mean() else 0.0
  5. var bestM = 1
  6. var m = 1
  7. var doneLearning = false
  8. while (m < numIterations && !doneLearning) {
  9. // Update data with pseudo-residuals
  10. // 根据梯度调整训练数据
  11. val data = predError.zip(input).map { case ((pred, _), point) =>
  12. //标签为上一棵树预测的数据的负梯度方向
  13. LabeledPoint(-loss.gradient(pred, point.label), point.features)
  14. }
  15. //训练下一棵树
  16. val model = new DecisionTree(treeStrategy).run(data)
  17. // Update partial model
  18. baseLearners(m) = model
  19. // Note: The setting of baseLearnerWeights is incorrect for losses other than SquaredError.
  20. // Technically, the weight should be optimized for the particular loss.
  21. // However, the behavior should be reasonable, though not optimal.
  22. baseLearnerWeights(m) = learningRate
  23. //更新预测和误差
  24. predError = GradientBoostedTreesModel.updatePredictionError(
  25. input, predError, baseLearnerWeights(m), baseLearners(m), loss)
  26. predErrorCheckpointer.update(predError)
  27. //当需要验证阈值,提前终止迭代时
  28. if (validate) {
  29. // Stop training early if
  30. // 1. Reduction in error is less than the validationTol or
  31. // 2. If the error increases, that is if the model is overfit.
  32. // We want the model returned corresponding to the best validation error.
  33. validatePredError = GradientBoostedTreesModel.updatePredictionError(
  34. validationInput, validatePredError, baseLearnerWeights(m), baseLearners(m), loss)
  35. validatePredErrorCheckpointer.update(validatePredError)
  36. val currentValidateError = validatePredError.values.mean()
  37. if (bestValidateError - currentValidateError < validationTol * Math.max(
  38. currentValidateError, 0.01)) {
  39. doneLearning = true
  40. } else if (currentValidateError < bestValidateError) {
  41. bestValidateError = currentValidateError
  42. bestM = m + 1
  43. }
  44. }
  45. m += 1
  46. }

  上面代码最重要的部分是更新预测和误差的实现。通过GradientBoostedTreesModel.updatePredictionError实现。

  1. def updatePredictionError(
  2. data: RDD[LabeledPoint],
  3. predictionAndError: RDD[(Double, Double)],
  4. treeWeight: Double,
  5. tree: DecisionTreeModel,
  6. loss: Loss): RDD[(Double, Double)] = {
  7. val newPredError = data.zip(predictionAndError).mapPartitions { iter =>
  8. iter.map { case (lp, (pred, error)) =>
  9. val newPred = pred + tree.predict(lp.features) * treeWeight
  10. val newError = loss.computeError(newPred, lp.label)
  11. (newPred, newError)
  12. }
  13. }
  14. newPredError
  15. }

5.2 测试

  利用梯度提升树进行预测时,调用的predict方法扩展自TreeEnsembleModel,它是树结构组合模型的表示,其核心代码如下所示:

  1. //不同的策略采用不同的预测方法
  2. def predict(features: Vector): Double = {
  3. (algo, combiningStrategy) match {
  4. case (Regression, Sum) =>
  5. predictBySumming(features)
  6. case (Regression, Average) =>
  7. predictBySumming(features) / sumWeights
  8. //用于梯度提升树,转换为1 或者 0
  9. case (Classification, Sum) => // binary classification
  10. val prediction = predictBySumming(features)
  11. // TODO: predicted labels are +1 or -1 for GBT. Need a better way to store this info.
  12. if (prediction > 0.0) 1.0 else 0.0
  13. case (Classification, Vote) =>
  14. predictByVoting(features)
  15. case _ =>
  16. throw new IllegalArgumentException()
  17. }
  18. }
  19. private def predictBySumming(features: Vector): Double = {
  20. val treePredictions = trees.map(_.predict(features))
  21. //两个向量的内集
  22. blas.ddot(numTrees, treePredictions, 1, treeWeights, 1)
  23. }

参考文献

【1】Stochastic Gradient Boost

【2】机器学习算法-梯度树提升GTB(GBRT)