|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.intel.hibench.sparkbench.ml |
| 19 | + |
| 20 | +import org.apache.spark.{SparkConf, SparkContext} |
| 21 | +import org.apache.spark.mllib.tree.GradientBoostedTrees |
| 22 | +import org.apache.spark.mllib.tree.configuration.BoostingStrategy |
| 23 | +import org.apache.spark.mllib.tree.model.GradientBoostedTreesModel |
| 24 | +import org.apache.spark.rdd.RDD |
| 25 | +import org.apache.spark.mllib.regression.LabeledPoint |
| 26 | + |
| 27 | +import scopt.OptionParser |
| 28 | + |
| 29 | +object GradientBoostedTree { |
| 30 | + |
| 31 | + case class Params( |
| 32 | + numClasses: Int = 2, |
| 33 | + maxDepth: Int = 30, |
| 34 | + maxBins: Int = 32, |
| 35 | + numIterations: Int = 20, |
| 36 | + learningRate: Double = 0.1, |
| 37 | + dataPath: String = null |
| 38 | + ) |
| 39 | + |
| 40 | + def main(args: Array[String]): Unit = { |
| 41 | + val defaultParams = Params() |
| 42 | + |
| 43 | + val parser = new OptionParser[Params]("GBT"){ |
| 44 | + head("GBT: an example of Gradient Boosted Tree for classification") |
| 45 | + opt[Int]("numClasses") |
| 46 | + .text(s"numClasses, default: ${defaultParams.numClasses}") |
| 47 | + .action((x,c) => c.copy(numClasses = x)) |
| 48 | + opt[Int]("maxDepth") |
| 49 | + .text(s"maxDepth, default: ${defaultParams.maxDepth}") |
| 50 | + .action((x,c) => c.copy(maxDepth = x)) |
| 51 | + opt[Int]("maxBins") |
| 52 | + .text(s"maxBins, default: ${defaultParams.maxBins}") |
| 53 | + .action((x,c) => c.copy(maxBins = x)) |
| 54 | + opt[Int]("numIterations") |
| 55 | + .text(s"numIterations, default: ${defaultParams.numIterations}") |
| 56 | + .action((x,c) => c.copy(numIterations = x)) |
| 57 | + opt[Double]("learningRate") |
| 58 | + .text(s"learningRate, default: ${defaultParams.learningRate}") |
| 59 | + .action((x,c) => c.copy(learningRate = x)) |
| 60 | + arg[String]("<dataPath>") |
| 61 | + .required() |
| 62 | + .text("data path for Gradient Boosted Tree") |
| 63 | + .action((xc) => c.copy(dataPath = x)) |
| 64 | + } |
| 65 | + parser.parse(args, defaultParams) match { |
| 66 | + case some(params) => run(params) |
| 67 | + case _ => sys.exit(1) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + def run(params: Params): Unit = { |
| 72 | + val conf = new SparkConf().setAppName(s"Gradient Boosted Tree with $params") |
| 73 | + val sc = new SparkContext(conf) |
| 74 | + |
| 75 | + val dataPath = params.dataPath |
| 76 | + val numClasses = params.numClasses |
| 77 | + val maxDepth = params.maxDepth |
| 78 | + val maxBins = params.maxBins |
| 79 | + val numIterations = params.numIterations |
| 80 | + val learningRate = params.learningRate |
| 81 | + |
| 82 | + // Load data file. |
| 83 | + val data: RDD[LabeledPoint] = sc.objectFile(dataPath) |
| 84 | + |
| 85 | + // Split the data into training and test sets (30% held out for testing) |
| 86 | + val splits = data.randomSplit(Array(0.7, 0.3)) |
| 87 | + val (trainingData, testData) = (splits(0), splits(1)) |
| 88 | + |
| 89 | + // Train a GradientBoostedTrees model. |
| 90 | + val boostingStrategy = BoostingStrategy.defaultParams("Classification") |
| 91 | + boostingStrategy.numIterations = numIterations |
| 92 | + boostingStrategy.learningRate = learningRate |
| 93 | + boostingStrategy.treeStrategy.numClasses = numClasses |
| 94 | + boostingStrategy.treeStrategy.maxDepth = maxDepth |
| 95 | + boostingStrategy.treeStrategy.maxBins = maxBins |
| 96 | + // Empty categoricalFeaturesInfo indicates all features are continuous. |
| 97 | + boostingStrategy.treeStrategy.categoricalFeaturesInfo = Map[Int, Int]() |
| 98 | + |
| 99 | + val model = GradientBoostedTrees.train(trainingData, boostingStrategy) |
| 100 | + |
| 101 | + // Evaluate model on test instances and compute test error |
| 102 | + val labelAndPreds = testData.map { point => |
| 103 | + val prediction = model.predict(point.features) |
| 104 | + (point.label, prediction) |
| 105 | + } |
| 106 | + val testErr = labelAndPreds.filter(r => r._1 != r._2).count.toDouble / testData.count() |
| 107 | + println("Test Error = " + testErr) |
| 108 | + |
| 109 | + sc.stop() |
| 110 | + } |
| 111 | +} |
0 commit comments