#include #include #include #include #include #include #include using namespace std; using namespace boost::assign; double isupper_f(string s) { if ( isupper(s[0]) ) return 0; return 1; } double len_f(string s) { return s.size(); } double dotprod(vector a, vector b) { assert(a.size() == b.size()); double sum; for (size_t i = 0; i < a.size(); i++) { sum += a[i] * b[i]; } return sum; } vector vadd(double x, vector v) { for (size_t i = 0; i < v.size(); i++) { v[i] += x; } return v; } vector vadd(vector v, vector w) { assert(v.size() == w.size()); for (size_t i = 0; i < v.size(); i++) { v[i] += w[i]; } return v; } vector vsub(vector v, vector w) { assert(v.size() == w.size()); for (size_t i = 0; i < v.size(); i++) { v[i] -= w[i]; } return v; } vector vmult(double x, vector v) { for (size_t i = 0; i < v.size(); i++) { v[i] *= x; } return v; } double vlen(vector v) { return sqrt(dotprod(v, v)); } vector vnorm(vector v) { double len = vlen(v); for (size_t i = 0; i < v.size(); i++) { v[i] /= len; } return v; } int main(void) { vector classes; vector words; classes += -1., 1., -1., 1., -1., 1., -1., 1., -1., 1.; words += "Haus","house","Mauer","plant","Zeug","hello","Maus","worker","Quartz","point"; vector > examples; for (size_t i = 0; i < classes.size(); i++) { pair p; p.first = classes[i]; p.second = words[i]; examples.push_back(p); } vector weights; weights += 0.,0.; double eta = 0.5; double bias = 0.; for (size_t t = 0; t < 2; t++) { for (size_t x = 0; x < examples.size(); x++) { vector feats; feats += isupper_f(examples[x].second), len_f(examples[x].second); double err = dotprod(weights, feats); //if ( (examples[x].first == 1 && err < 0) || (examples[x].first == -1 && err >= 0) ) { if ( examples[x].first * (err + bias) <= 0 ) { weights = vadd(weights, vmult(examples[x].first*eta, feats)); bias += examples[x].first*eta; } cout << "w " << weights[0] << " " << weights[1] << " bias " << bias << endl; } cout << "--" << endl; } cout << endl; for (size_t x = 0; x < examples.size(); x++) { vector feats; feats += isupper_f(examples[x].second), len_f(examples[x].second); cout << dotprod(weights, feats) + bias << endl; } vector f; f += isupper_f("Bauer"), len_f("Bauer"); cout << "test: " << dotprod(weights, f) + bias << endl; return 0; }