Perceptron with Margin

It is a simple simulation of Perceptron Algorithm using p5.js.

You can insert the data-points belonging to two classes as well as change the Learning Rate and Threshold(or Margin) on canvas at runtime using Sliders and simulate how the Linear Seperater converges to classify the given data.

You can interact on web browsers by simply click the index.html file on your browser.

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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0>
<style> body {padding: 0; margin: 0;} </style>
<script src="../p5.min.js"></script>
<script src="../addons/p5.dom.min.js"></script>
<script src="../addons/p5.sound.min.js"></script>
<script src="sketch.js"></script>
<style>
body {
padding: 0;
margin: 0;
}
canvas {
vertical-align: top;
}
html {
font-family: monospace;
color: #333;
font-size: 20px;
}
</style>
</head>
<body>
</body>
</html>

User can insert the data-points belonging to two classes as well as change the Learning Rate and Threshold or Margin on canvas at runtime using Sliders and simulate how the Linear Separater converges to classify the given data. sketch.js contains the core logic for the simulation.

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
instruction = "Tap on the Screen to Insert Data Points...";

var epsilonSlider, epsilonSpan;
var minPointsSlider;
var changeColor;

// Data
X1 = [];
X2 = [];
Y = [];

// Hyperparameters
b = 0.0;
w1 = 0.0;
w2 = 0.0;

var threshold = 100.0;
var learning_rate = 0.1;

// Parameters
var x1, x2;
var y1, y2;
var m, b;
var type = 1;

var flag = false;

function setup() {
createCanvas(windowWidth, windowHeight - 25);

createSpan("Learning Rate: ");
epsilonSlider = createSlider(0, 100, 10);
epsilonSpan = createSpan(epsilonSlider.value());
createSpan(" | ");
createSpan("Threshold: ");
minPointsSlider = createSlider(0, 300, 100);
minPointsSpan = createSpan(epsilonSlider.value());
createSpan(" | ");
changeColor = createButton("Change Color");
changeColor.mousePressed(swap);
createSpan(" | ");
train = createButton("Start Training");
train.mousePressed(function () {
flag = true;
});
}

function draw() {
background(51);

learning_rate = epsilonSlider.value() / 1000;
epsilonSpan.html(learning_rate.toString());
threshold = minPointsSlider.value();
minPointsSpan.html(threshold.toString());

fill(250);
noStroke();
textFont('monospace');
textSize(25);
text("Perceptron with Margin", 15, 40);
textSize(20);
text(instruction, 15, windowHeight - 60);

noStroke();
for (var i = 0; i < X1.length; i++) {
Y[i] == 1 ? fill(255, 0, 0) : fill(0, 0, 255);
ellipse(X1[i], X2[i], 8, 8);
}

if (flag) {
fit();
}

drawLine();
stroke(255, 0, 255);
line(x1, y1+threshold, x2, y2+threshold);
line(x1, y1-threshold, x2, y2-threshold);

stroke(255, 255, 0);
line(x1, y1, x2, y2);
}

function mouseClicked() {
X1.push(mouseX);
X2.push(mouseY);
Y.push(type);
}

function swap() {
type = -type;
}

function activate(y, threshold) {
if (y > threshold) {
return 1;
} else if (y <= -threshold) {
return -1;
}
return 0;
}

function fit() {
for (var i = 0; i < X1.length; i++) {
y = w1 * X1[i] + w2 * X2[i] + b;
y = activate(y, threshold);

console.log(" " + y + " " + Y[i]);

if (y != Y[i]) {
console.log("Here");

var x1 = map(X1[i], 0, width, 0, 1);
var x2 = map(X2[i], 0, height, 0, 1);

w1 += learning_rate * Y[i] * x1;
w2 += learning_rate * Y[i] * x2;
b += learning_rate * Y[i];
}
}
}

function keyPressed() {
if (keyCode == ENTER) {
swap();
} else if (keyCode == ESCAPE) {
flag = true;
}
}

function drawLine() {
x1 = 0;
x2 = width;

m = -(w1 / w2);
c = -(b / w2);

y1 = m * x1 + c;
y2 = m * x2 + c;
}


文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/04/07/Perceptron-with-Margin/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏