added code for instance based weighing for rank objectives (#3379)

* added code for instance based weighing for rank objectives

* Fix lint
This commit is contained in:
ngoyal2707
2018-06-22 15:10:59 -07:00
committed by Philip Hyunsu Cho
parent d062c6f61b
commit 5cd851ccef
5 changed files with 85 additions and 10 deletions

View File

@@ -37,6 +37,7 @@ class LambdaRankObj : public ObjFunction {
void Configure(const std::vector<std::pair<std::string, std::string> >& args) override {
param_.InitAllowUnknown(args);
}
void GetGradient(HostDeviceVector<bst_float>* preds,
const MetaInfo& info,
int iter,
@@ -50,6 +51,7 @@ class LambdaRankObj : public ObjFunction {
const std::vector<unsigned> &gptr = info.group_ptr_.size() == 0 ? tgptr : info.group_ptr_;
CHECK(gptr.size() != 0 && gptr.back() == info.labels_.size())
<< "group structure not consistent with #rows";
const auto ngroup = static_cast<bst_omp_uint>(gptr.size() - 1);
#pragma omp parallel
{
@@ -60,6 +62,11 @@ class LambdaRankObj : public ObjFunction {
std::vector<LambdaPair> pairs;
std::vector<ListEntry> lst;
std::vector< std::pair<bst_float, unsigned> > rec;
bst_float sum_weights = 0;
for (bst_omp_uint k = 0; k < ngroup; ++k) {
sum_weights += info.GetWeight(k);
}
bst_float weight_normalization_factor = ngroup/sum_weights;
#pragma omp for schedule(static)
for (bst_omp_uint k = 0; k < ngroup; ++k) {
lst.clear(); pairs.clear();
@@ -85,9 +92,11 @@ class LambdaRankObj : public ObjFunction {
for (unsigned pid = i; pid < j; ++pid) {
unsigned ridx = std::uniform_int_distribution<unsigned>(0, nleft + nright - 1)(rnd);
if (ridx < nleft) {
pairs.emplace_back(rec[ridx].second, rec[pid].second);
pairs.emplace_back(rec[ridx].second, rec[pid].second,
info.GetWeight(k) * weight_normalization_factor);
} else {
pairs.emplace_back(rec[pid].second, rec[ridx+j-i].second);
pairs.emplace_back(rec[pid].second, rec[ridx+j-i].second,
info.GetWeight(k) * weight_normalization_factor);
}
}
}
@@ -152,6 +161,9 @@ class LambdaRankObj : public ObjFunction {
// constructor
LambdaPair(unsigned pos_index, unsigned neg_index)
: pos_index(pos_index), neg_index(neg_index), weight(1.0f) {}
// constructor
LambdaPair(unsigned pos_index, unsigned neg_index, bst_float weight)
: pos_index(pos_index), neg_index(neg_index), weight(weight) {}
};
/*!
* \brief get lambda weight for existing pairs
@@ -205,7 +217,7 @@ class LambdaRankObjNDCG : public LambdaRankObj {
((1 << neg_label) - 1) * pos_loginv + ((1 << pos_label) - 1) * neg_loginv;
bst_float delta = (original - changed) * IDCG;
if (delta < 0.0f) delta = - delta;
pair.weight = delta;
pair.weight *= delta;
}
}
}
@@ -301,7 +313,7 @@ class LambdaRankObjMAP : public LambdaRankObj {
std::vector<MAPStats> map_stats;
GetMAPStats(sorted_list, &map_stats);
for (auto & pair : pairs) {
pair.weight =
pair.weight *=
GetLambdaMAP(sorted_list, pair.pos_index,
pair.neg_index, &map_stats);
}