Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
adding excludeLHS flag (#156)
Browse files Browse the repository at this point in the history
* adding excludeLHS flag

* excludeLHS + basedoc

Only `excludeLHS` if `-basedoc` is empty
  • Loading branch information
bkj authored and ledw committed Jun 18, 2018
1 parent 2016eaa commit 01f0f75
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
4 changes: 3 additions & 1 deletion examples/recomm_user_artists.sh
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ echo "Start to evaluate trained model:"
-label "A" \
-thread 40 \
-trainMode 1 \
-verbose true
-K 10 \
-verbose true \
-predictionFile artist_recs
21 changes: 17 additions & 4 deletions src/starspace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,8 @@ void StarSpace::predictOne(
Metrics StarSpace::evaluateOne(
const vector<Base>& lhs,
const vector<Base>& rhs,
vector<Predictions>& pred) {
vector<Predictions>& pred,
bool excludeLHS) {

std::priority_queue<Predictions> heap;

Expand Down Expand Up @@ -342,9 +343,21 @@ Metrics StarSpace::evaluateOne(
// get the first K predictions
int i = 0;
while (i < args_->K && heap.size() > 0) {
pred.push_back(heap.top());
Predictions heap_top = heap.top();
heap.pop();
i++;

bool keep = true;
if(excludeLHS && (args_->basedoc.empty())) {
int nwords = dict_->nwords();
auto it = std::find_if( lhs.begin(), lhs.end(),
[&heap_top, &nwords](const Base& el){ return (el.first - nwords + 1) == heap_top.second;} );
keep = it == lhs.end();
}

if(keep) {
pred.push_back(heap_top);
i++;
}
}

Metrics s;
Expand Down Expand Up @@ -390,7 +403,7 @@ void StarSpace::evaluate() {
auto evalThread = [&] (int idx, int start, int end) {
metrics[idx].clear();
for (int i = start; i < end; i++) {
auto s = evaluateOne(examples[i].LHSTokens, examples[i].RHSTokens, predictions[i]);
auto s = evaluateOne(examples[i].LHSTokens, examples[i].RHSTokens, predictions[i], args_->excludeLHS);
metrics[idx].add(s);
}
};
Expand Down
3 changes: 2 additions & 1 deletion src/starspace.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ class StarSpace {
Metrics evaluateOne(
const std::vector<Base>& lhs,
const std::vector<Base>& rhs,
std::vector<Predictions>& pred);
std::vector<Predictions>& pred,
bool excludeLHS);

std::shared_ptr<Dictionary> dict_;
std::shared_ptr<DataParser> parser_;
Expand Down
4 changes: 4 additions & 0 deletions src/utils/args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Args::Args() {
saveTempModel = false;
useWeight = false;
trainWord = false;
excludeLHS = false;
}

bool Args::isTrue(string arg) {
Expand Down Expand Up @@ -185,6 +186,8 @@ void Args::parseArgs(int argc, char** argv) {
useWeight = isTrue(string(argv[i + 1]));
} else if (strcmp(argv[i], "-trainWord") == 0) {
trainWord = isTrue(string(argv[i + 1]));
} else if (strcmp(argv[i], "-excludeLHS") == 0) {
excludeLHS = isTrue(string(argv[i + 1]));
} else {
cerr << "Unknown argument: " << argv[i] << std::endl;
printHelp();
Expand Down Expand Up @@ -277,6 +280,7 @@ void Args::printHelp() {
<< " In the case -fileFormat='fastText' and -basedoc is not provided, we compare true label with all other labels in the dictionary.\n"
<< " -predictionFile file path for save predictions. If not empty, top K predictions for each example will be saved.\n"
<< " -K if -predictionFile is not empty, top K predictions for each example will be saved.\n"
<< " -excludeLHS exclude elements in the LHS from predictions\n"
<< "\nThe following arguments are optional:\n"
<< " -normalizeText whether to run basic text preprocess for input files [" << normalizeText << "]\n"
<< " -useWeight whether input file contains weights [" << useWeight << "]\n"
Expand Down
1 change: 1 addition & 0 deletions src/utils/args.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Args {
bool shareEmb;
bool useWeight;
bool trainWord;
bool excludeLHS;

void parseArgs(int, char**);
void printHelp();
Expand Down

0 comments on commit 01f0f75

Please sign in to comment.