-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Combine multiple tree ensemble models into a single tree ensemble #364
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.ML.Runtime; | ||
using Microsoft.ML.Runtime.FastTree.Internal; | ||
using Microsoft.ML.Runtime.Internal.Calibration; | ||
|
||
[assembly: LoadableClass(typeof(TreeEnsembleCombiner), null, typeof(SignatureModelCombiner), "Fast Tree Model Combiner", "FastTreeCombiner")] | ||
|
||
namespace Microsoft.ML.Runtime.FastTree.Internal | ||
{ | ||
public sealed class TreeEnsembleCombiner : IModelCombiner<IPredictorProducing<float>, IPredictorProducing<float>> | ||
{ | ||
private readonly IHost _host; | ||
private readonly PredictionKind _kind; | ||
|
||
public TreeEnsembleCombiner(IHostEnvironment env, PredictionKind kind) | ||
{ | ||
_host = env.Register("TreeEnsembleCombiner"); | ||
switch (kind) | ||
{ | ||
case PredictionKind.BinaryClassification: | ||
case PredictionKind.Regression: | ||
case PredictionKind.Ranking: | ||
_kind = kind; | ||
break; | ||
default: | ||
throw _host.ExceptUserArg(nameof(kind), "Tree ensembles can be either binary classifiers, regressors or rankers"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Would it be better for this to be an interpolated |
||
} | ||
} | ||
|
||
public IPredictorProducing<float> CombineModels(IEnumerable<IPredictorProducing<float>> models) | ||
{ | ||
_host.CheckValue(models, nameof(models)); | ||
|
||
var ensemble = new Ensemble(); | ||
int modelCount = 0; | ||
int featureCount = -1; | ||
bool binaryClassifier = false; | ||
foreach (var model in models) | ||
{ | ||
modelCount++; | ||
|
||
var predictor = model; | ||
_host.CheckValue(predictor, nameof(models), "One of the models is null"); | ||
|
||
var calibrated = predictor as CalibratedPredictorBase; | ||
double paramA = 1; | ||
if (calibrated != null) | ||
{ | ||
_host.Check(calibrated.Calibrator is PlattCalibrator, | ||
"Combining FastTree models can only be done when the models are calibrated with Platt calibrator"); | ||
predictor = calibrated.SubPredictor; | ||
paramA = -(calibrated.Calibrator as PlattCalibrator).ParamA; | ||
} | ||
var tree = predictor as FastTreePredictionWrapper; | ||
if (tree == null) | ||
throw _host.Except("Model is not a tree ensemble"); | ||
foreach (var t in tree.TrainedEnsemble.Trees) | ||
{ | ||
var bytes = new byte[t.SizeInBytes()]; | ||
int position = -1; | ||
t.ToByteArray(bytes, ref position); | ||
position = -1; | ||
var tNew = new RegressionTree(bytes, ref position); | ||
if (paramA != 1) | ||
{ | ||
for (int i = 0; i < tNew.NumLeaves; i++) | ||
tNew.SetOutput(i, tNew.LeafValues[i] * paramA); | ||
} | ||
ensemble.AddTree(tNew); | ||
} | ||
|
||
if (modelCount == 1) | ||
{ | ||
binaryClassifier = calibrated != null; | ||
featureCount = tree.InputType.ValueCount; | ||
} | ||
else | ||
{ | ||
_host.Check((calibrated != null) == binaryClassifier, "Ensemble contains both calibrated and uncalibrated models"); | ||
_host.Check(featureCount == tree.InputType.ValueCount, "Found models with different number of features"); | ||
} | ||
} | ||
|
||
var scale = 1 / (double)modelCount; | ||
|
||
foreach (var t in ensemble.Trees) | ||
{ | ||
for (int i = 0; i < t.NumLeaves; i++) | ||
t.SetOutput(i, t.LeafValues[i] * scale); | ||
} | ||
|
||
switch (_kind) | ||
{ | ||
case PredictionKind.BinaryClassification: | ||
if (!binaryClassifier) | ||
return new FastTreeBinaryPredictor(_host, ensemble, featureCount, null); | ||
|
||
var cali = new PlattCalibrator(_host, -1, 0); | ||
return new FeatureWeightsCalibratedPredictor(_host, new FastTreeBinaryPredictor(_host, ensemble, featureCount, null), cali); | ||
case PredictionKind.Regression: | ||
return new FastTreeRegressionPredictor(_host, ensemble, featureCount, null); | ||
case PredictionKind.Ranking: | ||
return new FastTreeRankingPredictor(_host, ensemble, featureCount, null); | ||
default: | ||
_host.Assert(false); | ||
throw _host.ExceptNotSupp("PredictionKind can only be binary classification, regression or ranking"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since it is impossible to reach this state anyway due to the check in the constructor, perhaps this ought to just be a throw without the message. #Resolved |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
using System; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.ML.Runtime.Internal.Utilities; | ||
|
||
namespace Microsoft.ML.Runtime.FastTree.Internal | ||
{ | ||
|
@@ -290,7 +291,7 @@ public static string ToString(this byte[] buffer, ref int position) | |
|
||
public static int SizeInBytes(this byte[] a) | ||
{ | ||
return sizeof(int) + a.Length * sizeof(byte); | ||
return sizeof(int) + Utils.Size(a) * sizeof(byte); | ||
} | ||
|
||
public static void ToByteArray(this byte[] a, byte[] buffer, ref int position) | ||
|
@@ -314,7 +315,7 @@ public static byte[] ToByteArray(this byte[] buffer, ref int position) | |
|
||
public static int SizeInBytes(this short[] a) | ||
{ | ||
return sizeof(int) + a.Length * sizeof(short); | ||
return sizeof(int) + Utils.Size(a) * sizeof(short); | ||
} | ||
|
||
public unsafe static void ToByteArray(this short[] a, byte[] buffer, ref int position) | ||
|
@@ -353,7 +354,7 @@ public unsafe static short[] ToShortArray(this byte[] buffer, ref int position) | |
|
||
public static int SizeInBytes(this ushort[] a) | ||
{ | ||
return sizeof(int) + a.Length * sizeof(ushort); | ||
return sizeof(int) + Utils.Size(a) * sizeof(ushort); | ||
} | ||
|
||
public unsafe static void ToByteArray(this ushort[] a, byte[] buffer, ref int position) | ||
|
@@ -392,12 +393,12 @@ public unsafe static ushort[] ToUShortArray(this byte[] buffer, ref int position | |
|
||
public static int SizeInBytes(this int[] array) | ||
{ | ||
return sizeof(int) + array.Length * sizeof(int); | ||
return sizeof(int) + Utils.Size(array) * sizeof(int); | ||
} | ||
|
||
public unsafe static void ToByteArray(this int[] a, byte[] buffer, ref int position) | ||
{ | ||
int length = a.Length; | ||
int length = Utils.Size(a); | ||
length.ToByteArray(buffer, ref position); | ||
|
||
fixed (byte* tmpBuffer = buffer) | ||
|
@@ -415,6 +416,9 @@ public unsafe static int[] ToIntArray(this byte[] buffer, ref int position) | |
|
||
public unsafe static int[] ToIntArray(this byte[] buffer, ref int position, int length) | ||
{ | ||
if (length == 0) | ||
return null; | ||
|
||
int[] a = new int[length]; | ||
|
||
fixed (byte* tmpBuffer = buffer) | ||
|
@@ -433,7 +437,7 @@ public unsafe static int[] ToIntArray(this byte[] buffer, ref int position, int | |
|
||
public static int SizeInBytes(this uint[] array) | ||
{ | ||
return sizeof(int) + array.Length * sizeof(uint); | ||
return sizeof(int) + Utils.Size(array) * sizeof(uint); | ||
} | ||
|
||
public unsafe static void ToByteArray(this uint[] a, byte[] buffer, ref int position) | ||
|
@@ -472,7 +476,7 @@ public unsafe static uint[] ToUIntArray(this byte[] buffer, ref int position) | |
|
||
public static int SizeInBytes(this long[] array) | ||
{ | ||
return sizeof(int) + array.Length * sizeof(long); | ||
return sizeof(int) + Utils.Size(array) * sizeof(long); | ||
} | ||
|
||
public unsafe static void ToByteArray(this long[] a, byte[] buffer, ref int position) | ||
|
@@ -511,7 +515,7 @@ public unsafe static long[] ToLongArray(this byte[] buffer, ref int position) | |
|
||
public static int SizeInBytes(this ulong[] array) | ||
{ | ||
return sizeof(int) + array.Length * sizeof(ulong); | ||
return sizeof(int) + Utils.Size(array) * sizeof(ulong); | ||
} | ||
|
||
public unsafe static void ToByteArray(this ulong[] a, byte[] buffer, ref int position) | ||
|
@@ -550,7 +554,7 @@ public unsafe static ulong[] ToULongArray(this byte[] buffer, ref int position) | |
|
||
public static int SizeInBytes(this MD5Hash[] array) | ||
{ | ||
return sizeof(int) + array.Length * MD5Hash.SizeInBytes(); | ||
return sizeof(int) + Utils.Size(array) * MD5Hash.SizeInBytes(); | ||
} | ||
|
||
public static void ToByteArray(this MD5Hash[] a, byte[] buffer, ref int position) | ||
|
@@ -577,7 +581,7 @@ public unsafe static MD5Hash[] ToUInt128Array(this byte[] buffer, ref int positi | |
|
||
public static int SizeInBytes(this float[] array) | ||
{ | ||
return sizeof(int) + array.Length * sizeof(float); | ||
return sizeof(int) + Utils.Size(array) * sizeof(float); | ||
} | ||
|
||
public unsafe static void ToByteArray(this float[] a, byte[] buffer, ref int position) | ||
|
@@ -616,7 +620,7 @@ public unsafe static float[] ToFloatArray(this byte[] buffer, ref int position) | |
|
||
public static int SizeInBytes(this double[] array) | ||
{ | ||
return sizeof(int) + array.Length * sizeof(double); | ||
return sizeof(int) + Utils.Size(array) * sizeof(double); | ||
} | ||
|
||
public unsafe static void ToByteArray(this double[] a, byte[] buffer, ref int position) | ||
|
@@ -655,6 +659,8 @@ public unsafe static double[] ToDoubleArray(this byte[] buffer, ref int position | |
|
||
public static int SizeInBytes(this double[][] array) | ||
{ | ||
if (Utils.Size(array) == 0) | ||
return sizeof(int); | ||
return sizeof(int) + array.Sum(x => x.SizeInBytes()); | ||
} | ||
|
||
|
@@ -683,10 +689,12 @@ public static double[][] ToDoubleJaggedArray(this byte[] buffer, ref int positio | |
public static long SizeInBytes(this string[] array) | ||
{ | ||
long length = sizeof(int); | ||
for (int i = 0; i < array.Length; ++i) | ||
for (int i = 0; i < Utils.Size(array); ++i) | ||
{ | ||
length += array[i].SizeInBytes(); | ||
} | ||
if (length > 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is this intentional? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I'm not sure what my intention was. If the length of array is 0, then the size returned should be sizeof(int) since the length of the array is always serialized before the array itself. In reply to: 196203608 [](ancestors = 196203608) |
||
length += sizeof(int); | ||
return length; | ||
} | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
Thank you for fixing this. We should add a test for this function. I believe this function is not used when saving the tree model to disk and reading it back in TrainTest or CV, hence it was not caught during testing. #Resolved