Skip to content

Commit

Permalink
FIX misc.
Browse files Browse the repository at this point in the history
  • Loading branch information
jihelhere committed Oct 4, 2011
1 parent 41e6023 commit daa471b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ Multiclass Problems. In: Journal of Machine Learning Research 3, 951-991.
Dependencies
------------

1. a c++0x-aware compiler, for example gcc > 4.0
1. a c++0x-aware compiler, for example gcc > 4.4
2. autotools
3. if your OS doesn't provide a c++0x thread implementation, you need the boost library (only tested with boost 1.47)

Compiling
---------

./bootstrap
./configure --enable-debug=false
./configure --enable-debug=false [--with-boost=(yes|path_to_boost)]
make
make install

Expand Down Expand Up @@ -72,7 +73,7 @@ example:

1 1:-12 2:1.4
0.01 3:1.7


Remark
------
Expand Down
4 changes: 2 additions & 2 deletions bootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
if [ "$1" == cleanup ]
then
make clean
rm -rf depcomp install-sh missing Makefile Makefile.in stamp-h1 config.status configure config.log config.h* autom4te.cache/ aclocal.m4 src/.deps src/Makefile src/Makefile.in INSTALL config.guess config.sub ltmain.sh libtool m4/ src/.libs test/letter.scale.*
rm -rf depcomp install-sh missing Makefile Makefile.in stamp-h1 config.status configure config.log config.h* autom4te.cache/ aclocal.m4 src/.deps src/Makefile src/Makefile.in INSTALL config.guess config.sub ltmain.sh libtool src/.libs test/letter.scale.*
else
#libtoolize
aclocal
autoconf
autoheader
automake --add-missing --foreign
autoreconf
autoreconf --install

./configure
fi
37 changes: 17 additions & 20 deletions src/ranker-learn-iteration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@
#define CLIP 0.2
#define LOOP 10

//TODO: remove
using namespace std;

static int verbose_flag = 0;

struct Example {
double loss;
double score;
int label;
unordered_map<int, double> features;
std::unordered_map<int, double> features;

Example() : loss(0.0), score(0.0), label(0), features() {};
Example(double lo, double sc, int la) : loss(lo), score(sc), label(la), features() {};
Expand Down Expand Up @@ -91,12 +88,12 @@ struct mira_operator
double delta = example->loss - oracle->loss - (oracle->score - example->score);

//copy
unordered_map<int, double> difference = oracle->features;
std::unordered_map<int, double> difference = oracle->features;
double norm = 0;

unordered_map<int, double>::iterator end = example->features.end();
std::unordered_map<int, double>::iterator end = example->features.end();

for(unordered_map<int, double>::iterator j = example->features.begin(); j != end; ++j) {
for(std::unordered_map<int, double>::iterator j = example->features.begin(); j != end; ++j) {
double&ref = difference[j->first];
ref -= j->second;
norm += ref*ref;
Expand All @@ -109,7 +106,7 @@ struct mira_operator
double avgalpha = alpha * avgUpdate;

end = difference.end();
for(unordered_map<int, double>::iterator j = difference.begin(); j != end; ++j) {
for(std::unordered_map<int, double>::iterator j = difference.begin(); j != end; ++j) {
weights[j->first] += alpha * j->second;
avgWeights[j->first] += avgalpha * j->second;
}
Expand All @@ -121,7 +118,7 @@ struct mira_operator



int process(char* filename, int num_iterations, vector<double> &weights, vector<double> &avgWeights, unordered_map<string, int> &features, int &next_id, int iteration, int num_examples, bool alter_model, double clip)
int process(char* filename, int num_iterations, std::vector<double> &weights, std::vector<double> &avgWeights, std::unordered_map<std::string, int> &features, int &next_id, int iteration, int num_examples, bool alter_model, double clip)
{
size_t buffer_size = 0;
char* buffer = NULL;
Expand All @@ -139,7 +136,7 @@ int process(char* filename, int num_iterations, vector<double> &weights, vector<
double one_best_loss = 0;
bool is_one_best = true;

vector<Example*> examples;
std::vector<Example*> examples;
Example* oracle = NULL;
Example* official_oracle = NULL;

Expand Down Expand Up @@ -209,7 +206,7 @@ int process(char* filename, int num_iterations, vector<double> &weights, vector<
examples.push_back(example);


string token_as_string;
std::string token_as_string;

// read a line and fill label/features
for(token = strtok(buffer, " \t"); token != NULL; token = strtok(NULL, " \t\n")) {
Expand All @@ -233,7 +230,7 @@ int process(char* filename, int num_iterations, vector<double> &weights, vector<
}
else {
token_as_string = token;
unordered_map<string, int>::iterator id = features.find(token_as_string);
std::unordered_map<std::string, int>::iterator id = features.find(token_as_string);
unsigned int location = 0;
if(id == features.end()) {
if(!alter_model) continue; // skip unseen features
Expand All @@ -250,10 +247,10 @@ int process(char* filename, int num_iterations, vector<double> &weights, vector<
}

double value_as_double = strtod(value + 1, NULL);
if(!isinf(value_as_double) && !isnan(value_as_double)) {
if(!std::isinf(value_as_double) && !std::isnan(value_as_double)) {
example->features[location] = value_as_double;
//fprintf(stdout, "%s %d %g\n", token, location, value_as_double);
//if(iteration == 1) fprintf(stdout, "%s %g %g %g\n", token, vector_last(values), weights[location], weights[location + 1]);
//if(iteration == 1) fprintf(stdout, "%s %g %g %g\n", token, std::vector_last(values), weights[location], weights[location + 1]);
example->score += value_as_double * weights[location];
}
}
Expand Down Expand Up @@ -396,9 +393,9 @@ int main(int argc, char** argv) {
}


vector<double> weights;
vector<double> avgWeights;
unordered_map<string, int> features;
std::vector<double> weights;
std::vector<double> avgWeights;
std::unordered_map<std::string, int> features;
int next_id = 0;

if(num_examples == -1)
Expand All @@ -419,7 +416,7 @@ int main(int argc, char** argv) {
char* weight = strchr(buffer, ' ');
*weight = '\0';
double value = strtod(weight + 1, NULL);
features[string(buffer)] = next_id;
features[std::string(buffer)] = next_id;
weights.push_back(value);
avgWeights.push_back(value * (num_examples * num_iterations));
next_id++;
Expand All @@ -434,8 +431,8 @@ int main(int argc, char** argv) {
fprintf(stderr, "num iterations %d\n", num_iterations);
process(trainset, num_iterations, weights, avgWeights, features, next_id, iteration, num_examples, true, clip);

unordered_map<string, int>::iterator end = features.end();
for( unordered_map<string, int>::iterator i = features.begin(); i != end; ++i) {
std::unordered_map<std::string, int>::iterator end = features.end();
for( std::unordered_map<std::string, int>::iterator i = features.begin(); i != end; ++i) {
if(weights[i->second] != 0) {
fprintf(stdout, "%s %32.31g\n", i->first.c_str(), weights[i->second]);
}
Expand Down
7 changes: 3 additions & 4 deletions src/ranker-learn.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,11 @@ double process(const char* filename, const char* filter, std::vector<double> &we
// empty line -> end of examples for this instance
else if(lines.size() > 0) {
// std::vector<ranker::ExampleMaker*> exampleMakers(num_threads, NULL);
std::vector<ranker::ExampleMaker*> exampleMakers;
exampleMakers.reserve(num_threads);
std::vector<ranker::ExampleMaker*> exampleMakers(num_threads, NULL);

for(int i = 0; i < num_threads; ++i) {
exampleMakers.push_back(new ranker::ExampleMaker(lines, weights));
exampleMakers[i]->start(i*lines.size()/num_threads, (i+1)*lines.size()/num_threads);
exampleMakers[i] = new ranker::ExampleMaker(lines, weights);
exampleMakers[i]->start(i*lines.size()/num_threads, (i+1)*lines.size()/num_threads);
}


Expand Down

0 comments on commit daa471b

Please sign in to comment.