public unsafe string GetModelString() { long bufLen = 2L << 16; byte[] buffer = new byte[bufLen]; long size = 0; fixed(byte *ptr = buffer) PInvokeException.Check(PInvoke.BoosterSaveModelToString(Handle, -1, BestIteration, bufLen, ref size, ptr), nameof(PInvoke.BoosterSaveModelToString)); // If buffer size is not enough, reallocate buffer and get again. if (size > bufLen) { bufLen = size; buffer = new byte[bufLen]; fixed(byte *ptr = buffer) PInvokeException.Check(PInvoke.BoosterSaveModelToString(Handle, -1, BestIteration, bufLen, ref size, ptr), nameof(PInvoke.BoosterSaveModelToString)); } byte[] content = new byte[size]; Array.Copy(buffer, content, size); fixed(byte *ptr = content) return(Marshal.PtrToStringAnsi((IntPtr)ptr)); }
public void ResetParameter(Parameters pms) { var param = pms.ToString(); PInvokeException.Check(PInvoke.BoosterResetParameter(Handle, param), nameof(PInvoke.BoosterResetParameter)); }
// Calculate the number of predictions for a dataset with a given number of rows and iterations. public long CalcNumPredict(int numRow, PredictType predType, int numIteration) { long outLen = 0L; PInvokeException.Check(PInvoke.BoosterCalcNumPredict(Handle, numRow, (PInvoke.CApiPredictType)predType, 0, numIteration, ref outLen), nameof(PInvoke.BoosterCalcNumPredict)); return outLen; }
public Booster(Parameters parameters, Dataset trainset, Dataset validset = null) { if (trainset.CommonParameters != parameters.Common) throw new Exception("CommonParameters differ from those used to create training set"); if (trainset.DatasetParameters != parameters.Dataset) throw new Exception("DatasetParameters differ from those used to create training set"); if (validset != null) { if (validset.CommonParameters != parameters.Common) throw new Exception("CommonParameters differ from those used to create validation set"); if (validset.DatasetParameters != parameters.Dataset) throw new Exception("DatasetParameters differ from those used to create validation set"); } var param = parameters.ToString(); var handle = IntPtr.Zero; PInvokeException.Check(PInvoke.BoosterCreate(trainset.Handle, param, ref handle),nameof(PInvoke.BoosterCreate)); Handle = handle; if (validset != null) { PInvokeException.Check(PInvoke.BoosterAddValidData(handle, validset.Handle),nameof(PInvoke.BoosterAddValidData)); _hasValid = true; } BestIteration = -1; int numEval = this.EvalCounts; // At most one metric in ML.NET: to do remove this. if (numEval > 1) throw new Exception($"Expected at most one metric, got {numEval}"); else if (numEval == 1) _hasMetric = true; }
public long GetNumPredict(int dataIdx) { long outLen = 0; PInvokeException.Check(PInvoke.BoosterGetNumPredict(Handle, dataIdx, ref outLen), nameof(PInvoke.BoosterGetNumPredict)); return outLen; }
private unsafe string GetModelJSON(int startIteration, int numIteration) { long bufLen = 2L << 15; byte[] buffer = new byte[bufLen]; long size = 0L; fixed(byte *ptr = buffer) PInvokeException.Check(PInvoke.BoosterDumpModel(Handle, startIteration, numIteration, bufLen, ref size, ptr), nameof(PInvoke.BoosterDumpModel)); // If buffer size is not enough, reallocate buffer and get again. if (size > bufLen) { bufLen = size; buffer = new byte[bufLen]; fixed(byte *ptr = buffer) PInvokeException.Check(PInvoke.BoosterDumpModel(Handle, startIteration, numIteration, bufLen, ref size, ptr), nameof(PInvoke.BoosterDumpModel)); } byte[] content = new byte[size]; Array.Copy(buffer, content, size); fixed(byte *ptr = content) return(Marshal.PtrToStringAnsi((IntPtr)ptr)); }
public unsafe double[] PredictForMats(PredictType predictType, float[][] data, int numIteration = -1) { if (predictType == PredictType.LeafIndex) { throw new NotImplementedException("TODO: PredictType.LeafIndex"); } if (NumClasses != 1) { throw new Exception("Call PredictForMatsMulti when NumClasses > 1"); } var outResult = new double[data.Length]; if (data.Length > 0) { fixed(double *ptr = outResult) PInvokeException.Check(PInvoke.BoosterPredictForMats(Handle , data , /*nCol*/ data[0].Length , (PInvoke.CApiPredictType)predictType , (numIteration == -1) ? BestIteration : numIteration , "" , outResult.Length , ptr ), nameof(PInvoke.BoosterPredictForMats)); } return(outResult); }
public unsafe double [] PredictForMat(PredictType predictType, float [] data, int numIteration = -1) { if (predictType == PredictType.LeafIndex) { throw new NotImplementedException("TODO: PredictType.LeafIndex"); } long outLen = NumClasses; // TODO double[] outResult = new double[outLen]; fixed(double *ptr = outResult) PInvokeException.Check(PInvoke.BoosterPredictForMat(Handle , data , /*nRow*/ 1 , /*nCol*/ data.Length , /*isRowMajor*/ true , (PInvoke.CApiPredictType)predictType , (numIteration == -1) ? BestIteration : numIteration , "" , ref outLen , ptr ), nameof(PInvoke.BoosterPredictForMat)); return(outResult); }
public unsafe double[,] PredictForMatsMulti(PredictType predictType, float[][] data, int numIteration = -1) { if (predictType == PredictType.LeafIndex) { throw new NotImplementedException("TODO: PredictType.LeafIndex"); } var outResult = new double[data.Length, NumClasses]; if (data.Length > 0) { long outLen = outResult.GetLength(0) * outResult.GetLength(1); var hdl = GCHandle.Alloc(outResult, GCHandleType.Pinned); try { PInvokeException.Check(PInvoke.BoosterPredictForMats(Handle , data , /*nCol*/ data[0].Length , (PInvoke.CApiPredictType)predictType , (numIteration == -1) ? BestIteration : numIteration , "" , outLen , (double *)hdl.AddrOfPinnedObject().ToPointer() ), nameof(PInvoke.BoosterPredictForMats)); } finally { if (hdl.IsAllocated) { hdl.Free(); } } } return(outResult); }
public double GetLeafValue(int treeIdx, int leafIdx) { double val = 0.0; PInvokeException.Check(PInvoke.BoosterGetLeafValue(Handle, treeIdx, leafIdx, ref val), nameof(PInvoke.BoosterGetLeafValue)); return val; }
public bool Update() { int isFinished = 0; PInvokeException.Check(PInvoke.BoosterUpdateOneIter(Handle, ref isFinished), nameof(PInvoke.BoosterUpdateOneIter)); return isFinished == 1; }
public Booster(Parameters parameters, Dataset trainset, Dataset validset = null) { var param = parameters.ToString(); var handle = IntPtr.Zero; PInvokeException.Check(PInvoke.BoosterCreate(trainset.Handle, param, ref handle), nameof(PInvoke.BoosterCreate)); Handle = handle; if (validset != null) { PInvokeException.Check(PInvoke.BoosterAddValidData(handle, validset.Handle), nameof(PInvoke.BoosterAddValidData)); _hasValid = true; } BestIteration = -1; int numEval = this.EvalCounts; // At most one metric in ML.NET: to do remove this. if (numEval > 1) { throw new Exception($"Expected at most one metric, got {numEval}"); } else if (numEval == 1) { _hasMetric = true; } }
public void Dispose() { if (Handle != IntPtr.Zero) { PInvokeException.Check(PInvoke.BoosterFree(Handle), nameof(PInvoke.BoosterFree)); } Handle = IntPtr.Zero; }
public unsafe bool UpdateCustom(float[] grad, float[] hess) { int isFinished = 0; fixed(float *gradPtr = grad, hessPtr = hess) PInvokeException.Check(PInvoke.BoosterUpdateOneIterCustom(Handle, gradPtr, hessPtr, ref isFinished), nameof(PInvoke.BoosterUpdateOneIterCustom)); return isFinished == 1; }
// Load a booster from a string. Note that I can't use a ctr as would have the same signature as above. public static Booster FromString(string model) { Check.NonNull(model, nameof(model)); var handle = IntPtr.Zero; var numIteration = 0; PInvokeException.Check(PInvoke.BoosterLoadModelFromString(model, ref numIteration, ref handle), nameof(PInvoke.BoosterLoadModelFromString)); return new Booster(handle, numIteration); }
// Load a booster from a model file. public static Booster FromFile(string fileName) { Check.NonNull(fileName, nameof(fileName)); var handle = IntPtr.Zero; var numIteration = 0; PInvokeException.Check(PInvoke.BoosterCreateFromModelfile(fileName, ref numIteration, ref handle), nameof(PInvoke.BoosterCreateFromModelfile)); return new Booster(handle, numIteration); }
public void SetLearningRate(double learningRate) { if (learningRate <= 0.0) { throw new Exception($"Learning rate must be positive (got {learningRate})"); } PInvokeException.Check(PInvoke.BoosterResetParameter(Handle, "learning_rate=" + learningRate), nameof(PInvoke.BoosterResetParameter)); }
public unsafe Dataset(SparseMatrix data, int numCol, CommonParameters cp, DatasetParameters dp, float[] labels = null, float[] weights = null, int[] groups = null, Dataset reference = null) { CommonParameters = cp; DatasetParameters = dp; var pmString = ParamsToString(cp, dp); _handle = IntPtr.Zero; fixed(float *dataPtr = data.Data) fixed(int *indPtr = data.RowExtents, indices = data.ColumnIndices) { PInvokeException.Check(PInvoke.DatasetCreateFromCsr( indPtr, indices, dataPtr, data.RowExtents.Length, data.Data.Length, numCol, pmString, reference?._handle ?? IntPtr.Zero, ref _handle ), nameof(PInvoke.DatasetCreateFromCsr)); } if (labels != null) { SetLabels(labels); } if (weights != null) { SetWeights(weights); } if (groups != null) { SetGroups(groups); } if (NumFeatures != numCol) { throw new Exception("Expected GetNumCols to be equal to numCol"); } if (NumRows != data.RowCount) { throw new Exception("Expected GetNumRows to be equal to numTotalRow"); } }
private unsafe double Eval(int dataIdx) { if (!_hasMetric) return double.NaN; int outLen = 0; double[] res = new double[1]; fixed (double* ptr = res) PInvokeException.Check(PInvoke.BoosterGetEval(Handle, dataIdx, ref outLen, ptr), nameof(PInvoke.BoosterGetEval)); return res[0]; }
// Get the importance of a feature. public unsafe double[] GetFeatureImportance(int numIteration, ImportanceType importanceType) { // Get the number of features int cnt = this.NumFeatures; double[] res = new double[cnt]; fixed (double* ptr = res) PInvokeException.Check(PInvoke.BoosterFeatureImportance(Handle, numIteration, (int)importanceType, ptr), nameof(PInvoke.BoosterFeatureImportance)); return res; }
/* * public static int DatasetCreateFromCsr( * int[] indPtr, * int[] indices, * float[] data, * long nIndPtr, * long numElem, * long numCol, * string parameters, * IntPtr reference, * ref IntPtr ret) * { * return DatasetCreateFromCsr( * indPtr, CApiDType.Int32, * indices, data, CApiDType.Float32, * nIndPtr, numElem, numCol, parameters, reference, ref ret); * } */ /* * public static int DatasetCreateFromCsc( * int[] colPtr, * int[] indices, * float[] data, * long nColPtr, * long nElem, * long numRow, * string parameters, * IntPtr reference, * ref IntPtr ret) * { * return DatasetCreateFromCsc( * colPtr, CApiDType.Int32, * indices, * data, CApiDType.Float32, * nColPtr, nElem, numRow, parameters, reference, ref ret); * } */ /// <summary> /// Create from single matrix /// </summary> //Dataset(float[,] data,bool isRowMajor, Parameters pms = null, Dataset reference = null) //{ // var pmStr = (pms != null) ? pms.ToString() : ""; // var r = (reference != null) ? reference.Handle : IntPtr.Zero; // var rows = data.GetLength(0); // var cols = data.GetLength(1); // PInvokeException.Check(PInvoke.DatasetCreateFromMat(data, rows, cols, isRowMajoe, pmStr, r, ref _handle), // nameof(PInvoke.DatasetCreateFromMat)); //} /* * public static int DatasetCreateFromMat( * float[] data, * int nRow, * int nCol, * bool isRowMajor, * string parameters, * IntPtr reference, * ref IntPtr ret) * { * return DatasetCreateFromMat( * data, CApiDType.Float32, * nRow, nCol, * (isRowMajor ? 1 : 0), * parameters, reference, ref ret); * } */ /* * public static int DatasetCreateFromMats( * float[][] data, * int[] nRow, * int nCol, * bool isRowMajor, * string parameters, * IntPtr reference, * ref IntPtr ret) * { * return DatasetCreateFromMats( * data.Length, * data, CApiDType.Float32, * nRow, nCol, * (isRowMajor ? 1 : 0), * parameters, reference, ref ret); * } */ public unsafe Dataset GetSubset(int[] usedRowIndices, CommonParameters cp = null, DatasetParameters dp = null) { var pmString = ParamsToString(cp, dp); IntPtr p = IntPtr.Zero; fixed(int *usedRowIndices2 = usedRowIndices) PInvokeException.Check(PInvoke.DatasetGetSubset(_handle, usedRowIndices2, usedRowIndices.Length, pmString, ref p), nameof(PInvoke.DatasetGetSubset)); return(new Dataset(p)); }
public void SaveBinary(string fileName) { Check.NonNull(fileName, nameof(fileName)); if (!fileName.EndsWith(".bin")) { throw new ArgumentException(string.Format("File {0} is not a .bin file", fileName)); } PInvokeException.Check(PInvoke.DatasetSaveBinary(_handle, fileName), nameof(PInvoke.DatasetSaveBinary)); }
public unsafe double[] GetPredict(int dataIdx) { long outLen = GetNumPredict(dataIdx); double[] res = new double[outLen]; fixed (double* ptr = res) { PInvokeException.Check(PInvoke.BoosterGetPredict(Handle, dataIdx, ref outLen, ptr), nameof(PInvoke.BoosterGetPredict)); } Debug.Assert(outLen == res.Length); return res; }
public void SaveModel(int startIteration, int numIteration, string fileName) { Check.NonNull(fileName, nameof(fileName)); if (startIteration < 0) throw new ArgumentOutOfRangeException(nameof(startIteration)); if (numIteration < 0) throw new ArgumentOutOfRangeException(nameof(numIteration)); PInvoke.CApiFeatureImportanceType featImp = PInvoke.CApiFeatureImportanceType.Split; // not used? PInvokeException.Check(PInvoke.BoosterSaveModel(Handle, startIteration, numIteration, featImp, fileName), nameof(PInvoke.BoosterSaveModel)); }
public NetworkConfig(int numMachines, int rank, ReduceScatterFunction reduce, AllGatherFunction gather) { if (numMachines < 1) { throw new ArgumentOutOfRangeException("numMachines"); } PInvokeException.Check(PInvoke.NetworkInitWithFunctions(numMachines, rank, reduce, gather), nameof(PInvoke.NetworkInitWithFunctions)); }
//To Do: store this matrix efficiently. public unsafe void Refit(int[,] leafPreds) { Check.NonNull(leafPreds, nameof(leafPreds)); var len1 = leafPreds.GetLength(0);//nrow var len2 = leafPreds.GetLength(1);//ncol var data = new int[len1 * len2]; for (int i = 0; i< len1; ++i) for(int j = 0; j < len2; ++j) { data[i * len2 + j] = leafPreds[i, j]; } fixed (int *dataPtr = data) PInvokeException.Check(PInvoke.BoosterRefit(Handle,dataPtr,len1,len2), nameof(PInvoke.BoosterRefit)); }
public unsafe void SetGroups(int[] groups) { if (groups != null) { fixed(int *ptr = groups) PInvokeException.Check(PInvoke.DatasetSetField(_handle, "group", (IntPtr)ptr, groups.Length, PInvoke.CApiDType.Int32), nameof(PInvoke.DatasetSetField)); } else { PInvokeException.Check(PInvoke.DatasetSetField(_handle, "group", (IntPtr)null, 0, PInvoke.CApiDType.Int32), nameof(PInvoke.DatasetSetField)); } }
// Not used now. Can use for the continued train. public unsafe void SetInitScore(double[] initScores) { if (initScores == null) { throw new ArgumentNullException("initScores"); } if (initScores.Length % NumRows != 0) { throw new ArgumentException("Expected initScores to have a length a multiple of GetNumRows()", "initScores"); fixed(double *ptr = initScores) PInvokeException.Check(PInvoke.DatasetSetField(_handle, "init_score", (IntPtr)ptr, initScores.Length, PInvoke.CApiDType.Float64), nameof(PInvoke.DatasetSetField)); }
public unsafe void SetLabels(float[] labels) { if (labels == null) { throw new ArgumentNullException("labels"); } if (labels.Length != NumRows) { throw new ArgumentException("Expected labels to have a length equal to GetNumRows()", "labels"); fixed(float *ptr = labels) PInvokeException.Check(PInvoke.DatasetSetField(_handle, "label", (IntPtr)ptr, labels.Length, PInvoke.CApiDType.Float32), nameof(PInvoke.DatasetSetField)); }
public void SaveModel(int startIteration, int numIteration, string fileName) { Check.NonNull(fileName, nameof(fileName)); if (startIteration < 0) { throw new ArgumentOutOfRangeException(nameof(startIteration)); } if (numIteration < 0) { throw new ArgumentOutOfRangeException(nameof(numIteration)); } PInvokeException.Check(PInvoke.BoosterSaveModel(Handle, startIteration, numIteration, fileName), nameof(PInvoke.BoosterSaveModel)); }