-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
WrappedLightGbmBooster.cs
293 lines (270 loc) · 12.4 KB
/
WrappedLightGbmBooster.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Microsoft.ML.Runtime;
using Microsoft.ML.Trainers.FastTree;
namespace Microsoft.ML.Trainers.LightGbm
{
/// <summary>
/// Wrapper of Booster object of LightGBM.
/// </summary>
internal sealed class Booster : IDisposable
{
private readonly bool _hasValid;
private readonly bool _hasMetric;
public IntPtr Handle { get; private set; }
public int BestIteration { get; set; }
public Booster(Dictionary<string, object> parameters, Dataset trainset, Dataset validset = null)
{
var param = LightGbmInterfaceUtils.JoinParameters(parameters);
var handle = IntPtr.Zero;
LightGbmInterfaceUtils.Check(WrappedLightGbmInterface.BoosterCreate(trainset.Handle, param, ref handle));
Handle = handle;
if (validset != null)
{
LightGbmInterfaceUtils.Check(WrappedLightGbmInterface.BoosterAddValidData(Handle, validset.Handle));
_hasValid = true;
}
int numEval = 0;
BestIteration = -1;
LightGbmInterfaceUtils.Check(WrappedLightGbmInterface.BoosterGetEvalCounts(Handle, ref numEval));
// At most one metric in ML.NET.
Contracts.Assert(numEval <= 1);
if (numEval == 1)
_hasMetric = true;
}
public bool Update()
{
int isFinished = 0;
LightGbmInterfaceUtils.Check(WrappedLightGbmInterface.BoosterUpdateOneIter(Handle, ref isFinished));
return isFinished == 1;
}
public double EvalTrain()
{
return Eval(0);
}
public double EvalValid()
{
if (_hasValid)
return Eval(1);
else
return double.NaN;
}
private unsafe double Eval(int dataIdx)
{
if (!_hasMetric)
return double.NaN;
int outLen = 0;
double[] res = new double[1];
fixed (double* ptr = res)
LightGbmInterfaceUtils.Check(WrappedLightGbmInterface.BoosterGetEval(Handle, dataIdx, ref outLen, ptr));
return res[0];
}
[BestFriend]
internal unsafe string GetModelString()
{
int bufLen = 2 << 15;
byte[] buffer = new byte[bufLen];
int size = 0;
fixed (byte* ptr = buffer)
LightGbmInterfaceUtils.Check(WrappedLightGbmInterface.BoosterSaveModelToString(Handle, 0, BestIteration, bufLen, ref size, ptr));
// If buffer size is not enough, reallocate buffer and get again.
if (size > bufLen)
{
bufLen = size;
buffer = new byte[bufLen];
fixed (byte* ptr = buffer)
LightGbmInterfaceUtils.Check(WrappedLightGbmInterface.BoosterSaveModelToString(Handle, 0, BestIteration, bufLen, ref size, ptr));
}
byte[] content = new byte[size];
Array.Copy(buffer, content, size);
fixed (byte* ptr = content)
return LightGbmInterfaceUtils.GetString((IntPtr)ptr);
}
private static double[] Str2DoubleArray(string str, char delimiter)
{
var values = new List<double>();
foreach (var token in str.Split(delimiter))
{
var trimmed = token.Trim().ToLowerInvariant();
if (trimmed.Contains("-inf"))
values.Add(double.NegativeInfinity);
else if (trimmed.Contains("inf"))
values.Add(double.PositiveInfinity);
else if (trimmed.Contains("nan"))
values.Add(double.NaN);
else
// The value carried in the trimmed string is not inf, -inf, or nan.
// Therefore, double.Parse should be able to generate a valid number from it.
// If parsing fails, an exception will be thrown.
values.Add(double.Parse(trimmed, CultureInfo.InvariantCulture));
}
return values.ToArray();
}
private static int[] Str2IntArray(string str, char delimiter)
{
return str.Split(delimiter).Select(x => int.Parse(x, CultureInfo.InvariantCulture)).ToArray();
}
private static UInt32[] Str2UIntArray(string str, char delimiter)
{
return str.Split(delimiter).Select(x => UInt32.Parse(x, CultureInfo.InvariantCulture)).ToArray();
}
private static bool GetIsDefaultLeft(UInt32 decisionType)
{
// The second bit.
return (decisionType & 2) > 0;
}
private static bool GetIsCategoricalSplit(UInt32 decisionType)
{
// The first bit.
return (decisionType & 1) > 0;
}
private static bool GetHasMissing(UInt32 decisionType)
{
// The 3rd and 4th bits.
return ((decisionType >> 2) & 3) > 0;
}
private static double[] GetDefalutValue(double[] threshold, UInt32[] decisionType)
{
double[] ret = new double[threshold.Length];
for (int i = 0; i < threshold.Length; ++i)
{
if (GetHasMissing(decisionType[i]) && !GetIsCategoricalSplit(decisionType[i]))
{
if (GetIsDefaultLeft(decisionType[i]))
ret[i] = threshold[i];
else
ret[i] = threshold[i] + 1;
}
}
return ret;
}
private static bool FindInBitset(UInt32[] bits, int start, int end, int pos)
{
int i1 = pos / 32;
if (start + i1 >= end)
return false;
int i2 = pos % 32;
return ((bits[start + i1] >> i2) & 1) > 0;
}
private static int[] GetCatThresholds(UInt32[] catThreshold, int lowerBound, int upperBound)
{
List<int> cats = new List<int>();
for (int j = lowerBound; j < upperBound; ++j)
{
// 32 bits.
for (int k = 0; k < 32; ++k)
{
int cat = (j - lowerBound) * 32 + k;
if (FindInBitset(catThreshold, lowerBound, upperBound, cat) && cat > 0)
cats.Add(cat);
}
}
return cats.ToArray();
}
public InternalTreeEnsemble GetModel(int[] categoricalFeatureBoudaries)
{
InternalTreeEnsemble res = new InternalTreeEnsemble();
string modelString = GetModelString();
string[] lines = modelString.Split('\n');
int i = 0;
for (; i < lines.Length;)
{
if (lines[i].StartsWith("Tree="))
{
Dictionary<string, string> kvPairs = new Dictionary<string, string>();
++i;
while (!lines[i].StartsWith("Tree=") && lines[i].Trim().Length != 0)
{
string[] kv = lines[i].Split('=');
Contracts.Check(kv.Length == 2);
kvPairs[kv[0].Trim()] = kv[1].Trim();
++i;
}
int numberOfLeaves = int.Parse(kvPairs["num_leaves"], CultureInfo.InvariantCulture);
int numCat = int.Parse(kvPairs["num_cat"], CultureInfo.InvariantCulture);
if (numberOfLeaves > 1)
{
var leftChild = Str2IntArray(kvPairs["left_child"], ' ');
var rightChild = Str2IntArray(kvPairs["right_child"], ' ');
var splitFeature = Str2IntArray(kvPairs["split_feature"], ' ');
var threshold = Str2DoubleArray(kvPairs["threshold"], ' ');
var splitGain = Str2DoubleArray(kvPairs["split_gain"], ' ');
var leafOutput = Str2DoubleArray(kvPairs["leaf_value"], ' ');
var decisionType = Str2UIntArray(kvPairs["decision_type"], ' ');
var defaultValue = GetDefalutValue(threshold, decisionType);
var categoricalSplitFeatures = new int[numberOfLeaves - 1][];
var categoricalSplit = new bool[numberOfLeaves - 1];
if (categoricalFeatureBoudaries != null)
{
// Add offsets to split features.
for (int node = 0; node < numberOfLeaves - 1; ++node)
splitFeature[node] = categoricalFeatureBoudaries[splitFeature[node]];
}
if (numCat > 0)
{
var catBoundaries = Str2IntArray(kvPairs["cat_boundaries"], ' ');
var catThreshold = Str2UIntArray(kvPairs["cat_threshold"], ' ');
for (int node = 0; node < numberOfLeaves - 1; ++node)
{
if (GetIsCategoricalSplit(decisionType[node]))
{
int catIdx = (int)threshold[node];
var cats = GetCatThresholds(catThreshold, catBoundaries[catIdx], catBoundaries[catIdx + 1]);
categoricalSplitFeatures[node] = new int[cats.Length];
// Convert Cat thresholds to feature indices.
for (int j = 0; j < cats.Length; ++j)
categoricalSplitFeatures[node][j] = splitFeature[node] + cats[j] - 1;
splitFeature[node] = -1;
categoricalSplit[node] = true;
// Swap left and right child.
int t = leftChild[node];
leftChild[node] = rightChild[node];
rightChild[node] = t;
}
else
{
categoricalSplit[node] = false;
}
}
}
InternalRegressionTree tree = InternalRegressionTree.Create(numberOfLeaves, splitFeature, splitGain,
threshold.Select(x => (float)(x)).ToArray(), defaultValue.Select(x => (float)(x)).ToArray(), leftChild, rightChild, leafOutput,
categoricalSplitFeatures, categoricalSplit);
res.AddTree(tree);
}
else
{
InternalRegressionTree tree = new InternalRegressionTree(2);
var leafOutput = Str2DoubleArray(kvPairs["leaf_value"], ' ');
if (leafOutput[0] != 0)
{
// Convert Constant tree to Two-leaf tree, avoid being filter by TLC.
var categoricalSplitFeatures = new int[1][];
var categoricalSplit = new bool[1];
tree = InternalRegressionTree.Create(2, new int[] { 0 }, new double[] { 0 },
new float[] { 0 }, new float[] { 0 }, new int[] { -1 }, new int[] { -2 }, new double[] { leafOutput[0], leafOutput[0] },
categoricalSplitFeatures, categoricalSplit);
}
res.AddTree(tree);
}
}
else
++i;
}
return res;
}
#region IDisposable Support
public void Dispose()
{
if (Handle != IntPtr.Zero)
LightGbmInterfaceUtils.Check(WrappedLightGbmInterface.BoosterFree(Handle));
Handle = IntPtr.Zero;
}
#endregion
}
}