forked from VowpalWabbit/vowpal_wabbit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request VowpalWabbit#36 from hal3/master
new version of ldf functionality, many bugs fixed
- Loading branch information
Showing
36 changed files
with
1,242 additions
and
573 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
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,5 @@ | ||
ezexample: temp2.cc ../vowpalwabbit/libvw.a | ||
g++ -g -o $@ -l boost_program_options -l z -l pthread $< -L ../vowpalwabbit -l vw -l allreduce | ||
|
||
library_example: library_example.cc ../vowpalwabbit/libvw.a | ||
g++ -g -o $@ $< -L ../vowpalwabbit -l vw -l allreduce -l boost_program_options -l z -l pthread |
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,131 @@ | ||
#ifndef EZEXAMPLE_H | ||
#define EZEXAMPLE_H | ||
|
||
#include <stdio.h> | ||
#include "../vowpalwabbit/vw.h" | ||
|
||
using namespace std; | ||
typedef uint32_t fid; | ||
|
||
struct vw_namespace { | ||
char namespace_letter; | ||
public: vw_namespace(const char c) : namespace_letter(c) {} | ||
}; | ||
|
||
class ezexample { | ||
private: | ||
vw*vw_ref; | ||
vector<VW::feature_space> *dat; | ||
vector<fid> past_seeds; | ||
fid current_seed; | ||
vector<feature>*current_ns; | ||
char str[2]; | ||
bool pass_empty; | ||
string mylabel; | ||
ezexample(const ezexample & ex); | ||
ezexample & operator=(const ezexample & ex); | ||
|
||
public: | ||
|
||
ezexample(vw*this_vw, bool pe=false) { | ||
dat = new vector<VW::feature_space>(); | ||
vw_ref = this_vw; | ||
current_seed = 0; | ||
current_ns = NULL; | ||
str[0] = ' '; str[1] = 0; | ||
pass_empty = pe; | ||
mylabel = ""; | ||
} | ||
|
||
~ezexample() { | ||
if (dat != NULL) | ||
delete dat; | ||
} | ||
|
||
void addns(char c) { | ||
str[0] = c; | ||
dat->push_back( VW::feature_space(c, vector<feature>()) ); | ||
current_ns = &( dat->at(dat->size()-1).second ); | ||
past_seeds.push_back(current_seed); | ||
current_seed = VW::hash_space(*vw_ref, str); | ||
} | ||
|
||
void remns() { | ||
if (dat->size() == 0) { | ||
current_seed = 0; | ||
current_ns = NULL; | ||
} else { | ||
current_seed = past_seeds.back(); | ||
past_seeds.pop_back(); | ||
dat->pop_back(); | ||
current_ns = &(dat->back().second); | ||
} | ||
} | ||
|
||
inline fid hash(string fstr) { | ||
return VW::hash_feature(*vw_ref, fstr, current_seed); | ||
} | ||
inline fid hash(char* fstr) { | ||
return VW::hash_feature_cstr(*vw_ref, fstr, current_seed); | ||
} | ||
inline fid hash(char c, string fstr) { | ||
str[0] = c; | ||
return VW::hash_feature(*vw_ref, fstr, VW::hash_space(*vw_ref, str)); | ||
} | ||
inline fid hash(char c, char* fstr) { | ||
str[0] = c; | ||
return VW::hash_feature_cstr(*vw_ref, fstr, VW::hash_space(*vw_ref, str)); | ||
} | ||
|
||
inline fid addf(fid fint, float val) { | ||
if (!current_ns) return 0; | ||
feature f = { val, fint }; | ||
current_ns->push_back(f); | ||
return fint; | ||
} | ||
inline fid addf(fid fint ) { return addf(fint , 1.0); } | ||
inline fid addf(string fstr, float val) { return addf(hash(fstr), val); } | ||
inline fid addf(string fstr ) { return addf(hash(fstr), 1.0); } | ||
|
||
float predict() { | ||
static example* empty_example = VW::read_example(*vw_ref, (char*)"| "); | ||
example *ec = VW::import_example(*vw_ref, *dat); | ||
|
||
if (mylabel.length() > 0) | ||
VW::parse_example_label(*vw_ref, *ec, mylabel); | ||
|
||
vw_ref->learn(vw_ref, ec); | ||
if (pass_empty) | ||
vw_ref->learn(vw_ref, empty_example); | ||
float pred = ec->final_prediction; | ||
VW::finish_example(*vw_ref, ec); | ||
return pred; | ||
} | ||
|
||
inline ezexample& set_label(string label) { mylabel = label; return *this; } | ||
inline ezexample& operator()(fid fint ) { addf(fint, 1.0); return *this; } | ||
inline ezexample& operator()(string fstr ) { addf(fstr, 1.0); return *this; } | ||
inline ezexample& operator()(const char* fstr ) { addf(fstr, 1.0); return *this; } | ||
inline ezexample& operator()(fid fint, float val) { addf(fint, val); return *this; } | ||
inline ezexample& operator()(string fstr, float val) { addf(fstr, val); return *this; } | ||
inline ezexample& operator()(const char* fstr, float val) { addf(fstr, val); return *this; } | ||
inline ezexample& operator()(const vw_namespace&n) { addns(n.namespace_letter); return *this; } | ||
inline ezexample& operator--() { remns(); return *this; } | ||
inline float operator()() { return predict(); } | ||
|
||
|
||
void print() { | ||
cerr << "ezexample dat->size=" << dat->size() << ", current_seed=" << current_seed << endl; | ||
for (size_t i=0; i<dat->size(); i++) { | ||
cerr << " namespace(" << dat->at(i).first << "):" << endl; | ||
for (size_t j=0; j<dat->at(i).second.size(); j++) { | ||
cerr << " " << dat->at(i).second[j].weight_index << "\t: " << dat->at(i).second[j].x << endl; | ||
} | ||
} | ||
} | ||
}; | ||
|
||
|
||
|
||
|
||
#endif |
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,75 @@ | ||
#include <stdio.h> | ||
#include "../vowpalwabbit/vw.h" | ||
#include "ezexample.h" | ||
|
||
using namespace std; | ||
|
||
inline feature vw_feature_from_string(vw& v, string fstr, unsigned long seed, float val) | ||
{ | ||
uint32_t foo = VW::hash_feature(v, fstr, seed); | ||
feature f = { val, foo}; | ||
return f; | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
// INITIALIZE WITH WHATEVER YOU WOULD PUT ON THE VW COMMAND LINE -- THIS READS IN A MODEL FROM train.w | ||
vw vw = VW::initialize("--hash all -q st --noconstant -i train.w -t --quiet"); | ||
|
||
// HAL'S SPIFFY INTERFACE USING C++ CRAZINESS | ||
ezexample ex(&vw, false); | ||
ex(vw_namespace('s')) | ||
("p^the_man") | ||
("w^the") | ||
("w^man") | ||
(vw_namespace('t')) | ||
("p^le_homme") | ||
("w^le") | ||
("w^homme"); | ||
cerr << "should be near zero = " << ex() << endl; | ||
|
||
--ex; // remove the most recent namespace | ||
ex(vw_namespace('t')) | ||
("p^un_homme") | ||
("w^un") | ||
("w^homme"); | ||
cerr << "should be near one = " << ex() << endl; | ||
|
||
// AND FINISH UP | ||
vw.finish(&vw); | ||
} | ||
|
||
/* | ||
*/ | ||
|
||
/* | ||
// JOHN'S CLUNKY INTERFACE USING STRINGS | ||
example *vec1 = VW::read_example(vw, (char*)"|s p^the_man w^the w^man |t p^un_homme w^un w^homme"); | ||
vw.learn(&vw, vec1); | ||
cerr << "p1 = " << vec1->final_prediction << endl; | ||
VW::finish_example(vw, vec1); | ||
example *vec2 = VW::read_example(vw, (char*)"|s p^the_man w^the w^man |t p^le_homme w^le w^homme"); | ||
vw.learn(&vw, vec2); | ||
cerr << "p2 = " << vec2->final_prediction << endl; | ||
VW::finish_example(vw, vec2); | ||
// JOHN'S CLUNKY INTERFACE USING VECTORS | ||
vector< VW::feature_space > ec_info; | ||
vector<feature> s_features, t_features; | ||
uint32_t s_hash = VW::hash_space(vw, "s"); | ||
uint32_t t_hash = VW::hash_space(vw, "t"); | ||
s_features.push_back( vw_feature_from_string(vw, "p^the_man", s_hash, 1.0) ); | ||
s_features.push_back( vw_feature_from_string(vw, "w^the", s_hash, 1.0) ); | ||
s_features.push_back( vw_feature_from_string(vw, "w^man", s_hash, 1.0) ); | ||
t_features.push_back( vw_feature_from_string(vw, "p^le_homme", t_hash, 1.0) ); | ||
t_features.push_back( vw_feature_from_string(vw, "w^le", t_hash, 1.0) ); | ||
t_features.push_back( vw_feature_from_string(vw, "w^homme", t_hash, 1.0) ); | ||
ec_info.push_back( VW::feature_space('s', s_features) ); | ||
ec_info.push_back( VW::feature_space('t', t_features) ); | ||
example* vec3 = VW::import_example(vw, ec_info); | ||
vw.learn(&vw, vec3); | ||
cerr << "p3 = " << vec3->final_prediction << endl; | ||
VW::finish_example(vw, vec3); | ||
*/ |
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,12 @@ | ||
#include <iostream> | ||
#include <vector> | ||
|
||
using namespace std; | ||
|
||
int main(int argc, char**argv) { | ||
vector< pair< char, vector<int> > > u = vector< pair< char, vector<int> > >(); | ||
u.push_back( pair< char, vector<int> >('a', vector<int>()) ); | ||
vector<int>*v = &(u[0].second); | ||
v->push_back(0); | ||
cout << "i want this to say one: " << u[0].second.size() << endl; | ||
} |
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,4 @@ | ||
1 |s p^the_man w^the w^man |t p^un_homme w^un w^homme | ||
0 |s p^the_man w^the w^man |t p^le_homme w^le w^homme | ||
0 |s p^a_man w^a w^man |t p^un_homme w^un w^homme | ||
1 |s p^a_man w^a w^man |t p^le_homme w^le w^homme |
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,5 @@ | ||
#!/bin/bash | ||
rm -f train.cache train.w | ||
../vowpalwabbit/vw -c -d train -f train.w -q st --passes 100 --hash all --noconstant | ||
../vowpalwabbit/vw -t -d train -i train.w -p train.pred --noconstant | ||
|
Binary file not shown.
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
Oops, something went wrong.