public void Dispose() { if (Handle != IntPtr.Zero) PInvokeException.Check(PInvoke.BoosterFree(Handle), nameof(PInvoke.BoosterFree)); Handle = IntPtr.Zero; }
public NetworkConfig(CommonParameters pms) { PInvokeException.Check(PInvoke.NetworkInit(pms.Machines, pms.LocalListenPort, pms.TimeOut, pms.NumMachines), nameof(PInvoke.NetworkInit)); }
public void MergeWith(Booster other) { Check.NonNull(other, nameof(other)); PInvokeException.Check(PInvoke.BoosterMerge(Handle, other.Handle), nameof(PInvoke.BoosterMerge)); }
public void ShuffleModels() { PInvokeException.Check(PInvoke.BoosterShuffleModels(Handle), nameof(PInvoke.BoosterShuffleModels)); }
public void SetLeafValue(int treeIdx, int leafIdx, double val) { PInvokeException.Check(PInvoke.BoosterSetLeafValue(Handle, treeIdx, leafIdx, val), nameof(PInvoke.BoosterSetLeafValue)); }
public void ResetTrainingData(Dataset trainset) { Check.NonNull(trainset, nameof(trainset)); PInvokeException.Check(PInvoke.BoosterResetTrainingData(Handle, trainset.Handle), nameof(PInvoke.BoosterResetTrainingData)); }
public void RollbackOneIter() { PInvokeException.Check(PInvoke.BoosterRollbackOneIter(Handle), nameof(PInvoke.BoosterRollbackOneIter)); }
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 void ResetParameter(Parameters pms) { var param = pms.ToString(); PInvokeException.Check(PInvoke.BoosterResetParameter(Handle, param), nameof(PInvoke.BoosterResetParameter)); }
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"); } }
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"); } }