コード例 #1
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public void Dispose()
 {
     if (Handle != IntPtr.Zero)
         PInvokeException.Check(PInvoke.BoosterFree(Handle), nameof(PInvoke.BoosterFree));
     Handle = IntPtr.Zero;
 }
コード例 #2
0
ファイル: NetworkConfig.cs プロジェクト: rca22/LightGBM.Net
 public NetworkConfig(CommonParameters pms)
 {
     PInvokeException.Check(PInvoke.NetworkInit(pms.Machines, pms.LocalListenPort, pms.TimeOut, pms.NumMachines),
                            nameof(PInvoke.NetworkInit));
 }
コード例 #3
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public void MergeWith(Booster other)
 {
     Check.NonNull(other, nameof(other));
     PInvokeException.Check(PInvoke.BoosterMerge(Handle, other.Handle),
                            nameof(PInvoke.BoosterMerge));
 }
コード例 #4
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public void ShuffleModels()
 {
     PInvokeException.Check(PInvoke.BoosterShuffleModels(Handle), nameof(PInvoke.BoosterShuffleModels));
 }
コード例 #5
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public void SetLeafValue(int treeIdx, int leafIdx, double val)
 {
     PInvokeException.Check(PInvoke.BoosterSetLeafValue(Handle, treeIdx, leafIdx, val),
                            nameof(PInvoke.BoosterSetLeafValue));
 }
コード例 #6
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public void ResetTrainingData(Dataset trainset)
 {
     Check.NonNull(trainset, nameof(trainset));
     PInvokeException.Check(PInvoke.BoosterResetTrainingData(Handle, trainset.Handle),
                            nameof(PInvoke.BoosterResetTrainingData));
 }
コード例 #7
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public void RollbackOneIter()
 {
     PInvokeException.Check(PInvoke.BoosterRollbackOneIter(Handle),
                            nameof(PInvoke.BoosterRollbackOneIter));
 }
コード例 #8
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 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));
 }
コード例 #9
0
ファイル: Booster.cs プロジェクト: rca22/LightGBM.Net
 public void ResetParameter(Parameters pms)
 {
     var param = pms.ToString();
     PInvokeException.Check(PInvoke.BoosterResetParameter(Handle, param),
                            nameof(PInvoke.BoosterResetParameter));
 }
コード例 #10
0
ファイル: Dataset.cs プロジェクト: kdjsrt/LightGBM.Net
        public unsafe Dataset(double[][] sampleValuePerColumn,
                              int[][] sampleIndicesPerColumn,
                              int numCol,
                              int[] sampleNonZeroCntPerColumn,
                              int numSampleRow,
                              int numTotalRow,
                              CommonParameters cp,
                              DatasetParameters dp,
                              float[] labels  = null,
                              float[] weights = null,
                              int[] groups    = null)
        {
            CommonParameters  = cp;
            DatasetParameters = dp;
            var pmString = ParamsToString(cp, dp);

            _handle = IntPtr.Zero;

            // Use GCHandle to pin the memory, avoid the memory relocation.
            GCHandle[] gcValues  = new GCHandle[numCol];
            GCHandle[] gcIndices = new GCHandle[numCol];
            try
            {
                double *[] ptrArrayValues  = new double *[numCol];
                int *[]    ptrArrayIndices = new int *[numCol];
                for (int i = 0; i < numCol; i++)
                {
                    gcValues[i]        = GCHandle.Alloc(sampleValuePerColumn[i], GCHandleType.Pinned);
                    ptrArrayValues[i]  = (double *)gcValues[i].AddrOfPinnedObject().ToPointer();
                    gcIndices[i]       = GCHandle.Alloc(sampleIndicesPerColumn[i], GCHandleType.Pinned);
                    ptrArrayIndices[i] = (int *)gcIndices[i].AddrOfPinnedObject().ToPointer();
                }
                ;
                fixed(double **ptrValues = ptrArrayValues)
                fixed(int **ptrIndices = ptrArrayIndices)
                fixed(int *ptrSampleNonZeroCntPerColumn = sampleNonZeroCntPerColumn)
                {
                    PInvokeException.Check(PInvoke.DatasetCreateFromSampledColumn(
                                               (IntPtr)ptrValues, (IntPtr)ptrIndices, numCol, ptrSampleNonZeroCntPerColumn, numSampleRow, numTotalRow,
                                               pmString, ref _handle), nameof(PInvoke.DatasetCreateFromSampledColumn));
                }
            }
            finally
            {
                for (int i = 0; i < numCol; i++)
                {
                    if (gcValues[i].IsAllocated)
                    {
                        gcValues[i].Free();
                    }
                    if (gcIndices[i].IsAllocated)
                    {
                        gcIndices[i].Free();
                    }
                }
                ;
            }
            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 != numTotalRow)
            {
                throw new Exception("Expected GetNumRows to be equal to numTotalRow");
            }
        }
コード例 #11
0
ファイル: Dataset.cs プロジェクト: kdjsrt/LightGBM.Net
        public unsafe Dataset(float[][] 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;

            var gcHandles = new List <GCHandle>(data.Length);

            try
            {
                float *[] dataPtrs = new float *[data.Length];
                int[]     nRows    = new int[data.Length];
                for (int i = 0; i < data.Length; i++)
                {
                    var hdl = GCHandle.Alloc(data[i], GCHandleType.Pinned);
                    gcHandles.Add(hdl);
                    dataPtrs[i] = (float *)hdl.AddrOfPinnedObject().ToPointer();
                    nRows[i]    = 1;
                }
                ;
                fixed(float **dataPtr = dataPtrs)
                fixed(int *nRowsPtr = nRows)
                {
                    PInvokeException.Check(PInvoke.DatasetCreateFromMats(
                                               data.Length,
                                               dataPtr,
                                               nRowsPtr,
                                               numCol,
                                               /*isRowMajor*/ true,
                                               pmString,
                                               reference?._handle ?? IntPtr.Zero,
                                               ref _handle
                                               ), nameof(PInvoke.DatasetCreateFromMats));
                }
            }
            finally
            {
                foreach (var hdl in gcHandles)
                {
                    if (hdl.IsAllocated)
                    {
                        hdl.Free();
                    }
                }
                ;
            }
            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.Length)
            {
                throw new Exception("Expected GetNumRows to be equal to numTotalRow");
            }
        }