Use bst_float consistently throughout (#1824)
* Fix various typos * Add override to functions that are overridden gcc gives warnings about functions that are being overridden by not being marked as oveirridden. This fixes it. * Use bst_float consistently Use bst_float for all the variables that involve weight, leaf value, gradient, hessian, gain, loss_chg, predictions, base_margin, feature values. In some cases, when due to additions and so on the value can take a larger value, double is used. This ensures that type conversions are minimal and reduces loss of precision.
This commit is contained in:
@@ -33,35 +33,35 @@ class MyLogistic : public ObjFunction {
|
||||
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
|
||||
param_.InitAllowUnknown(args);
|
||||
}
|
||||
void GetGradient(const std::vector<float> &preds,
|
||||
void GetGradient(const std::vector<bst_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);
|
||||
bst_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]));
|
||||
// logistic transformation
|
||||
bst_float p = 1.0f / (1.0f + std::exp(-preds[i]));
|
||||
// this is the gradient
|
||||
float grad = (p - info.labels[i]) * w;
|
||||
bst_float grad = (p - info.labels[i]) * w;
|
||||
// this is the second order gradient
|
||||
float hess = p * (1.0f - p) * w;
|
||||
bst_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 {
|
||||
void PredTransform(std::vector<bst_float> *io_preds) override {
|
||||
// transform margin value to probability.
|
||||
std::vector<float> &preds = *io_preds;
|
||||
std::vector<bst_float> &preds = *io_preds;
|
||||
for (size_t i = 0; i < preds.size(); ++i) {
|
||||
preds[i] = 1.0f / (1.0f + expf(-preds[i]));
|
||||
preds[i] = 1.0f / (1.0f + std::exp(-preds[i]));
|
||||
}
|
||||
}
|
||||
float ProbToMargin(float base_score) const override {
|
||||
bst_float ProbToMargin(bst_float base_score) const override {
|
||||
// transform probability to margin value
|
||||
return -std::log(1.0f / base_score - 1.0f);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user