forked from dmlc/xgboost
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
457 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,3 +72,4 @@ build | |
config.mk | ||
xgboost | ||
*.data | ||
build_plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
XGBoost Plugins Modules | ||
======================= | ||
This folder contains plugin modules to xgboost that can be optionally installed. | ||
The plugin system helps us to extend xgboost with additional features, | ||
and add experimental features that may not yet ready to be included in main project. | ||
|
||
To include a certain plugin, say ```plugin_a```, you only need to add the following line to the config.mk. | ||
|
||
```makefile | ||
# Add plugin by include the plugin in config | ||
include plugin/plugin_a/plugin.mk | ||
``` | ||
|
||
Then rebuild libxgboost by typing make, you can get a new library with the plugin enabled. | ||
|
||
Link Static XGBoost Library with Plugins | ||
---------------------------------------- | ||
This problem only happens when you link ```libxgboost.a```. | ||
If you only use ```libxgboost.so```(this include python and other bindings), | ||
you can ignore this section. | ||
|
||
When you want to link ```libxgboost.a``` with additional plugins included, | ||
you will need to enabled whole archeive via The following option. | ||
```bash | ||
--whole-archive libxgboost.a --no-whole-archive | ||
``` | ||
|
||
Write Your Own Plugin | ||
--------------------- | ||
You can plugin your own modules to xgboost by adding code to this folder, | ||
without modification to the main code repo. | ||
The [example](example) folder provides an example to write a plugin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
XGBoost Plugin Example | ||
====================== | ||
This folder provides an example of xgboost plugin. | ||
|
||
There are three steps you need to to do to add plugin to xgboost | ||
- Create your source .cc file, implement a new extension | ||
- In this example [custom_obj.cc](custom_obj.cc) | ||
- Register this extension to xgboost via registration macr | ||
- In this example ```XGBOOST_REGISTER_OBJECTIVE``` in [this line](custom_obj.cc#L75) | ||
- Create a [plugin.mk](plugin.mk) on this folder | ||
|
||
To add this plugin, add the following line to ```config.mk```(template in make/config.mk). | ||
```makefile | ||
# Add plugin by include the plugin in config | ||
include plugin/example/plugin.mk | ||
``` | ||
|
||
Then you can test this plugin by using ```objective=mylogistic``` parameter. | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/*! | ||
* Copyright 2015 by Contributors | ||
* \file custom_metric.cc | ||
* \brief This is an example to define plugin of xgboost. | ||
* This plugin defines the additional metric function. | ||
*/ | ||
#include <xgboost/base.h> | ||
#include <dmlc/parameter.h> | ||
#include <xgboost/objective.h> | ||
|
||
namespace xgboost { | ||
namespace obj { | ||
|
||
// This is a helpful data structure to define parameters | ||
// You do not have to use it. | ||
// see http://dmlc-core.readthedocs.org/en/latest/parameter.html | ||
// for introduction of this module. | ||
struct MyLogisticParam : public dmlc::Parameter<MyLogisticParam> { | ||
float scale_neg_weight; | ||
// declare parameters | ||
DMLC_DECLARE_PARAMETER(MyLogisticParam) { | ||
DMLC_DECLARE_FIELD(scale_neg_weight).set_default(1.0f).set_lower_bound(0.0f) | ||
.describe("Scale the weight of negative examples by this factor"); | ||
} | ||
}; | ||
|
||
DMLC_REGISTER_PARAMETER(MyLogisticParam); | ||
|
||
// Define a customized logistic regression objective in C++. | ||
// Implement the interface. | ||
class MyLogistic : public ObjFunction { | ||
public: | ||
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override { | ||
param_.InitAllowUnknown(args); | ||
} | ||
void GetGradient(const std::vector<float> &preds, | ||
const MetaInfo &info, | ||
int iter, | ||
std::vector<bst_gpair> *out_gpair) override { | ||
out_gpair->resize(preds.size()); | ||
for (size_t i = 0; i < preds.size(); ++i) { | ||
float w = info.GetWeight(i); | ||
// scale the negative examples! | ||
if (info.labels[i] == 0.0f) w *= param_.scale_neg_weight; | ||
// logistic transoformation | ||
float p = 1.0f / (1.0f + expf(-preds[i])); | ||
// this is the gradient | ||
float grad = (p - info.labels[i]) * w; | ||
// this is the second order gradient | ||
float hess = p * (1.0f - p) * w; | ||
out_gpair->at(i) = bst_gpair(grad, hess); | ||
} | ||
} | ||
const char* DefaultEvalMetric() const override { | ||
return "error"; | ||
} | ||
void PredTransform(std::vector<float> *io_preds) override { | ||
// transform margin value to probability. | ||
std::vector<float> &preds = *io_preds; | ||
for (size_t i = 0; i < preds.size(); ++i) { | ||
preds[i] = 1.0f / (1.0f + expf(-preds[i])); | ||
} | ||
} | ||
float ProbToMargin(float base_score) const override { | ||
// transform probability to margin value | ||
return -std::log(1.0f / base_score - 1.0f); | ||
} | ||
|
||
private: | ||
MyLogisticParam param_; | ||
}; | ||
|
||
// Finally register the objective function. | ||
// After it succeeds you can try use xgboost with objective=mylogistic | ||
XGBOOST_REGISTER_OBJECTIVE(MyLogistic, "mylogistic") | ||
.describe("User defined logistic regression plugin") | ||
.set_body([]() { return new MyLogistic(); }); | ||
|
||
} // namespace obj | ||
} // namespace xgboost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Add the object files you like to include in this plugin. | ||
PLUGIN_OBJS += build_plugin/example/custom_obj.o | ||
# Add additional dependent libraries this plugin might have | ||
PLUGIN_LDFLAGS += |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
PLUGIN_OBJS += build_plugin/lz4/sparse_page_lz4_format.o | ||
PLUGIN_LDFLAGS += -llz4 |
Oops, something went wrong.