-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
182 lines (171 loc) · 4.4 KB
/
main.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
*
* Neural Network for Nim
*
* This is the network used to play an optimal game of nim. It spits out garbage if the postion is loosing.
*
* Cody Smith
* 2019
*
*/
import { Connection } from "./connection.js";
import { Network } from "./Network.js";
import {
getBatch,
convertBatchToInputAndOutput
} from "./generateTraingingData.js";
setTimeout(testBackProp, 1);
function plot(x, y) {
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: "line",
data: {
labels: x,
datasets: [
{
label: "error",
data: y,
borderWidth: 1
}
]
},
options: {
scales: {
yAxes: [
{
type: "logarithmic",
ticks: {
beginAtZero: true
}
}
]
}
}
});
}
export function testBackProp() {
const net = new Network();
window.nimNet = net;
const c1 = new Connection(19, 19);
c1.fullyConnect();
net.addConnection(c1);
const c2 = new Connection(19, 32);
c2.fullyConnect();
net.addConnection(c2);
const c3 = new Connection(32, 32);
c3.fullyConnect();
net.addConnection(c3);
const c4 = new Connection(32, 19);
c4.fullyConnect();
net.addConnection(c4);
const c5 = new Connection(19, 4);
c5.fullyConnect();
net.addConnection(c5);
const c6 = new Connection(4, 4);
c6.fullyConnect();
net.addConnection(c6);
const c7 = new Connection(4, 1);
c7.fullyConnect();
net.addConnection(c7);
net.maintainBiasNeurons();
//
//Object.freeze(net);
var LEARNINGRATE = 0.00000000000005;
var myLearningRate = LEARNINGRATE;
let iters = [];
let errors = [];
function iterate(n = 0) {
if (n > 1000) {
return;
}
let batch = getBatch(300);
myLearningRate = LEARNINGRATE * (0.5 * Math.random());
if (errors[errors.length - 1] < 2) myLearningRate = myLearningRate * 0.01;
let binaryBatch = convertBatchToInputAndOutput(batch);
net.backPropogation(binaryBatch, myLearningRate, true);
console.log(n);
// test
let batch2 = getBatch(50);
let binaryBatch2 = convertBatchToInputAndOutput(batch2);
let errorSum = 0;
for (var b of binaryBatch2) {
let fwd = net.forwardPropogate(b.input);
errorSum += numeric.norm2(
numeric.subVV(fwd.activations[fwd.activations.length - 1], b.output)
);
}
errorSum = errorSum / batch2.length;
iters.push(n);
errors.push(errorSum);
console.log({ errorSum });
if (n % 50 == 0) {
try {
plot(iters, errors);
let can = net.draw();
can.id = "flubber";
let div = document.getElementById("flubber");
if (div) document.body.removeChild(div);
document.body.appendChild(can);
console.log("net", net);
} catch (er) {}
}
setTimeout(iterate, 1, n + 1);
}
iterate();
}
export function testBackPropToy() {
const net = new Network();
const c1 = new Connection(4, 4); //give me some space
c1.fullyConnect(undefined, true);
net.addConnection(c1);
const c2 = new Connection(4, 2);
c2.fullyConnect(undefined, true);
net.addConnection(c2);
window.addNet = net;
//
//
function getAddBatch(n = 1000) {
let res = [];
for (var i = 0; i < n; i++) {
let x = Math.random() * 10;
let y = Math.random() * 10;
let z = Math.random() * 10;
res.push({ input: [1, x, y, z], output: [1, x + y + z / 2] });
}
return res;
}
let iters = [];
let errors = [];
function iterate(n = 0) {
if (n > 3000) {
plot(iters, errors);
return;
}
let batch = getAddBatch(500);
// let binaryBatch = convertBatchToInputAndOutput(batch);
net.backPropogation(batch, 0.000001, false);
console.log(n);
// test
let batch2 = getAddBatch(100);
// let binaryBatch2 = convertBatchToInputAndOutput(batch2);
let errorSum = 0;
for (var b of batch2) {
let fwd = net.forwardPropogate(b.input);
errorSum += numeric.norm2(
numeric.subVV(fwd.activations[fwd.activations.length - 1], b.output)
);
}
errorSum = errorSum / batch2.length;
iters.push(n);
errors.push(errorSum);
console.log({ errorSum });
setTimeout(iterate, 1, n + 1);
}
iterate();
let can = net.draw();
can.id = "flubber";
let div = document.getElementById("flubber");
if (div) document.body.removeChild(div);
document.body.appendChild(can);
console.log("net", net);
}