Skip to content

Commit c94c1d9

Browse files
Merge pull request #70 from Krymancer/ml-perceptron
Adding perceptron algorithm
2 parents 599a0ba + 2f7be15 commit c94c1d9

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed
+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include <iostream>
2+
#include <cstdlib>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
double f(double x){
8+
const double m = 0.3;
9+
const double b = 0.2;
10+
return m * x + b;
11+
}
12+
13+
class Perceptron{
14+
public:
15+
vector<double> weights;
16+
double lr;
17+
18+
Perceptron(int inputLength, double learningRate){
19+
weights.resize(inputLength,0);
20+
21+
for(int i=0; i<weights.size(); i++){
22+
weights[i] = -1 + 2 * (double)rand()/RAND_MAX;
23+
}
24+
25+
lr = learningRate;
26+
}
27+
28+
double sing(double value){
29+
return value > 0 ? 1 : -1;
30+
}
31+
32+
double guess(vector<double> inputs){
33+
double sum = 0;
34+
35+
for(int i=0; i<inputs.size(); i++){
36+
sum += inputs[i] * weights[i];
37+
}
38+
39+
return sing(sum);
40+
}
41+
42+
void train(vector<double> inputs, double target){
43+
double attempt = guess(inputs);
44+
double error = target - attempt;
45+
46+
for(int i=0; i<weights.size(); i++){
47+
weights[i] += error * inputs[i] * lr;
48+
}
49+
}
50+
};
51+
52+
class Point{
53+
public:
54+
double x,y,label;
55+
double bias = 1;
56+
57+
Point(){
58+
x = -1 + 2 * (double)rand()/RAND_MAX;
59+
y = -1 + 2 * (double)rand()/RAND_MAX;
60+
label = y >= f(x) ? 1 : -1;
61+
}
62+
};
63+
64+
int main(){
65+
srand(time(0));
66+
67+
int pointssize = 1000;
68+
double trainedRate = 0;
69+
double untrainedRate = 0;
70+
71+
Perceptron perceptron(3,0.1);
72+
73+
vector<Point> points;
74+
points.resize(pointssize);
75+
76+
for(int i=0; i<points.size(); i++){
77+
Point p;
78+
points[i] = p;
79+
//cout << points[i].x << endl;
80+
}
81+
82+
for(int i=0; i<points.size(); i++){
83+
vector<double> inputs;
84+
inputs.push_back(points[i].x);
85+
inputs.push_back(points[i].y);
86+
inputs.push_back(points[i].bias);
87+
88+
double target = points[i].label;
89+
double guess = perceptron.guess(inputs);
90+
91+
if(guess == target){
92+
untrainedRate++;
93+
}
94+
}
95+
96+
for(int i=0; i<points.size(); i++){
97+
vector<double> inputs;
98+
inputs.push_back(points[i].x);
99+
inputs.push_back(points[i].y);
100+
inputs.push_back(points[i].bias);
101+
double target = points[i].label;
102+
103+
perceptron.train(inputs,target);
104+
}
105+
106+
for(int i=0; i<points.size(); i++){
107+
vector<double> inputs;
108+
inputs.push_back(points[i].x);
109+
inputs.push_back(points[i].y);
110+
inputs.push_back(points[i].bias);
111+
112+
double target = points[i].label;
113+
double guess = perceptron.guess(inputs);
114+
115+
if(guess == target){
116+
trainedRate++;
117+
}
118+
}
119+
120+
cout << "Trained: " << trainedRate/pointssize << " Untrained: " << untrainedRate/pointssize << endl;
121+
122+
return 0;
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
class Perceptron{
2+
constructor(inputLength, learningRate){
3+
this.weights = [];
4+
this.learningRate = learningRate;
5+
6+
for(let i=0; i < inputLength; i++){
7+
this.weights[i] = this.random(1,-1);
8+
}
9+
}
10+
11+
sing(value){
12+
return value > 0 ? 1 : -1;
13+
}
14+
15+
random(max,min){
16+
return Math.random() * (max - min) + min;
17+
}
18+
19+
guess(inputs){
20+
let sum = 0;
21+
22+
this.weights.forEach((weight,index) => {
23+
sum += inputs[index] * weight;
24+
});
25+
26+
return this.sing(sum);
27+
}
28+
29+
train(inputs, target){
30+
const guess = this.guess(inputs);
31+
const error = target - guess;
32+
33+
for(let i=0; i < this.weights.length; i++){
34+
this.weights[i] += error * inputs[i] * this.learningRate;
35+
}
36+
}
37+
}
38+
39+
class Point{
40+
constructor(){
41+
this.x = this.random(1,-1);
42+
this.y = this.random(1,-1);
43+
//this.label = this.x > this.y ? 1 : -1;
44+
this.label = this.y >= f(this.x) ? 1 : -1;
45+
this.bias = 1;
46+
}
47+
48+
random(max,min){
49+
return Math.random() * (max - min) + min;
50+
}
51+
}
52+
53+
function f(x){
54+
const m = 0.3;
55+
const b = 0.2;
56+
return m * x + b;
57+
}
58+
59+
function main(){
60+
const perceptron = new Perceptron(3,0.1);
61+
const points = [];
62+
const pointsLenght = 1000;
63+
let rate = 0;
64+
let untrainedRate = 0;
65+
66+
for(let i=0; i < pointsLenght; i++){
67+
points.push(new Point());
68+
}
69+
70+
points.forEach(point => {
71+
const input = [point.x, point.y, point.bias];
72+
const target = point.label;
73+
const guess = perceptron.guess(input);
74+
75+
if(guess === target){
76+
untrainedRate++;
77+
}
78+
});
79+
80+
points.forEach(point => {
81+
const input = [point.x, point.y, point.bias];
82+
const target = point.label;
83+
84+
perceptron.train(input, target);
85+
});
86+
87+
points.forEach(point => {
88+
const input = [point.x, point.y, point.bias];
89+
const target = point.label;
90+
const guess = perceptron.guess(input);
91+
92+
if(guess === target){
93+
rate++;
94+
}
95+
});
96+
97+
console.log('Trained:', rate/pointsLenght,
98+
'Untrained:',untrainedRate/pointsLenght);
99+
}
100+
101+
102+
main();

0 commit comments

Comments
 (0)