From 26c490f404731d053a6205719b6246502c07b449 Mon Sep 17 00:00:00 2001 From: Patrick Simianer Date: Sat, 14 Jun 2014 16:46:27 +0200 Subject: init --- perceptron/perceptron.cc | 156 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 perceptron/perceptron.cc (limited to 'perceptron/perceptron.cc') diff --git a/perceptron/perceptron.cc b/perceptron/perceptron.cc new file mode 100644 index 0000000..3aa6a72 --- /dev/null +++ b/perceptron/perceptron.cc @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace boost::assign; + +typedef vector vec; + + +/* + * features + * + */ +double +isupper_f(string s) +{ + if ( isupper(s[0]) ) return 0; + return 1; +} + +double +len_f(string s) +{ + return s.size(); +} + + +/* + * vector ops + * + */ +double +dotprod( vec a, vec b ) +{ + assert( a.size() == b.size() ); + double sum; + for ( size_t i = 0; i < a.size(); i++ ) { + sum += a[i] * b[i]; + } + return sum; +} + +vec +vadd( double x, vec v ) +{ + for ( size_t i = 0; i < v.size(); i++ ) { + v[i] += x; + } + return v; +} + +vec +vadd( vec v, vec w ) +{ + assert( v.size() == w.size() ); + for ( size_t i = 0; i < v.size(); i++ ) { + v[i] += w[i]; + } + return v; +} + +vec +vsub( vec v, vec w ) +{ + assert(v.size() == w.size()); + for (size_t i = 0; i < v.size(); i++) { + v[i] -= w[i]; + } + return v; +} + +vec +vmult( double x, vec v ) +{ + for (size_t i = 0; i < v.size(); i++) { + v[i] *= x; + } + return v; +} + +double +vlen( vec v ) +{ + return sqrt( dotprod(v, v) ); +} + +vec +vnorm( vec v ) +{ + double len = vlen(v); + for (size_t i = 0; i < v.size(); i++) { + v[i] /= len; + } + return v; +} + + +int +main( void ) +{ + vec 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); + } + + vec weights; + weights += 0.,0.,0.; + + double eta = 0.5; + + for (size_t t = 0; t < 2; t++) + { + for (size_t x = 0; x < examples.size(); x++) { + vec feats; + feats += 1.; + 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 <= 0 ) { + weights = vadd(weights, vmult(examples[x].first*eta, feats)); + } + cout << "w " << weights[0] << " " << weights[1] << " " << weights[2] << endl; + } + cout << "--" << endl; + } + + cout << endl; + for (size_t x = 0; x < examples.size(); x++) { + vec feats; + feats += 1.; + feats += isupper_f(examples[x].second), len_f(examples[x].second); + cout << dotprod(weights, feats) << endl; + } + + vec f; + f += 1.; + f += isupper_f("Bauer"), len_f("Bauer"); + cout << "test: " << dotprod(weights, f) << endl; + + + return 0; +} + -- cgit v1.2.3