-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-3162] [MLlib] Add local tree training for decision tree regressors #19433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 22 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
219a120
Add data structures for local tree training & associated tests (in Lo…
smurching 7107143
Add utility methods used for impurity and split calculations during b…
smurching 49bf0ae
Add test suites for utility methods used during best-split computation:
smurching bc54b16
Update RandomForest.scala to use new utility methods for impurity/sp…
smurching 6a68a5c
Add local decision tree training logic
smurching 9a7174e
Add local decision tree unit/integration tests
smurching abc86b2
Fix tests, remove perf test in LocalTreeIntegrationSuite, use Scala t…
smurching 5c29d3d
Update calculateImpurityStats to avoid recomputing parent impurity s…
smurching cc6a30c
Merge branch 'master' into pr-splitup
smurching c9a8e01
Fix test bug where instanceWeights weren't properly passed to update …
smurching 93e17fc
Use per-training-example instance weights in local tree training
smurching fd6cdbb
Respond to review comments:
smurching 0d904aa
WIP, renamed activeNodes -> currentLevelActiveNodes, WIP sharing memo…
smurching e6ca306
WIP
smurching 4f0b973
WIP, about to replace dummyStatsAggregator with an ImpurityAggregator…
smurching 1e5db8a
More WIP, use raw ImpurityCalculator/ImpurityAggregator in LocalDecis…
smurching a55a237
Revert "More WIP, use raw ImpurityCalculator/ImpurityAggregator in Lo…
smurching ebade23
Rename dummyStatsAggregator -> currNodeStatsAgg
smurching 9cc6333
Respond to review comments
smurching 7efb1e0
Fix indentation, remove unnecessary params from LocalDecisionTreeRegr…
smurching 3f72cc0
Remove redundant helper method in LocalTreeUnitSuite
smurching b7e6e40
Merge branch 'master' into pr-splitup
smurching 22de575
Respond to easy comments
smurching 926b5d2
Merge branch 'master' into pr-splitup
smurching dbb6a59
Rename getNonConstantFeatures -> getFeaturesWithSplits (to more accur…
smurching c0985a8
Respond to review comments: compute parent impurity calculator outsid…
smurching 0b27c56
Merge branch 'master' into pr-splitup
smurching 072e5bc
Remove unneeded newline
smurching d86dd18
Remove spaces...
smurching File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
mllib/src/main/scala/org/apache/spark/ml/tree/impl/AggUpdateUtils.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.tree.impl | ||
|
||
import org.apache.spark.ml.tree.Split | ||
|
||
/** | ||
* Helpers for updating DTStatsAggregators during collection of sufficient stats for tree training. | ||
*/ | ||
private[impl] object AggUpdateUtils { | ||
|
||
/** | ||
* Updates the parent node stats of the passed-in impurity aggregator with the labels | ||
* corresponding to the feature values at indices [from, to). | ||
* @param indices Array of row indices for feature values; indices(i) = row index of the ith | ||
* feature value | ||
*/ | ||
private[impl] def updateParentImpurity( | ||
statsAggregator: DTStatsAggregator, | ||
indices: Array[Int], | ||
from: Int, | ||
to: Int, | ||
instanceWeights: Array[Double], | ||
labels: Array[Double]): Unit = { | ||
from.until(to).foreach { idx => | ||
val rowIndex = indices(idx) | ||
val label = labels(rowIndex) | ||
statsAggregator.updateParent(label, instanceWeights(rowIndex)) | ||
} | ||
} | ||
|
||
/** | ||
* Update aggregator for an (unordered feature, label) pair | ||
* @param featureSplits Array of splits for the current feature | ||
*/ | ||
private[impl] def updateUnorderedFeature( | ||
agg: DTStatsAggregator, | ||
featureValue: Int, | ||
label: Double, | ||
featureIndex: Int, | ||
featureIndexIdx: Int, | ||
featureSplits: Array[Split], | ||
instanceWeight: Double): Unit = { | ||
val leftNodeFeatureOffset = agg.getFeatureOffset(featureIndexIdx) | ||
// Each unordered split has a corresponding bin for impurity stats of data points that fall | ||
// onto the left side of the split. For each unordered split, update left-side bin if applicable | ||
// for the current data point. | ||
val numSplits = agg.metadata.numSplits(featureIndex) | ||
var splitIndex = 0 | ||
while (splitIndex < numSplits) { | ||
if (featureSplits(splitIndex).shouldGoLeft(featureValue, featureSplits)) { | ||
agg.featureUpdate(leftNodeFeatureOffset, splitIndex, label, instanceWeight) | ||
} | ||
splitIndex += 1 | ||
} | ||
} | ||
|
||
/** Update aggregator for an (ordered feature, label) pair */ | ||
private[impl] def updateOrderedFeature( | ||
agg: DTStatsAggregator, | ||
featureValue: Int, | ||
label: Double, | ||
featureIndex: Int, | ||
featureIndexIdx: Int, | ||
instanceWeight: Double): Unit = { | ||
// The bin index of an ordered feature is just the feature value itself | ||
val binIndex = featureValue | ||
agg.update(featureIndexIdx, binIndex, label, instanceWeight) | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
mllib/src/main/scala/org/apache/spark/ml/tree/impl/FeatureColumn.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.tree.impl | ||
|
||
import org.apache.spark.util.collection.BitSet | ||
|
||
/** | ||
* Stores values for a single training data column (a single continuous or categorical feature). | ||
* | ||
* Values are currently stored in a dense representation only. | ||
* TODO: Support sparse storage (to optimize deeper levels of the tree), and maybe compressed | ||
* storage (to optimize upper levels of the tree). | ||
* | ||
* TODO: Sort feature values to support more complicated splitting logic (e.g. considering every | ||
* possible continuous split instead of discretizing continuous features). | ||
* | ||
* TODO: Consider sorting feature values; the only changed required would be to | ||
* sort values at construction-time. Sorting might improve locality during stats | ||
* aggregation (we'd frequently update the same O(statsSize) array for a (feature, bin), | ||
* instead of frequently updating for the same feature). | ||
* | ||
*/ | ||
private[impl] class FeatureColumn( | ||
val featureIndex: Int, | ||
val values: Array[Int]) | ||
extends Serializable { | ||
|
||
/** For debugging */ | ||
override def toString: String = { | ||
" FeatureVector(" + | ||
s" featureIndex: $featureIndex,\n" + | ||
s" values: ${values.mkString(", ")},\n" + | ||
" )" | ||
} | ||
|
||
def deepCopy(): FeatureColumn = new FeatureColumn(featureIndex, values.clone()) | ||
|
||
override def equals(other: Any): Boolean = { | ||
other match { | ||
case o: FeatureColumn => | ||
featureIndex == o.featureIndex && values.sameElements(o.values) | ||
case _ => false | ||
} | ||
} | ||
|
||
override def hashCode: Int = { | ||
com.google.common.base.Objects.hashCode( | ||
featureIndex: java.lang.Integer, | ||
values) | ||
} | ||
|
||
/** | ||
* Reorders the subset of feature values at indices [from, to) in the passed-in column | ||
* according to the split information encoded in instanceBitVector (feature values for rows | ||
* that split left appear before feature values for rows that split right). | ||
* | ||
* @param numLeftRows Number of rows on the left side of the split | ||
* @param tempVals Destination buffer for reordered feature values | ||
* @param instanceBitVector instanceBitVector(i) = true if the row for the (from + i)th feature | ||
* value splits right, false otherwise | ||
*/ | ||
private[ml] def updateForSplit( | ||
from: Int, | ||
to: Int, | ||
numLeftRows: Int, | ||
tempVals: Array[Int], | ||
instanceBitVector: BitSet): Unit = { | ||
LocalDecisionTreeUtils.updateArrayForSplit(values, from, to, numLeftRows, tempVals, | ||
instanceBitVector) | ||
} | ||
} | ||
|
||
private[impl] object FeatureColumn { | ||
/** | ||
* Store column values sorted by decision tree node (i.e. all column values for a node occur | ||
* in a contiguous subarray). | ||
*/ | ||
private[impl] def apply(featureIndex: Int, values: Array[Int]) = { | ||
new FeatureColumn(featureIndex, values) | ||
} | ||
|
||
} |
137 changes: 137 additions & 0 deletions
137
mllib/src/main/scala/org/apache/spark/ml/tree/impl/ImpurityUtils.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.tree.impl | ||
|
||
import org.apache.spark.mllib.tree.impurity._ | ||
import org.apache.spark.mllib.tree.model.ImpurityStats | ||
|
||
/** Helper methods for impurity-related calculations during node split decisions. */ | ||
private[impl] object ImpurityUtils { | ||
|
||
/** | ||
* Get impurity calculator containing statistics for all labels for rows corresponding to | ||
* feature values in [from, to). | ||
* @param indices indices(i) = row index corresponding to ith feature value | ||
*/ | ||
private[impl] def getParentImpurityCalculator( | ||
metadata: DecisionTreeMetadata, | ||
indices: Array[Int], | ||
from: Int, | ||
to: Int, | ||
instanceWeights: Array[Double], | ||
labels: Array[Double]): ImpurityCalculator = { | ||
// Compute sufficient stats (e.g. label counts) for all data at the current node, | ||
// store result in currNodeStatsAgg.parentStats so that we can share it across | ||
// all features for the current node | ||
val currNodeStatsAgg = new DTStatsAggregator(metadata, featureSubset = None) | ||
AggUpdateUtils.updateParentImpurity(currNodeStatsAgg, indices, from, to, | ||
instanceWeights, labels) | ||
currNodeStatsAgg.getParentImpurityCalculator() | ||
} | ||
|
||
/** | ||
* Calculate the impurity statistics for a given (feature, split) based upon left/right | ||
* aggregates. | ||
* | ||
* @param parentCalc Optional: an ImpurityCalculator containing the impurity stats | ||
* of the node currently being split. | ||
* @param leftImpurityCalculator left node aggregates for this (feature, split) | ||
* @param rightImpurityCalculator right node aggregate for this (feature, split) | ||
* @param metadata learning and dataset metadata for DecisionTree | ||
* @return Impurity statistics for this (feature, split) | ||
*/ | ||
private[impl] def calculateImpurityStats( | ||
parentCalc: Option[ImpurityCalculator], | ||
leftImpurityCalculator: ImpurityCalculator, | ||
rightImpurityCalculator: ImpurityCalculator, | ||
metadata: DecisionTreeMetadata): ImpurityStats = { | ||
|
||
val parentImpurityCalculator | ||
= parentCalc.getOrElse(leftImpurityCalculator.copy.add(rightImpurityCalculator)) | ||
val impurity: Double = parentImpurityCalculator.calculate() | ||
|
||
val leftCount = leftImpurityCalculator.count | ||
val rightCount = rightImpurityCalculator.count | ||
|
||
val totalCount = leftCount + rightCount | ||
|
||
// If left child or right child doesn't satisfy minimum instances per node, | ||
// then this split is invalid, return invalid information gain stats. | ||
if ((leftCount < metadata.minInstancesPerNode) || | ||
(rightCount < metadata.minInstancesPerNode)) { | ||
return ImpurityStats.getInvalidImpurityStats(parentImpurityCalculator) | ||
} | ||
|
||
val leftImpurity = leftImpurityCalculator.calculate() // Note: This equals 0 if count = 0 | ||
val rightImpurity = rightImpurityCalculator.calculate() | ||
|
||
val leftWeight = leftCount / totalCount.toDouble | ||
val rightWeight = rightCount / totalCount.toDouble | ||
|
||
val gain = impurity - leftWeight * leftImpurity - rightWeight * rightImpurity | ||
// If information gain doesn't satisfy minimum information gain, | ||
// then this split is invalid, return invalid information gain stats. | ||
if (gain < metadata.minInfoGain) { | ||
return ImpurityStats.getInvalidImpurityStats(parentImpurityCalculator) | ||
} | ||
|
||
// If information gain is non-positive but doesn't violate the minimum info gain constraint, | ||
// return a stats object with correct values but valid = false to indicate that we should not | ||
// split. | ||
if (gain <= 0) { | ||
return new ImpurityStats(gain, impurity, parentImpurityCalculator, leftImpurityCalculator, | ||
rightImpurityCalculator, valid = false) | ||
} | ||
|
||
|
||
new ImpurityStats(gain, impurity, parentImpurityCalculator, | ||
leftImpurityCalculator, rightImpurityCalculator) | ||
} | ||
|
||
/** | ||
* Given an impurity aggregator containing label statistics for a given (node, feature, bin), | ||
* returns the corresponding "centroid", used to order bins while computing best splits. | ||
* | ||
* @param metadata learning and dataset metadata for DecisionTree | ||
*/ | ||
private[impl] def getCentroid( | ||
metadata: DecisionTreeMetadata, | ||
binStats: ImpurityCalculator): Double = { | ||
|
||
if (binStats.count != 0) { | ||
if (metadata.isMulticlass) { | ||
// multiclass classification | ||
// For categorical features in multiclass classification, | ||
// the bins are ordered by the impurity of their corresponding labels. | ||
binStats.calculate() | ||
} else if (metadata.isClassification) { | ||
// binary classification | ||
// For categorical features in binary classification, | ||
// the bins are ordered by the count of class 1. | ||
binStats.stats(1) | ||
} else { | ||
// regression | ||
// For categorical features in regression and binary classification, | ||
// the bins are ordered by the prediction. | ||
binStats.predict | ||
} | ||
} else { | ||
Double.MaxValue | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
featureIndex is not used