Skip to content

Instantly share code, notes, and snippets.

@felipecruz
Created November 19, 2014 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipecruz/092ffe9198e41e40b90c to your computer and use it in GitHub Desktop.
Save felipecruz/092ffe9198e41e40b90c to your computer and use it in GitHub Desktop.
Perceptron
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
class Perceptron {
vector<double> w;
double learning_rate;
bool error;
public:
Perceptron(vector<double> _w, double _learning_rate) {
w = _w;
learning_rate = _learning_rate;
error = false;
};
~Perceptron() {};
vector<double> pred(vector<double> x);
void train(vector<vector<double>> x, vector<double> y);
double sum(vector<double> xw);
};
vector<double> Perceptron::pred(vector<double> x) {
vector<double>::const_iterator x_it = x.begin();
vector<double>::const_iterator w_it = w.begin();
vector<double> y = vector<double>();
for (;x_it != x.end() && w_it != w.end(); ++x_it, ++w_it) {
y.push_back((*x_it) * (*w_it));
}
return y;
}
double add(double x1, double x2) {
return x1 + x2;
}
int sign(double x) {
return (x > 0) ? 1 : ((x < 0) ? -1 : 0);
}
double Perceptron::sum(vector<double> xw) {
double y = 0.0;
for (auto _y : xw) {
y += _y;
}
return y;
}
void Perceptron::train(vector<vector<double>> xs, vector<double> ys) {
vector<vector<double>>::const_iterator xs_it = xs.begin();
vector<double>::const_iterator ys_it = ys.begin();
int c = 0;
error = true;
while (error) {
c++;
error = false;
for (;xs_it != xs.end() && ys_it != ys.end(); ++xs_it, ++ys_it) {
double y_hat = sum(pred(*xs_it));
double y = *ys_it * y_hat;
if (sign(y) <= 0) {
for (size_t i = 0; i < w.size(); i ++) {
w[i] += y * (*xs_it)[i];
}
error = true;
}
}
}
cout << "Iterations to convergence: " << c << endl;
error = false;
}
int main(int argc, char *argv[]) {
vector<double> x({0.01, 0.01});
Perceptron p(x, 0.02);
vector<double> x1({0.0, 0.0});
vector<double> x2({1.0, 0.0});
vector<double> x3({0.0, 1.0});
vector<double> x4({1.0, 1.0});
double y1 = 0.0;
double y2 = 1.0;
double y3 = 1.0;
double y4 = 1.0;
vector<vector<double>> dataset({x1, x2, x3, x4});
vector<double> ys({y1, y2, y3, y4});
p.train(dataset, ys);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment