xgboost
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
xgboost::Predictor Class Referenceabstract

Performs prediction on individual training instances or batches of instances for GBTree. The predictor also manages a prediction cache associated with input matrices. If possible, it will use previously calculated predictions instead of calculating new predictions. Prediction functions all take a GBTreeModel and a DMatrix as input and output a vector of predictions. The predictor does not modify any state of the model itself. More...

#include <predictor.h>

Collaboration diagram for xgboost::Predictor:
Collaboration graph

Public Member Functions

 Predictor (GenericParameter const *generic_param, std::shared_ptr< std::unordered_map< DMatrix *, PredictionCacheEntry >> cache)
 
virtual ~Predictor ()=default
 
virtual void Configure (const std::vector< std::pair< std::string, std::string >> &cfg)
 Configure and register input matrices in prediction cache. More...
 
virtual void PredictBatch (DMatrix *dmat, HostDeviceVector< bst_float > *out_preds, const gbm::GBTreeModel &model, int tree_begin, unsigned ntree_limit=0)=0
 Generate batch predictions for a given feature matrix. May use cached predictions if available instead of calculating from scratch. More...
 
virtual void UpdatePredictionCache (const gbm::GBTreeModel &model, std::vector< std::unique_ptr< TreeUpdater >> *updaters, int num_new_trees)=0
 Update the internal prediction cache using newly added trees. Will use the tree updater to do this if possible. Should be called as a part of the tree boosting process to facilitate the look up of predictions at a later time. More...
 
virtual void PredictInstance (const SparsePage::Inst &inst, std::vector< bst_float > *out_preds, const gbm::GBTreeModel &model, unsigned ntree_limit=0)=0
 
virtual void PredictLeaf (DMatrix *dmat, std::vector< bst_float > *out_preds, const gbm::GBTreeModel &model, unsigned ntree_limit=0)=0
 predict the leaf index of each tree, the output will be nsample * ntree vector this is only valid in gbtree predictor. More...
 
virtual void PredictContribution (DMatrix *dmat, std::vector< bst_float > *out_contribs, const gbm::GBTreeModel &model, unsigned ntree_limit=0, std::vector< bst_float > *tree_weights=nullptr, bool approximate=false, int condition=0, unsigned condition_feature=0)=0
 feature contributions to individual predictions; the output will be a vector of length (nfeats + 1) * num_output_group * nsample, arranged in that order. More...
 
virtual void PredictInteractionContributions (DMatrix *dmat, std::vector< bst_float > *out_contribs, const gbm::GBTreeModel &model, unsigned ntree_limit=0, std::vector< bst_float > *tree_weights=nullptr, bool approximate=false)=0
 

Static Public Member Functions

static PredictorCreate (std::string const &name, GenericParameter const *generic_param, std::shared_ptr< std::unordered_map< DMatrix *, PredictionCacheEntry >> cache)
 Creates a new Predictor*. More...
 

Protected Member Functions

std::unordered_map< DMatrix *, PredictionCacheEntry >::iterator FindCache (DMatrix const *dmat)
 

Protected Attributes

GenericParameter const * generic_param_
 
std::shared_ptr< std::unordered_map< DMatrix *, PredictionCacheEntry > > cache_
 Map of matrices and associated cached predictions to facilitate storing and looking up predictions. More...
 

Detailed Description

Performs prediction on individual training instances or batches of instances for GBTree. The predictor also manages a prediction cache associated with input matrices. If possible, it will use previously calculated predictions instead of calculating new predictions. Prediction functions all take a GBTreeModel and a DMatrix as input and output a vector of predictions. The predictor does not modify any state of the model itself.

Constructor & Destructor Documentation

◆ Predictor()

xgboost::Predictor::Predictor ( GenericParameter const *  generic_param,
std::shared_ptr< std::unordered_map< DMatrix *, PredictionCacheEntry >>  cache 
)
inline

◆ ~Predictor()

virtual xgboost::Predictor::~Predictor ( )
virtualdefault

Member Function Documentation

◆ Configure()

virtual void xgboost::Predictor::Configure ( const std::vector< std::pair< std::string, std::string >> &  cfg)
virtual

Configure and register input matrices in prediction cache.

Parameters
cfgThe configuration.

◆ Create()

static Predictor* xgboost::Predictor::Create ( std::string const &  name,
GenericParameter const *  generic_param,
std::shared_ptr< std::unordered_map< DMatrix *, PredictionCacheEntry >>  cache 
)
static

Creates a new Predictor*.

Parameters
nameName of the predictor.
generic_paramPointer to runtime parameters.
cachePointer to prediction cache.

◆ FindCache()

std::unordered_map<DMatrix*, PredictionCacheEntry>::iterator xgboost::Predictor::FindCache ( DMatrix const *  dmat)
inlineprotected

◆ PredictBatch()

virtual void xgboost::Predictor::PredictBatch ( DMatrix dmat,
HostDeviceVector< bst_float > *  out_preds,
const gbm::GBTreeModel &  model,
int  tree_begin,
unsigned  ntree_limit = 0 
)
pure virtual

Generate batch predictions for a given feature matrix. May use cached predictions if available instead of calculating from scratch.

Parameters
[in,out]dmatFeature matrix.
[in,out]out_predsThe output preds.
modelThe model to predict from.
tree_beginThe tree begin index.
ntree_limit(Optional) The ntree limit. 0 means do not limit trees.

◆ PredictContribution()

void xgboost::Predictor::PredictContribution ( DMatrix dmat,
std::vector< bst_float > *  out_contribs,
const gbm::GBTreeModel &  model,
unsigned  ntree_limit = 0,
std::vector< bst_float > *  tree_weights = nullptr,
bool  approximate = false,
int  condition = 0,
unsigned  condition_feature = 0 
)
pure virtual

feature contributions to individual predictions; the output will be a vector of length (nfeats + 1) * num_output_group * nsample, arranged in that order.

Parameters
[in,out]dmatThe input feature matrix.
[in,out]out_contribsThe output feature contribs.
modelModel to make predictions from.
ntree_limit(Optional) The ntree limit.
tree_weights(Optional) Weights to multiply each tree by.
approximateUse fast approximate algorithm.
conditionCondition on the condition_feature (0=no, -1=cond off, 1=cond on).
condition_featureFeature to condition on (i.e. fix) during calculations.

◆ PredictInstance()

virtual void xgboost::Predictor::PredictInstance ( const SparsePage::Inst inst,
std::vector< bst_float > *  out_preds,
const gbm::GBTreeModel &  model,
unsigned  ntree_limit = 0 
)
pure virtual

◆ PredictInteractionContributions()

virtual void xgboost::Predictor::PredictInteractionContributions ( DMatrix dmat,
std::vector< bst_float > *  out_contribs,
const gbm::GBTreeModel &  model,
unsigned  ntree_limit = 0,
std::vector< bst_float > *  tree_weights = nullptr,
bool  approximate = false 
)
pure virtual

◆ PredictLeaf()

void xgboost::Predictor::PredictLeaf ( DMatrix dmat,
std::vector< bst_float > *  out_preds,
const gbm::GBTreeModel &  model,
unsigned  ntree_limit = 0 
)
pure virtual

predict the leaf index of each tree, the output will be nsample * ntree vector this is only valid in gbtree predictor.

Parameters
[in,out]dmatThe input feature matrix.
[in,out]out_predsThe output preds.
modelModel to make predictions from.
ntree_limit(Optional) The ntree limit.

◆ UpdatePredictionCache()

void xgboost::Predictor::UpdatePredictionCache ( const gbm::GBTreeModel &  model,
std::vector< std::unique_ptr< TreeUpdater >> *  updaters,
int  num_new_trees 
)
pure virtual

Update the internal prediction cache using newly added trees. Will use the tree updater to do this if possible. Should be called as a part of the tree boosting process to facilitate the look up of predictions at a later time.

Parameters
modelThe model.
[in,out]updatersThe updater sequence for gradient boosting.
num_new_treesNumber of new trees.

Member Data Documentation

◆ cache_

std::shared_ptr<std::unordered_map<DMatrix*, PredictionCacheEntry> > xgboost::Predictor::cache_
protected

Map of matrices and associated cached predictions to facilitate storing and looking up predictions.

◆ generic_param_

GenericParameter const* xgboost::Predictor::generic_param_
protected

The documentation for this class was generated from the following file: