Predictor

public class Predictor

Treelite Predictor

Author:Philip Cho

Constructors

Predictor

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

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.
  • verbose – Whether to print extra diagnostic messages
  • include_master_thread – Whether the master thread (the thread calling the predict() method) should itself be assigned work. This option is applicable only to batch prediction.
Throws:
  • TreeliteError
Return:

Created Predictor

Methods

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

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 synchronized 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 may be called by only one thread at a time. In order to use multiple threads to process multiple prediction requests simultaneously, use Predictor.predict(Data[], boolean) instead.

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 synchronized 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 may be called by only one thread at a time. In order to use multiple threads to process multiple prediction requests simultaneously, use Predictor.predict(Data[], boolean) instead.

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]