forked from iqbaleff214/simple-single-layer-perceptron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjst-03.html
85 lines (64 loc) · 2.25 KB
/
jst-03.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl"
crossorigin="anonymous"
/>
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<script
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0"
crossorigin="anonymous"
></script>
<script>
const ALPHA = 0.0002;
const DATA = [
// infections, infected countries
[5.0, 5.0],
[5.0, 1.0],
[7.0, 4.0],
[12.0, 5.0],
];
const nextDayInfections = [5.0, 3.0, 5.5, 8.5];
var weights = [0.56, 0.43];
const weightedSum = (data, weights) => {
var prediction = 0;
for (const [i, weight] of weights.entries()) {
prediction += data[i] * weight;
}
return prediction;
};
const updateWeights = (dataPoint, prediction, trueInfectedCount) => {
for (const [i, d] of dataPoint.entries()) {
const update = (prediction - trueInfectedCount) * d;
weights[i] -= ALPHA * update;
}
};
const neuralNet = (data, weights) => weightedSum(data, weights);
const error = (prediction, trueValue) => (prediction - trueValue) ** 2;
for (const i of Array(100).keys()) {
var errors = 0;
console.log(`epoch ${i + 1}`);
for (const [j, dataPoint] of DATA.entries()) {
const prediction = neuralNet(dataPoint, weights);
const trueInfectedCount = nextDayInfections[j];
errors += error(prediction, trueInfectedCount);
updateWeights(dataPoint, prediction, trueInfectedCount);
console.log(`prediction: ${prediction}`);
}
const epochError = errors / DATA.length;
console.log(`error: ${epochError}\n`);
}
</script>
</body>
</html>