summaryrefslogtreecommitdiff
path: root/perceptron/perceptron1.cc
blob: 0d9957b1720e03b33918253384233de3c8feceb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <iostream>
#include <vector>
#include <utility>
#include <string>
#include <boost/assign/std/vector.hpp>
#include <cassert>
#include <cmath>

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<double> a, vector<double> 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<double>
vadd(double x, vector<double> v)
{
  for (size_t i = 0; i < v.size(); i++) {
    v[i] += x;
  }
  return v;
}

vector<double>
vadd(vector<double> v, vector<double> w)
{
  assert(v.size() == w.size());
  for (size_t i = 0; i < v.size(); i++) {
    v[i] += w[i];
  }
  return v;
}

vector<double>
vsub(vector<double> v, vector<double> w)
{
  assert(v.size() == w.size());
  for (size_t i = 0; i < v.size(); i++) {
    v[i] -= w[i];
  }
  return v;
}

vector<double>
vmult(double x, vector<double> v)
{
  for (size_t i = 0; i < v.size(); i++) {
    v[i] *= x;
  }
  return v;
}

double
vlen(vector<double> v)
{
  return sqrt(dotprod(v, v));
}

vector<double>
vnorm(vector<double> v)
{
  double len = vlen(v);
  for (size_t i = 0; i < v.size(); i++) {
    v[i] /= len;
  }
  return v;
}


int main(void)
{
  vector<double> classes;
  vector<string> words;
  classes += -1.,     1.,      -1.,      1.,       -1.,    1.,      -1.,     1.,         -1.,     1.;
  words   += "Haus","house","Mauer","plant","Zeug","hello","Maus","worker","Quartz","point";

  vector<pair<double, string> > examples;
  for (size_t i = 0; i < classes.size(); i++) {
    pair<double, string> p;
    p.first = classes[i];
    p.second = words[i];
    examples.push_back(p);
  }

  vector<double> 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<double> 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<double> feats;
      feats += isupper_f(examples[x].second), len_f(examples[x].second);
      cout << dotprod(weights, feats) + bias << endl;
    }
    
    vector<double> f;
    f += isupper_f("Bauer"), len_f("Bauer");
    cout << "test: " << dotprod(weights, f) + bias << endl;


  return 0;
}