-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Carlos Becker
committed
May 26, 2017
1 parent
3abff37
commit 145bb44
Showing
6 changed files
with
165 additions
and
1 deletion.
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
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,34 @@ | ||
#ifndef LIGHTGBM_PREDICTION_EARLY_STOP_H_ | ||
#define LIGHTGBM_PREDICTION_EARLY_STOP_H_ | ||
|
||
#include <functional> | ||
#include <string> | ||
|
||
#include <LightGBM/export.h> | ||
|
||
namespace LightGBM | ||
{ | ||
struct PredictionEarlyStopInstance | ||
{ | ||
/// Callback function type for early stopping. | ||
/// Takes current prediction and number of elements in prediction | ||
/// @returns true if prediction should stop according to criterion | ||
using FunctionType = std::function<bool(const double*, int)>; | ||
|
||
FunctionType callbackFunction; // callback function itself | ||
int roundPeriod; // call callbackFunction every `runPeriod` iterations | ||
}; | ||
|
||
struct PredictionEarlyStopConfig | ||
{ | ||
int roundPeriod; | ||
double marginThreshold; | ||
}; | ||
|
||
/// Create an early stopping algorithm of type `type`, with given roundPeriod and margin threshold | ||
LIGHTGBM_EXPORT PredictionEarlyStopInstance createPredictionEarlyStopInstance(const std::string& type, | ||
const PredictionEarlyStopConfig& config); | ||
|
||
} // namespace LightGBM | ||
|
||
#endif // LIGHTGBM_PREDICTION_EARLY_STOP_H_ |
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
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,88 @@ | ||
#include <LightGBM/prediction_early_stop.h> | ||
|
||
using namespace LightGBM; | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
#include <cmath> | ||
|
||
namespace | ||
{ | ||
PredictionEarlyStopInstance createNone(const PredictionEarlyStopConfig&) | ||
{ | ||
return PredictionEarlyStopInstance{ | ||
[](const double*, int) | ||
{ | ||
return false; | ||
}, 100000000 // arbitrary number high enough so that the function will rarely be called | ||
}; | ||
} | ||
|
||
PredictionEarlyStopInstance createMulticlass(const PredictionEarlyStopConfig& config) | ||
{ | ||
// marginThreshold will be captured by value | ||
const double marginThreshold = config.marginThreshold; | ||
|
||
return PredictionEarlyStopInstance{ | ||
[marginThreshold](const double* pred, int sz) | ||
{ | ||
// copy and sort | ||
std::vector<double> votes(static_cast<size_t>(sz)); | ||
for (int i=0; i < sz; ++i) | ||
votes[i] = pred[i]; | ||
std::partial_sort(votes.begin(), votes.begin() + 2, votes.end(), std::greater<double>()); | ||
|
||
const auto margin = votes[0] - votes[1]; | ||
|
||
if (margin > marginThreshold) | ||
return true; | ||
|
||
return false; | ||
}, | ||
config.roundPeriod | ||
}; | ||
} | ||
|
||
PredictionEarlyStopInstance createBinary(const PredictionEarlyStopConfig& config) | ||
{ | ||
// marginThreshold will be captured by value | ||
const double marginThreshold = config.marginThreshold; | ||
|
||
return PredictionEarlyStopInstance{ | ||
[marginThreshold](const double* pred, int) | ||
{ | ||
const auto margin = 2.0 * fabs(pred[0]); | ||
|
||
if (margin > marginThreshold) | ||
return true; | ||
|
||
return false; | ||
}, | ||
config.roundPeriod | ||
}; | ||
} | ||
} | ||
|
||
namespace LightGBM | ||
{ | ||
PredictionEarlyStopInstance createPredictionEarlyStopInstance(const std::string& type, | ||
const PredictionEarlyStopConfig& config) | ||
{ | ||
if (type == "none") | ||
{ | ||
return createNone(config); | ||
} | ||
else if (type == "multiclass") | ||
{ | ||
return createMulticlass(config); | ||
} | ||
else if (type == "binary") | ||
{ | ||
return createBinary(config); | ||
} | ||
else | ||
{ | ||
throw std::runtime_error("Unknown early stopping type: " + type); | ||
} | ||
} | ||
} |