コード例 #1
0
        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));
        }
コード例 #2
0
        public void ResetParameter(Parameters pms)
        {
            var param = pms.ToString();

            PInvokeException.Check(PInvoke.BoosterResetParameter(Handle, param),
                                   nameof(PInvoke.BoosterResetParameter));
        }
コード例 #3
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 // 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;
 }
コード例 #4
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
        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;
        }
コード例 #5
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public long GetNumPredict(int dataIdx)
 {
     long outLen = 0;
     PInvokeException.Check(PInvoke.BoosterGetNumPredict(Handle, dataIdx, ref outLen),
                             nameof(PInvoke.BoosterGetNumPredict));
     return outLen;
 }
コード例 #6
0
        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));
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        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);
        }
コード例 #9
0
        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);
        }
コード例 #10
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 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;
 }
コード例 #11
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public bool Update()
 {
     int isFinished = 0;
     PInvokeException.Check(PInvoke.BoosterUpdateOneIter(Handle, ref isFinished),
                            nameof(PInvoke.BoosterUpdateOneIter));
     return isFinished == 1;
 }
コード例 #12
0
        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;
            }
        }
コード例 #13
0
 public void Dispose()
 {
     if (Handle != IntPtr.Zero)
     {
         PInvokeException.Check(PInvoke.BoosterFree(Handle), nameof(PInvoke.BoosterFree));
     }
     Handle = IntPtr.Zero;
 }
コード例 #14
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 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;
 }
コード例 #15
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 // 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);
 }
コード例 #16
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 // 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);
 }
コード例 #17
0
 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));
 }
コード例 #18
0
        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");
            }
        }
コード例 #19
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 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];
 }
コード例 #20
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
        // 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;
        }
コード例 #21
0
/*
 *      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));
        }
コード例 #22
0
        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));
        }
コード例 #23
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 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;
 }
コード例 #24
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
        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));
        }
コード例 #25
0
ファイル: NetworkConfig.cs プロジェクト: rca22/LightGBM.Net
        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));
        }
コード例 #26
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 //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));
 }
コード例 #27
0
 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));
     }
 }
コード例 #28
0
        // 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));
        }
コード例 #29
0
        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));
        }
コード例 #30
0
        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));
        }