Changeset 12597
- Timestamp:
- 07/06/15 13:02:19 (9 years ago)
- Location:
- branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithmStatic.cs
r12590 r12597 58 58 internal double[] y; 59 59 internal int[] activeIdx; 60 internal double[] rim;60 internal double[] pseudoRes; 61 61 62 62 internal IList<IRegressionModel> models; … … 88 88 pred = Enumerable.Repeat(f0, nRows).ToArray(); 89 89 predTest = Enumerable.Repeat(f0, problemData.TestIndices.Count()).ToArray(); 90 rim= new double[nRows];90 pseudoRes = new double[nRows]; 91 91 92 92 models = new List<IRegressionModel>(); … … 168 168 var y = gbmState.y; 169 169 var activeIdx = gbmState.activeIdx; 170 var rim = gbmState.rim;170 var pseudoRes = gbmState.pseudoRes; 171 171 172 172 // copy output of gradient function to pre-allocated rim array (pseudo-residuals) 173 173 int rimIdx = 0; 174 174 foreach (var g in lossFunction.GetLossGradient(y, yPred, w)) { 175 rim[rimIdx++] = g;175 pseudoRes[rimIdx++] = g; 176 176 } 177 177 178 var tree = treeBuilder.CreateRegressionTreeForGradientBoosting( rim, maxDepth, activeIdx, lossFunction.GetLineSearchFunc(y, yPred, w), r, m);178 var tree = treeBuilder.CreateRegressionTreeForGradientBoosting(pseudoRes, maxDepth, activeIdx, lossFunction.GetLineSearchFunc(y, yPred, w), r, m); 179 179 180 180 int i = 0; -
branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/AbsoluteErrorLoss.cs
r12590 r12597 52 52 53 53 while (targetEnum.MoveNext() & predEnum.MoveNext() & weightEnum.MoveNext()) { 54 // weight * sign(res) 54 55 var res = targetEnum.Current - predEnum.Current; 55 56 if (res > 0) yield return weightEnum.Current; … … 61 62 } 62 63 64 // return median of residuals 63 65 public LineSearchFunc GetLineSearchFunc(IEnumerable<double> target, IEnumerable<double> pred, IEnumerable<double> weight) { 64 66 var targetArr = target.ToArray(); -
branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/LossFunctions/RelativeErrorLoss.cs
r12590 r12597 29 29 namespace HeuristicLab.Algorithms.DataAnalysis { 30 30 // relative error loss is a special case of weighted absolute error loss 31 // absolute loss is weighted by (1/target) 31 32 public class RelativeErrorLoss : ILossFunction { 32 33 public double GetLoss(IEnumerable<double> target, IEnumerable<double> pred, IEnumerable<double> weight) { … … 52 53 53 54 while (targetEnum.MoveNext() & predEnum.MoveNext() & weightEnum.MoveNext()) { 55 // weight * sign(res) * abs(1 / target) 54 56 var res = targetEnum.Current - predEnum.Current; 55 57 if (res > 0) yield return weightEnum.Current * 1.0 / Math.Abs(targetEnum.Current); … … 71 73 72 74 // line search for relative error 73 // TODO: check and improve?75 // weighted median (weight = 1/target) 74 76 LineSearchFunc lineSearch = (idx, startIdx, endIdx) => { 75 77 // weighted median calculation 76 78 int nRows = endIdx - startIdx + 1; // startIdx and endIdx are inclusive 77 if (nRows == 1) return targetArr[idx[startIdx]] - predArr[idx[startIdx]]; 79 if (nRows == 1) return targetArr[idx[startIdx]] - predArr[idx[startIdx]]; // res 78 80 else if (nRows == 2) { 81 // weighted average of two residuals 79 82 var w0 = weightArr[idx[startIdx]] * Math.Abs(1.0 / targetArr[idx[startIdx]]); 80 83 var w1 = weightArr[idx[endIdx]] * Math.Abs(1.0 / targetArr[idx[endIdx]]); 81 if (w0 > w1) { 82 return targetArr[idx[startIdx]] - predArr[idx[startIdx]]; 83 } else if (w0 < w1) { 84 return targetArr[idx[endIdx]] - predArr[idx[endIdx]]; 85 } else { 86 // same weight 87 return ((targetArr[idx[startIdx]] - predArr[idx[startIdx]]) + (targetArr[idx[endIdx]] - predArr[idx[endIdx]])) / 2; 88 } 84 return (w0 * (targetArr[idx[startIdx]] - predArr[idx[startIdx]]) + w1 * (targetArr[idx[endIdx]] - predArr[idx[endIdx]])) / (w0 + w1); 89 85 } else { 90 86 var ts = from offset in Enumerable.Range(0, nRows) 91 87 let i = startIdx + offset 92 select new { res = targetArr[idx[i]] - predArr[idx[i]], weight = weightArr[idx[i]] * Math.Abs(1.0 / targetArr[idx[i]]) }; 88 let row = idx[i] 89 select new { res = targetArr[row] - predArr[row], weight = weightArr[row] * Math.Abs(1.0 / targetArr[row]) }; 93 90 ts = ts.OrderBy(t => t.res); 94 91 var totalWeight = ts.Sum(t => t.weight); -
branches/GBT-trunkintegration/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/RegressionTreeBuilder.cs
r12590 r12597 64 64 private int curTreeNodeIdx; // the index where the next tree node is stored 65 65 66 private readonly IList<RegressionTreeModel.TreeNode> nodeQueue; //TODO66 private readonly IList<RegressionTreeModel.TreeNode> nodeQueue; 67 67 68 68 // prepare and allocate buffer variables in ctor
Note: See TracChangeset
for help on using the changeset viewer.