Predictor

public class Predictor implements Serializable

Treelite Predictor

Author

Hyunsu Cho

Constructors

Predictor

public Predictor(String libpath, int nthread, boolean verbose)

Create a Predictor by loading a shared library (dll/so/dylib). The library is expected to contain the compiled code for making prediction for a particular tree ensemble model. The predictor also spawns a fixed-size pool of worker threads, who will wait for prediction tasks. (Note that the worker threads will go to sleep when no prediction task is available, to free up CPU cycles for other processes.)

Parameters
  • libpath – Path to the shared library

  • nthread – Number of workers threads to spawn. Set to -1 to use default, i.e., to launch as many threads as CPU cores available on the system. You are not allowed to launch more threads than CPU cores. Setting nthread=1 indicates that the main thread should be exclusively used.

  • verbose – Whether to print extra diagnostic messages

Throws
  • TreeliteError

Return

Created Predictor

Methods

GetGlobalBias

public float GetGlobalBias()

Get global bias which adjusting predicted margin scores.

Returns

Value of global bias

GetNumFeature

public int GetNumFeature()

Get the number of features used by the compiled model. Call this method to allocate array for storing data entries of a single instance.

Returns

Number of features

GetNumOutputGroup

public int GetNumOutputGroup()

Get the number of output groups for the compiled model. This number is 1 for tasks other than multi-class classification. For multi-class classification task, the number is equal to the number of classes.

Returns

Number of output groups

GetPredTransform

public String GetPredTransform()

Get name of post prediction transformation used to train the loaded model.

Returns

Name of (post-)prediction transformation

GetSigmoidAlpha

public float GetSigmoidAlpha()

Get alpha value in sigmoid transformation used to train the loaded model.

Returns

Alpha value of sigmoid transformation

dispose

public synchronized void dispose()

Destructor, to be called when the object is garbage collected

finalize

protected void finalize()

predict

public float[] predict(Data[] inst, boolean pred_margin)

Perform single-instance prediction. Prediction is run by the calling thread.

Parameters
  • inst – array of data entires comprising the instance

  • pred_margin – whether to predict a probability or a raw margin score

Returns

Resulting predictions, of dimension [num_output_group]

predict

public float[][] predict(SparseBatch batch, boolean verbose, boolean pred_margin)

Perform batch prediction with a 2D sparse data matrix. Worker threads will internally divide up work for batch prediction. Note that this function will be blocked by mutex when worker_thread > 1. In order to use multiple threads to process multiple prediction requests simultaneously, use Predictor.predict(Data[], boolean) instead or keep worker_thread = 1.

Parameters
  • batch – a SparseBatch, representing a slice of a 2D sparse matrix

  • verbose – whether to print extra diagnostic messages

  • pred_margin – whether to predict probabilities or raw margin scores

Returns

Resulting predictions, of dimension [num_row]*[num_output_group]

predict

public float[][] predict(DenseBatch batch, boolean verbose, boolean pred_margin)

Perform batch prediction with a 2D dense data matrix. Worker threads will internally divide up work for batch prediction. Note that this function will be blocked by mutex when worker_thread > 1. In order to use multiple threads to process multiple prediction requests simultaneously, use Predictor.predict(Data[], boolean) instead or keep worker_thread = 1.

Parameters
  • batch – a DenseBatch, representing a slice of a 2D dense matrix

  • verbose – whether to print extra diagnostic messages

  • pred_margin – whether to predict probabilities or raw margin scores

Returns

Resulting predictions, of dimension [num_row]*[num_output_group]