-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started api; implemented parallel predictor
- Loading branch information
Benoit Favre
committed
Jul 25, 2011
1 parent
8bd5146
commit 2169916
Showing
15 changed files
with
393 additions
and
8 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
|
||
ACLOCAL_AMFLAGS=-I m4 | ||
SUBDIRS= src |
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
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,6 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
int ranker(int x) { | ||
return x * 2; | ||
} |
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 |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
#define LOOP 10 | ||
#define NUM_THREADS 1 | ||
|
||
using namespace ranker; | ||
|
||
static int verbose_flag = 0; | ||
|
||
|
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,95 @@ | ||
|
||
#include <stdio.h> | ||
#include "ranker.hh" | ||
|
||
using namespace ranker; | ||
|
||
predictor::predictor(int num_threads, std::string modelname, std::string mappingname) { | ||
this->num_threads = num_threads; | ||
load_model(modelname); | ||
load_mapping(mappingname); | ||
} | ||
|
||
predictor::predictor(int num_threads, std::string modelname) { | ||
this->num_threads = num_threads; | ||
load_model(modelname); | ||
} | ||
|
||
void predictor::load_model(std::string filename) { | ||
FILE* fp = fopen(filename.c_str(), "r"); | ||
if(!fp) { | ||
fprintf(stderr, "ERROR: cannot load model from \"%s\"\n", filename.c_str()); | ||
return; | ||
} | ||
|
||
size_t buffer_size = 0; | ||
char* buffer = NULL; | ||
int length = 0; | ||
|
||
while(0 < (length = getline(&buffer, &buffer_size, fp))) { | ||
buffer[length - 1] = '\0'; // chop | ||
char* weight1 = strchr(buffer, ' '); | ||
*weight1 = '\0'; | ||
char* weight2 = strchr(weight1 + 1, ' '); | ||
if(weight2 != NULL) { | ||
*weight2 = '\0'; | ||
model[strtol(buffer, NULL, 10)] = strtod(weight2 + 1, NULL); | ||
} else { | ||
model[strtol(buffer, NULL, 10)] = strtod(weight1 + 1, NULL); | ||
} | ||
} | ||
fclose(fp); | ||
} | ||
|
||
void predictor::load_mapping(std::string filename) { | ||
FILE* fp = fopen(filename.c_str(), "r"); | ||
if(!fp) { | ||
fprintf(stderr, "ERROR: cannot load mapping from \"%s\"\n", filename.c_str()); | ||
return; | ||
} | ||
|
||
size_t buffer_size = 0; | ||
char* buffer = NULL; | ||
int length = 0; | ||
|
||
int next_id = 1; | ||
while(0 < (length = getline(&buffer, &buffer_size, fp))) { | ||
buffer[length - 1] = '\0'; // chop | ||
char* end = strchr(buffer, ' '); | ||
*end = '\0'; | ||
auto found = model.find(next_id); | ||
if(found != model.end()) { | ||
mapping[std::string(buffer)] = next_id; | ||
} | ||
next_id++; | ||
} | ||
fclose(fp); | ||
} | ||
|
||
int predictor::map(std::string feature) { | ||
auto found = mapping.find(feature); | ||
if(found != mapping.end()) return found->second; | ||
return 0; | ||
} | ||
|
||
int predictor::predict(std::vector<example> &nbest) { | ||
int argmax = -1; | ||
double max = 0; | ||
for(size_t i = 0; i < nbest.size(); i++) { | ||
compute_score(nbest[i]); | ||
if(argmax == -1 || nbest[i].score > max) { | ||
max = nbest[i].score; | ||
argmax = i; | ||
} | ||
} | ||
return argmax; | ||
} | ||
|
||
double predictor::compute_score(example& x) { | ||
double score = 0; | ||
for(auto i = x.features.begin(); i != x.features.end(); i++) { | ||
score += model[i->id] * i->value; | ||
} | ||
x.score = score; | ||
return score; | ||
} |
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 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
#include <unordered_map> | ||
#include "Example.hh" | ||
|
||
namespace ranker { | ||
struct predictor { | ||
int num_threads; | ||
std::unordered_map<std::string, int> mapping; | ||
std::unordered_map<int, double> model; | ||
predictor(int num_threads, std::string modelname); | ||
predictor(int num_threads, std::string modelname, std::string mappingname); | ||
void load_model(std::string filename); | ||
void load_mapping(std::string filename); | ||
int map(std::string feature); | ||
int predict(std::vector<example>& nbest); | ||
double compute_score(example& i); | ||
}; | ||
} |
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,46 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include "ranker.hh" | ||
|
||
int main(int argc, char** argv) { | ||
if(argc < 2) { | ||
fprintf(stderr, "usage: %s <model>\n", argv[0]); | ||
return 1; | ||
} | ||
ranker::predictor model(1, std::string(argv[1])); | ||
|
||
char* buffer = NULL; | ||
size_t buffer_length = 0; | ||
ssize_t length = 0; | ||
|
||
std::vector<ranker::example> examples; | ||
while(0 <= (length = getline(&buffer, &buffer_length, stdin))) { | ||
if(length == 1) { | ||
fprintf(stdout, "%d\n", model.predict(examples)); | ||
examples.clear(); | ||
} else { | ||
ranker::example x; | ||
char *inputstring = buffer; | ||
char *token = NULL; | ||
token = strsep(&inputstring, " \t"); // skip label | ||
for(;(token = strsep(&inputstring, " \t\n"));) { | ||
if(!strcmp(token,"")) continue; | ||
char* value = strrchr(token, ':'); | ||
if(value != NULL) { | ||
*value = '\0'; | ||
double value_as_double = strtod(value + 1, NULL); | ||
//nbe is the loss, not a feature | ||
if(!strcmp(token, "nbe")) { | ||
x.loss = value_as_double; | ||
} else { | ||
int location = strtol(token, NULL, 10); | ||
x.features.push_back(ranker::feature(location, value_as_double)); | ||
} | ||
} | ||
} | ||
examples.push_back(x); | ||
} | ||
} | ||
return 0; | ||
} |
Oops, something went wrong.