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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
<html>
|
3 |
+
|
4 |
+
<head>
|
5 |
+
<title>Carbono UI</title>
|
6 |
+
<style>
|
7 |
+
a {
|
8 |
+
color: white;
|
9 |
+
}
|
10 |
+
|
11 |
+
body {
|
12 |
+
background: #000;
|
13 |
+
color: #fff;
|
14 |
+
font-family: monospace;
|
15 |
+
margin: 0;
|
16 |
+
padding: 15px;
|
17 |
+
display: flex;
|
18 |
+
flex-direction: column;
|
19 |
+
gap: 15px;
|
20 |
+
overflow-x: hidden;
|
21 |
+
}
|
22 |
+
|
23 |
+
h3 {
|
24 |
+
margin: 1.5rem;
|
25 |
+
margin-bottom: 0;
|
26 |
+
}
|
27 |
+
|
28 |
+
p {
|
29 |
+
margin: 1.5rem;
|
30 |
+
margin-top: 0rem;
|
31 |
+
color: #777;
|
32 |
+
}
|
33 |
+
|
34 |
+
.grid {
|
35 |
+
display: grid;
|
36 |
+
grid-template-columns: minmax(400px, 1fr) minmax(300px, 2fr);
|
37 |
+
gap: 15px;
|
38 |
+
opacity: 0;
|
39 |
+
transform: translateY(20px);
|
40 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
41 |
+
}
|
42 |
+
|
43 |
+
.widget {
|
44 |
+
background: #000;
|
45 |
+
border-radius: 10px;
|
46 |
+
padding: 15px;
|
47 |
+
box-sizing: border-box;
|
48 |
+
width: 100%;
|
49 |
+
opacity: 0;
|
50 |
+
transform: translateY(20px);
|
51 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
52 |
+
animation-delay: 0.2s;
|
53 |
+
}
|
54 |
+
|
55 |
+
.widget-title {
|
56 |
+
font-size: 1.1em;
|
57 |
+
margin-bottom: 12px;
|
58 |
+
border-bottom: 1px solid #333;
|
59 |
+
padding-bottom: 8px;
|
60 |
+
opacity: 0;
|
61 |
+
transform: translateY(10px);
|
62 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
63 |
+
animation-delay: 0.3s;
|
64 |
+
}
|
65 |
+
|
66 |
+
.input-group {
|
67 |
+
margin-bottom: 12px;
|
68 |
+
opacity: 0;
|
69 |
+
transform: translateY(10px);
|
70 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
71 |
+
animation-delay: 0.4s;
|
72 |
+
}
|
73 |
+
|
74 |
+
.settings-grid {
|
75 |
+
display: grid;
|
76 |
+
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
77 |
+
gap: 10px;
|
78 |
+
margin-bottom: 12px;
|
79 |
+
opacity: 0;
|
80 |
+
transform: translateY(10px);
|
81 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
82 |
+
animation-delay: 0.5s;
|
83 |
+
}
|
84 |
+
|
85 |
+
input[type="text"],
|
86 |
+
input[type="number"],
|
87 |
+
select,
|
88 |
+
textarea {
|
89 |
+
outline: none;
|
90 |
+
width: 100%;
|
91 |
+
padding: 6px;
|
92 |
+
background: #222;
|
93 |
+
border: 1px solid #444;
|
94 |
+
color: #fff;
|
95 |
+
border-radius: 8px;
|
96 |
+
margin-top: 4px;
|
97 |
+
box-sizing: border-box;
|
98 |
+
transition: background 0.3s, border 0.3s;
|
99 |
+
}
|
100 |
+
|
101 |
+
span {
|
102 |
+
background-color: white;
|
103 |
+
color: black;
|
104 |
+
font-weight: 600;
|
105 |
+
font-size: 12px;
|
106 |
+
padding: 1px;
|
107 |
+
border-radius: 3px;
|
108 |
+
cursor: pointer;
|
109 |
+
}
|
110 |
+
|
111 |
+
input[type="text"]:focus,
|
112 |
+
input[type="number"]:focus,
|
113 |
+
select:focus,
|
114 |
+
textarea:focus {
|
115 |
+
background: #333;
|
116 |
+
border: 1px solid #666;
|
117 |
+
}
|
118 |
+
|
119 |
+
button {
|
120 |
+
background: #fff;
|
121 |
+
color: #000;
|
122 |
+
border: none;
|
123 |
+
padding: 6px 12px;
|
124 |
+
border-radius: 6px;
|
125 |
+
cursor: pointer;
|
126 |
+
transition: all 0.1s ease;
|
127 |
+
border: 1px solid white;
|
128 |
+
opacity: 0;
|
129 |
+
height: 28px;
|
130 |
+
transform: translateY(10px);
|
131 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
132 |
+
animation-delay: 0.6s;
|
133 |
+
}
|
134 |
+
|
135 |
+
button:hover {
|
136 |
+
border: 1px solid white;
|
137 |
+
color: white;
|
138 |
+
background: #000;
|
139 |
+
}
|
140 |
+
|
141 |
+
.progress-container {
|
142 |
+
height: 180px;
|
143 |
+
position: relative;
|
144 |
+
border: 1px solid #333;
|
145 |
+
border-radius: 8px;
|
146 |
+
margin-bottom: 10px;
|
147 |
+
opacity: 0;
|
148 |
+
transform: translateY(10px);
|
149 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
150 |
+
animation-delay: 0.7s;
|
151 |
+
}
|
152 |
+
|
153 |
+
.loss-graph {
|
154 |
+
position: absolute;
|
155 |
+
bottom: 0;
|
156 |
+
width: 100%;
|
157 |
+
height: 100%;
|
158 |
+
}
|
159 |
+
|
160 |
+
.network-graph {
|
161 |
+
position: absolute;
|
162 |
+
bottom: 0;
|
163 |
+
width: 100%;
|
164 |
+
height: 100%;
|
165 |
+
}
|
166 |
+
|
167 |
+
.flex-container {
|
168 |
+
display: flex;
|
169 |
+
gap: 20px;
|
170 |
+
opacity: 0;
|
171 |
+
transform: translateY(10px);
|
172 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
173 |
+
animation-delay: 0.8s;
|
174 |
+
}
|
175 |
+
|
176 |
+
.prediction-section,
|
177 |
+
.model-section {
|
178 |
+
flex: 1;
|
179 |
+
}
|
180 |
+
|
181 |
+
.button-group {
|
182 |
+
display: flex;
|
183 |
+
gap: 10px;
|
184 |
+
opacity: 0;
|
185 |
+
transform: translateY(10px);
|
186 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
187 |
+
animation-delay: 0.9s;
|
188 |
+
}
|
189 |
+
|
190 |
+
.visualization-container {
|
191 |
+
margin-top: 15px;
|
192 |
+
opacity: 0;
|
193 |
+
transform: translateY(10px);
|
194 |
+
animation: fadeInUp 0.5s ease-out forwards;
|
195 |
+
animation-delay: 1s;
|
196 |
+
}
|
197 |
+
|
198 |
+
.epoch-progress {
|
199 |
+
height: 5px;
|
200 |
+
background: #222;
|
201 |
+
border-radius: 8px;
|
202 |
+
overflow: hidden;
|
203 |
+
}
|
204 |
+
|
205 |
+
.epoch-bar {
|
206 |
+
height: 100%;
|
207 |
+
width: 0;
|
208 |
+
background: #fff;
|
209 |
+
transition: width 0.3s ease;
|
210 |
+
}
|
211 |
+
|
212 |
+
@keyframes fadeInUp {
|
213 |
+
to {
|
214 |
+
opacity: 1;
|
215 |
+
transform: translateY(0);
|
216 |
+
}
|
217 |
+
}
|
218 |
+
|
219 |
+
/* Responsive Design */
|
220 |
+
@media (max-width: 768px) {
|
221 |
+
.grid {
|
222 |
+
grid-template-columns: 1fr;
|
223 |
+
}
|
224 |
+
|
225 |
+
.flex-container {
|
226 |
+
flex-direction: column;
|
227 |
+
}
|
228 |
+
}
|
229 |
+
</style>
|
230 |
+
</head>
|
231 |
+
|
232 |
+
<body>
|
233 |
+
<h3>playground</h3>
|
234 |
+
<p>this is a web app for showcasing carbono, a self-contained micro-library that makes it super easy to play, create and share small neural networks; it's the easiest, hackable machine learning js library; it's also convenient to quickly prototype on embedded devices. to download it and know more you can go to the <a href="https://github.com/appvoid/carbono" target="_blank">github repo</a>; you can see additional training details by opening the console; to load a dummy dataset, <span id="loadDataBtn">click here</span> and then click "train" button.</p>
|
235 |
+
<div class="grid">
|
236 |
+
<!-- Group 1: Data & Training -->
|
237 |
+
<div class="widget">
|
238 |
+
<div class="widget-title">model settings</div>
|
239 |
+
|
240 |
+
<div class="input-group">
|
241 |
+
<label>training set:</label>
|
242 |
+
<textarea id="trainingData" rows="3" placeholder="1,1,1,0
|
243 |
+
1,0,1,0
|
244 |
+
0,1,0,1"></textarea>
|
245 |
+
</div>
|
246 |
+
<p>last number represents actual desired output</p>
|
247 |
+
<div class="input-group">
|
248 |
+
<label>validation set:</label>
|
249 |
+
<textarea id="testData" rows="3" placeholder="0,0,0,1"></textarea>
|
250 |
+
</div>
|
251 |
+
|
252 |
+
<div class="settings-grid">
|
253 |
+
<div class="input-group">
|
254 |
+
<label>epochs:</label>
|
255 |
+
<input type="number" id="epochs" value="50">
|
256 |
+
</div>
|
257 |
+
<div class="input-group">
|
258 |
+
<label>learning rate:</label>
|
259 |
+
<input type="number" id="learningRate" value="0.1" step="0.001">
|
260 |
+
</div>
|
261 |
+
<div class="input-group">
|
262 |
+
<label>batch size:</label>
|
263 |
+
<input type="number" id="batchSize" value="8">
|
264 |
+
</div>
|
265 |
+
<div class="input-group">
|
266 |
+
<label>hidden layers:</label>
|
267 |
+
<input type="number" id="numHiddenLayers" value="1">
|
268 |
+
</div>
|
269 |
+
</div>
|
270 |
+
|
271 |
+
<!-- New UI Elements for Layer Configuration -->
|
272 |
+
|
273 |
+
<div id="hiddenLayersConfig"></div>
|
274 |
+
</div>
|
275 |
+
|
276 |
+
<!-- Group 2: Progress & Visualization -->
|
277 |
+
<div class="widget">
|
278 |
+
<div class="widget-title">training progress</div>
|
279 |
+
<div id="progress">
|
280 |
+
<div class="progress-container">
|
281 |
+
<canvas id="lossGraph" class="loss-graph"></canvas>
|
282 |
+
</div>
|
283 |
+
<p>training loss is white, validation loss is gray</p>
|
284 |
+
<div class="epoch-progress">
|
285 |
+
<div id="epochBar" class="epoch-bar"></div>
|
286 |
+
</div>
|
287 |
+
<div id="stats" style="margin-top: 10px;"></div>
|
288 |
+
</div>
|
289 |
+
<div class="model-section">
|
290 |
+
<br>
|
291 |
+
<div class="widget-title">model management</div>
|
292 |
+
<p>save the weights to load them on your app or share them on huggingface!</p>
|
293 |
+
<div class="button-group">
|
294 |
+
<button id="trainButton">train</button>
|
295 |
+
<button id="saveButton">save</button>
|
296 |
+
<button id="loadButton">load</button>
|
297 |
+
<div class="prediction-section">
|
298 |
+
<div class="widget-title">prediction</div>
|
299 |
+
<p>predict output</p>
|
300 |
+
<div class="input-group">
|
301 |
+
<label>input:</label>
|
302 |
+
<input type="text" id="predictionInput" placeholder="0.4, 0.2, 0.6">
|
303 |
+
</div>
|
304 |
+
<button id="predictButton">predict</button>
|
305 |
+
<div id="predictionResult" style="margin-top: 10px;"></div>
|
306 |
+
</div>
|
307 |
+
<div class="visualization-container">
|
308 |
+
<div class="widget-title">visualization</div>
|
309 |
+
<div class="progress-container">
|
310 |
+
<canvas id="networkGraph" class="network-graph"></canvas>
|
311 |
+
</div>
|
312 |
+
<p>internal model's representation</p>
|
313 |
+
</div>
|
314 |
+
</div>
|
315 |
+
</div>
|
316 |
+
</div>
|
317 |
+
</div>
|
318 |
+
|
319 |
+
<script>
|
320 |
+
// 🧠 carbono: A Fun and Friendly Neural Network Class 🧠
|
321 |
+
// This micro-library wraps everything you need to have
|
322 |
+
// This is the simplest yet functional feedforward mlp in js
|
323 |
+
class carbono {
|
324 |
+
constructor(debug = true) {
|
325 |
+
this.layers = []; // 📚 Stores info about each layer
|
326 |
+
this.weights = []; // ⚖️ Stores weights for each layer
|
327 |
+
this.biases = []; // 🔧 Stores biases for each layer
|
328 |
+
this.activations = []; // 🚀 Stores activation functions for each layer
|
329 |
+
this.details = {}; // 📊 Stores details about the model
|
330 |
+
this.debug = debug; // 🐛 Enables or disables debug messages
|
331 |
+
}
|
332 |
+
// 🏗️ Add a new layer to the neural network
|
333 |
+
layer(inputSize, outputSize, activation = 'tanh') {
|
334 |
+
// 🧱 Store layer information
|
335 |
+
this.layers.push({
|
336 |
+
inputSize,
|
337 |
+
outputSize,
|
338 |
+
activation
|
339 |
+
});
|
340 |
+
// 🔍 Check if the new layer's input size matches the previous layer's output size
|
341 |
+
if (this.weights.length > 0) {
|
342 |
+
const lastLayerOutputSize = this.layers[this.layers.length - 2].outputSize;
|
343 |
+
if (inputSize !== lastLayerOutputSize) {
|
344 |
+
throw new Error('Oops! The input size of the new layer must match the output size of the previous layer.');
|
345 |
+
}
|
346 |
+
}
|
347 |
+
// 🎲 Initialize weights using Xavier/Glorot initialization
|
348 |
+
const weights = [];
|
349 |
+
for (let i = 0; i < outputSize; i++) {
|
350 |
+
const row = [];
|
351 |
+
for (let j = 0; j < inputSize; j++) {
|
352 |
+
row.push((Math.random() - 0.5) * 2 * Math.sqrt(6 / (inputSize + outputSize)));
|
353 |
+
}
|
354 |
+
weights.push(row);
|
355 |
+
}
|
356 |
+
this.weights.push(weights);
|
357 |
+
// 🎚️ Initialize biases with small positive values
|
358 |
+
const biases = Array(outputSize).fill(0.01);
|
359 |
+
this.biases.push(biases);
|
360 |
+
// 🚀 Store the activation function for this layer
|
361 |
+
this.activations.push(activation);
|
362 |
+
}
|
363 |
+
// 🧮 Apply the activation function
|
364 |
+
activationFunction(x, activation) {
|
365 |
+
switch (activation) {
|
366 |
+
case 'tanh':
|
367 |
+
return Math.tanh(x); // 〰️ Hyperbolic tangent
|
368 |
+
case 'sigmoid':
|
369 |
+
return 1 / (1 + Math.exp(-x)); // 📈 S-shaped curve
|
370 |
+
case 'relu':
|
371 |
+
return Math.max(0, x); // 📐 Rectified Linear Unit
|
372 |
+
case 'selu':
|
373 |
+
const alpha = 1.67326;
|
374 |
+
const scale = 1.0507;
|
375 |
+
return x > 0 ? scale * x : scale * alpha * (Math.exp(x) - 1); // 🚀 Scaled Exponential Linear Unit
|
376 |
+
default:
|
377 |
+
throw new Error('Whoops! We don\'t know that activation function.');
|
378 |
+
}
|
379 |
+
}
|
380 |
+
// 📐 Calculate the derivative of the activation function
|
381 |
+
activationDerivative(x, activation) {
|
382 |
+
switch (activation) {
|
383 |
+
case 'tanh':
|
384 |
+
return 1 - Math.pow(Math.tanh(x), 2);
|
385 |
+
case 'sigmoid':
|
386 |
+
const sigmoid = 1 / (1 + Math.exp(-x));
|
387 |
+
return sigmoid * (1 - sigmoid);
|
388 |
+
case 'relu':
|
389 |
+
return x > 0 ? 1 : 0;
|
390 |
+
case 'selu':
|
391 |
+
const alpha = 1.67326;
|
392 |
+
const scale = 1.0507;
|
393 |
+
return x > 0 ? scale : scale * alpha * Math.exp(x);
|
394 |
+
default:
|
395 |
+
throw new Error('Oops! We don\'t know the derivative of that activation function.');
|
396 |
+
}
|
397 |
+
}
|
398 |
+
// 🏋️♀️ Train the neural network
|
399 |
+
async train(trainSet, options = {}) {
|
400 |
+
// 🎛️ Set up training options with default values
|
401 |
+
const {
|
402 |
+
epochs = 200, // 🔄 Number of times to go through the entire dataset
|
403 |
+
learningRate = 0.212, // 📏 How big of steps to take when adjusting weights
|
404 |
+
batchSize = 16, // 📦 Number of samples to process before updating weights
|
405 |
+
printEveryEpochs = 100, // 🖨️ How often to print progress
|
406 |
+
earlyStopThreshold = 1e-6, // 🛑 When to stop if the error is small enough
|
407 |
+
testSet = null, // 🧪 Optional test set for evaluation
|
408 |
+
callback = null // 📡 Callback function for real-time updates
|
409 |
+
} = options;
|
410 |
+
const start = Date.now(); // ⏱️ Start the timer
|
411 |
+
// 🛡️ Make sure batch size is at least 2
|
412 |
+
if (batchSize < 1) batchSize = 2;
|
413 |
+
// 🏗️ Automatically create layers if none exist
|
414 |
+
if (this.layers.length === 0) {
|
415 |
+
const numInputs = trainSet[0].input.length;
|
416 |
+
this.layer(numInputs, numInputs, 'tanh');
|
417 |
+
this.layer(numInputs, 1, 'tanh');
|
418 |
+
}
|
419 |
+
let lastTrainLoss = 0;
|
420 |
+
let lastTestLoss = null;
|
421 |
+
// 🔄 Main training loop
|
422 |
+
for (let epoch = 0; epoch < epochs; epoch++) {
|
423 |
+
let trainError = 0;
|
424 |
+
// 📦 Process data in batches
|
425 |
+
for (let b = 0; b < trainSet.length; b += batchSize) {
|
426 |
+
const batch = trainSet.slice(b, b + batchSize);
|
427 |
+
let batchError = 0;
|
428 |
+
// 🧠 Forward pass and backward pass for each item in the batch
|
429 |
+
for (const data of batch) {
|
430 |
+
// 🏃♂️ Forward pass
|
431 |
+
const layerInputs = [data.input];
|
432 |
+
for (let i = 0; i < this.weights.length; i++) {
|
433 |
+
const inputs = layerInputs[i];
|
434 |
+
const weights = this.weights[i];
|
435 |
+
const biases = this.biases[i];
|
436 |
+
const activation = this.activations[i];
|
437 |
+
const outputs = [];
|
438 |
+
for (let j = 0; j < weights.length; j++) {
|
439 |
+
const weight = weights[j];
|
440 |
+
let sum = biases[j];
|
441 |
+
for (let k = 0; k < inputs.length; k++) {
|
442 |
+
sum += inputs[k] * weight[k];
|
443 |
+
}
|
444 |
+
outputs.push(this.activationFunction(sum, activation));
|
445 |
+
}
|
446 |
+
layerInputs.push(outputs);
|
447 |
+
}
|
448 |
+
// 🔙 Backward pass
|
449 |
+
const outputLayerIndex = this.weights.length - 1;
|
450 |
+
const outputLayerInputs = layerInputs[layerInputs.length - 1];
|
451 |
+
const outputErrors = [];
|
452 |
+
for (let i = 0; i < outputLayerInputs.length; i++) {
|
453 |
+
const error = data.output[i] - outputLayerInputs[i];
|
454 |
+
outputErrors.push(error);
|
455 |
+
}
|
456 |
+
let layerErrors = [outputErrors];
|
457 |
+
for (let i = this.weights.length - 2; i >= 0; i--) {
|
458 |
+
const nextLayerWeights = this.weights[i + 1];
|
459 |
+
const nextLayerErrors = layerErrors[0];
|
460 |
+
const currentLayerInputs = layerInputs[i + 1];
|
461 |
+
const currentActivation = this.activations[i];
|
462 |
+
const errors = [];
|
463 |
+
for (let j = 0; j < this.layers[i].outputSize; j++) {
|
464 |
+
let error = 0;
|
465 |
+
for (let k = 0; k < this.layers[i + 1].outputSize; k++) {
|
466 |
+
error += nextLayerErrors[k] * nextLayerWeights[k][j];
|
467 |
+
}
|
468 |
+
errors.push(error * this.activationDerivative(currentLayerInputs[j], currentActivation));
|
469 |
+
}
|
470 |
+
layerErrors.unshift(errors);
|
471 |
+
}
|
472 |
+
// 🔧 Update weights and biases
|
473 |
+
for (let i = 0; i < this.weights.length; i++) {
|
474 |
+
const inputs = layerInputs[i];
|
475 |
+
const errors = layerErrors[i];
|
476 |
+
const weights = this.weights[i];
|
477 |
+
const biases = this.biases[i];
|
478 |
+
for (let j = 0; j < weights.length; j++) {
|
479 |
+
const weight = weights[j];
|
480 |
+
for (let k = 0; k < inputs.length; k++) {
|
481 |
+
weight[k] += learningRate * errors[j] * inputs[k];
|
482 |
+
}
|
483 |
+
biases[j] += learningRate * errors[j];
|
484 |
+
}
|
485 |
+
}
|
486 |
+
batchError += Math.abs(outputErrors[0]); // Assuming binary output
|
487 |
+
}
|
488 |
+
trainError += batchError;
|
489 |
+
}
|
490 |
+
lastTrainLoss = trainError / trainSet.length;
|
491 |
+
// 🧪 Evaluate on test set if provided
|
492 |
+
if (testSet) {
|
493 |
+
let testError = 0;
|
494 |
+
for (const data of testSet) {
|
495 |
+
const prediction = this.predict(data.input);
|
496 |
+
testError += Math.abs(data.output[0] - prediction[0]);
|
497 |
+
}
|
498 |
+
lastTestLoss = testError / testSet.length;
|
499 |
+
}
|
500 |
+
// 📢 Print progress if needed
|
501 |
+
if ((epoch + 1) % printEveryEpochs === 0 && this.debug === true) {
|
502 |
+
console.log(`Epoch ${epoch + 1}, Train Loss: ${lastTrainLoss.toFixed(6)}${testSet ? `, Test Loss: ${lastTestLoss.toFixed(6)}` : ''}`);
|
503 |
+
}
|
504 |
+
// 📡 Call the callback function with current progress
|
505 |
+
if (callback) {
|
506 |
+
await callback(epoch + 1, lastTrainLoss, lastTestLoss);
|
507 |
+
}
|
508 |
+
// Add a small delay to prevent UI freezing
|
509 |
+
await new Promise(resolve => setTimeout(resolve, 0));
|
510 |
+
// 🛑 Check for early stopping
|
511 |
+
if (lastTrainLoss < earlyStopThreshold) {
|
512 |
+
console.log(`We stopped at epoch ${epoch + 1} with train loss: ${lastTrainLoss.toFixed(6)}${testSet ? ` and test loss: ${lastTestLoss.toFixed(6)}` : ''}`);
|
513 |
+
break;
|
514 |
+
}
|
515 |
+
}
|
516 |
+
const end = Date.now(); // ⏱️ Stop the timer
|
517 |
+
// 🧮 Calculate total number of parameters
|
518 |
+
let totalParams = 0;
|
519 |
+
for (let i = 0; i < this.weights.length; i++) {
|
520 |
+
const weightLayer = this.weights[i];
|
521 |
+
const biasLayer = this.biases[i];
|
522 |
+
totalParams += weightLayer.flat().length + biasLayer.length;
|
523 |
+
}
|
524 |
+
// 📊 Create a summary of the training
|
525 |
+
const trainingSummary = {
|
526 |
+
trainLoss: lastTrainLoss,
|
527 |
+
testLoss: lastTestLoss,
|
528 |
+
parameters: totalParams,
|
529 |
+
training: {
|
530 |
+
time: end - start,
|
531 |
+
epochs,
|
532 |
+
learningRate,
|
533 |
+
batchSize
|
534 |
+
},
|
535 |
+
layers: this.layers.map(layer => ({
|
536 |
+
inputSize: layer.inputSize,
|
537 |
+
outputSize: layer.outputSize,
|
538 |
+
activation: layer.activation
|
539 |
+
}))
|
540 |
+
};
|
541 |
+
this.details = trainingSummary;
|
542 |
+
return trainingSummary;
|
543 |
+
}
|
544 |
+
// 🔮 Use the trained network to make predictions
|
545 |
+
predict(input) {
|
546 |
+
let layerInput = input;
|
547 |
+
const allActivations = [input]; // Track all activations through layers
|
548 |
+
const allRawValues = []; // Track pre-activation values
|
549 |
+
for (let i = 0; i < this.weights.length; i++) {
|
550 |
+
const weights = this.weights[i];
|
551 |
+
const biases = this.biases[i];
|
552 |
+
const activation = this.activations[i];
|
553 |
+
const layerOutput = [];
|
554 |
+
const rawValues = [];
|
555 |
+
for (let j = 0; j < weights.length; j++) {
|
556 |
+
const weight = weights[j];
|
557 |
+
let sum = biases[j];
|
558 |
+
for (let k = 0; k < layerInput.length; k++) {
|
559 |
+
sum += layerInput[k] * weight[k];
|
560 |
+
}
|
561 |
+
rawValues.push(sum);
|
562 |
+
layerOutput.push(this.activationFunction(sum, activation));
|
563 |
+
}
|
564 |
+
allRawValues.push(rawValues);
|
565 |
+
allActivations.push(layerOutput);
|
566 |
+
layerInput = layerOutput;
|
567 |
+
}
|
568 |
+
// Store last activation values for visualization
|
569 |
+
this.lastActivations = allActivations;
|
570 |
+
this.lastRawValues = allRawValues;
|
571 |
+
return layerInput;
|
572 |
+
}
|
573 |
+
// 💾 Save the model to a file
|
574 |
+
save(name = 'model') {
|
575 |
+
const data = {
|
576 |
+
weights: this.weights,
|
577 |
+
biases: this.biases,
|
578 |
+
activations: this.activations,
|
579 |
+
layers: this.layers,
|
580 |
+
details: this.details
|
581 |
+
};
|
582 |
+
const blob = new Blob([JSON.stringify(data)], {
|
583 |
+
type: 'application/json'
|
584 |
+
});
|
585 |
+
const url = URL.createObjectURL(blob);
|
586 |
+
const a = document.createElement('a');
|
587 |
+
a.href = url;
|
588 |
+
a.download = `${name}.json`;
|
589 |
+
a.click();
|
590 |
+
URL.revokeObjectURL(url);
|
591 |
+
}
|
592 |
+
// 📂 Load a saved model from a file
|
593 |
+
load(callback) {
|
594 |
+
const handleListener = (event) => {
|
595 |
+
const file = event.target.files[0];
|
596 |
+
if (!file) return;
|
597 |
+
const reader = new FileReader();
|
598 |
+
reader.onload = (event) => {
|
599 |
+
const text = event.target.result;
|
600 |
+
try {
|
601 |
+
const data = JSON.parse(text);
|
602 |
+
this.weights = data.weights;
|
603 |
+
this.biases = data.biases;
|
604 |
+
this.activations = data.activations;
|
605 |
+
this.layers = data.layers;
|
606 |
+
this.details = data.details;
|
607 |
+
callback();
|
608 |
+
if (this.debug === true) console.log('Model loaded successfully!');
|
609 |
+
input.removeEventListener('change', handleListener);
|
610 |
+
input.remove();
|
611 |
+
} catch (e) {
|
612 |
+
input.removeEventListener('change', handleListener);
|
613 |
+
input.remove();
|
614 |
+
if (this.debug === true) console.error('Failed to load model:', e);
|
615 |
+
}
|
616 |
+
};
|
617 |
+
reader.readAsText(file);
|
618 |
+
};
|
619 |
+
const input = document.createElement('input');
|
620 |
+
input.type = 'file';
|
621 |
+
input.accept = '.json';
|
622 |
+
input.style.opacity = '0';
|
623 |
+
document.body.append(input);
|
624 |
+
input.addEventListener('change', handleListener.bind(this));
|
625 |
+
input.click();
|
626 |
+
}
|
627 |
+
}
|
628 |
+
document.getElementById("loadDataBtn").onclick = () => {
|
629 |
+
document.getElementById('trainingData').value = `1.0, 0.0, 0.0, 0.0
|
630 |
+
0.7, 0.7, 0.8, 1
|
631 |
+
0.0, 1.0, 0.0, 0.5`
|
632 |
+
document.getElementById('testData').value = `0.4, 0.2, 0.6, 1.0
|
633 |
+
0.2, 0.82, 0.83, 1.0`
|
634 |
+
}
|
635 |
+
// Interface code
|
636 |
+
const nn = new carbono();
|
637 |
+
let lossHistory = [];
|
638 |
+
const ctx = document.getElementById('lossGraph').getContext('2d');
|
639 |
+
|
640 |
+
function parseCSV(csv) {
|
641 |
+
return csv.trim().split('\n').map(row => {
|
642 |
+
const values = row.split(',').map(Number);
|
643 |
+
return {
|
644 |
+
input: values.slice(0, -1),
|
645 |
+
output: [values[values.length - 1]]
|
646 |
+
};
|
647 |
+
});
|
648 |
+
}
|
649 |
+
|
650 |
+
function drawLossGraph() {
|
651 |
+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
|
652 |
+
const width = ctx.canvas.width;
|
653 |
+
const height = ctx.canvas.height;
|
654 |
+
// Combine train and test losses to find overall max for scaling
|
655 |
+
const maxLoss = Math.max(
|
656 |
+
...lossHistory.map(loss => Math.max(loss.train, loss.test || 0))
|
657 |
+
);
|
658 |
+
// Draw training loss (white line)
|
659 |
+
ctx.strokeStyle = '#fff';
|
660 |
+
ctx.beginPath();
|
661 |
+
lossHistory.forEach((loss, i) => {
|
662 |
+
const x = (i / (lossHistory.length - 1)) * width;
|
663 |
+
const y = height - (loss.train / maxLoss) * height;
|
664 |
+
if (i === 0) ctx.moveTo(x, y);
|
665 |
+
else ctx.lineTo(x, y);
|
666 |
+
});
|
667 |
+
ctx.stroke();
|
668 |
+
// Draw test loss (gray line)
|
669 |
+
ctx.strokeStyle = '#777';
|
670 |
+
ctx.beginPath();
|
671 |
+
lossHistory.forEach((loss, i) => {
|
672 |
+
if (loss.test !== undefined) {
|
673 |
+
const x = (i / (lossHistory.length - 1)) * width;
|
674 |
+
const y = height - (loss.test / maxLoss) * height;
|
675 |
+
if (i === 0 || lossHistory[i - 1].test === undefined) ctx.moveTo(x, y);
|
676 |
+
else ctx.lineTo(x, y);
|
677 |
+
}
|
678 |
+
});
|
679 |
+
ctx.stroke();
|
680 |
+
}
|
681 |
+
|
682 |
+
function createLayerConfigUI(numLayers) {
|
683 |
+
const container = document.getElementById('hiddenLayersConfig');
|
684 |
+
container.innerHTML = ''; // Clear previous UI
|
685 |
+
for (let i = 0; i < numLayers; i++) {
|
686 |
+
const group = document.createElement('div');
|
687 |
+
group.className = 'input-group';
|
688 |
+
const label = document.createElement('label');
|
689 |
+
label.textContent = `layer ${i + 1} nodes:`;
|
690 |
+
const input = document.createElement('input');
|
691 |
+
input.type = 'number';
|
692 |
+
input.value = 5;
|
693 |
+
input.dataset.layerIndex = i;
|
694 |
+
const activationLabel = document.createElement('label');
|
695 |
+
activationLabel.innerHTML = `<br>activation:`;
|
696 |
+
const activationSelect = document.createElement('select');
|
697 |
+
const activations = ['tanh', 'sigmoid', 'relu', 'selu'];
|
698 |
+
activations.forEach(act => {
|
699 |
+
const option = document.createElement('option');
|
700 |
+
option.value = act;
|
701 |
+
option.textContent = act;
|
702 |
+
activationSelect.appendChild(option);
|
703 |
+
});
|
704 |
+
activationSelect.dataset.layerIndex = i;
|
705 |
+
group.appendChild(label);
|
706 |
+
group.appendChild(input);
|
707 |
+
group.appendChild(activationLabel);
|
708 |
+
group.appendChild(activationSelect);
|
709 |
+
container.appendChild(group);
|
710 |
+
}
|
711 |
+
}
|
712 |
+
document.getElementById('numHiddenLayers').addEventListener('change', (event) => {
|
713 |
+
const numLayers = parseInt(event.target.value);
|
714 |
+
createLayerConfigUI(numLayers);
|
715 |
+
});
|
716 |
+
createLayerConfigUI(document.getElementById('numHiddenLayers').value);
|
717 |
+
document.getElementById('trainButton').addEventListener('click', async () => {
|
718 |
+
lossHistory = []; // Initialize as empty array
|
719 |
+
const trainingData = parseCSV(document.getElementById('trainingData').value);
|
720 |
+
const testData = parseCSV(document.getElementById('testData').value);
|
721 |
+
lossHistory = [];
|
722 |
+
document.getElementById('stats').innerHTML = '';
|
723 |
+
const numHiddenLayers = parseInt(document.getElementById('numHiddenLayers').value);
|
724 |
+
const layerConfigs = [];
|
725 |
+
for (let i = 0; i < numHiddenLayers; i++) {
|
726 |
+
const sizeInput = document.querySelector(`input[data-layer-index="${i}"]`);
|
727 |
+
const activationSelect = document.querySelector(`select[data-layer-index="${i}"]`);
|
728 |
+
layerConfigs.push({
|
729 |
+
size: parseInt(sizeInput.value),
|
730 |
+
activation: activationSelect.value
|
731 |
+
});
|
732 |
+
}
|
733 |
+
nn.layers = []; // Reset layers
|
734 |
+
nn.weights = [];
|
735 |
+
nn.biases = [];
|
736 |
+
nn.activations = [];
|
737 |
+
const numInputs = trainingData[0].input.length;
|
738 |
+
nn.layer(numInputs, layerConfigs[0].size, layerConfigs[0].activation);
|
739 |
+
for (let i = 1; i < layerConfigs.length; i++) {
|
740 |
+
nn.layer(layerConfigs[i - 1].size, layerConfigs[i].size, layerConfigs[i].activation);
|
741 |
+
}
|
742 |
+
nn.layer(layerConfigs[layerConfigs.length - 1].size, 1, 'tanh'); // Output layer
|
743 |
+
const options = {
|
744 |
+
epochs: parseInt(document.getElementById('epochs').value),
|
745 |
+
learningRate: parseFloat(document.getElementById('learningRate').value),
|
746 |
+
batchSize: parseInt(document.getElementById('batchSize').value),
|
747 |
+
printEveryEpochs: 1,
|
748 |
+
testSet: testData.length > 0 ? testData : null,
|
749 |
+
callback: async (epoch, trainLoss, testLoss) => {
|
750 |
+
lossHistory.push({
|
751 |
+
train: trainLoss,
|
752 |
+
test: testLoss
|
753 |
+
});
|
754 |
+
drawLossGraph();
|
755 |
+
document.getElementById('epochBar').style.width =
|
756 |
+
`${(epoch / options.epochs) * 100}%`;
|
757 |
+
document.getElementById('stats').innerHTML =
|
758 |
+
`<p>• current epoch: ${epoch}/${options.epochs}` +
|
759 |
+
`<br> • train/val loss: ${trainLoss.toFixed(6)}` +
|
760 |
+
(testLoss ? ` | ${testLoss.toFixed(6)}</p>` : '');
|
761 |
+
}
|
762 |
+
}
|
763 |
+
try {
|
764 |
+
const trainButton = document.getElementById('trainButton');
|
765 |
+
trainButton.disabled = true;
|
766 |
+
trainButton.textContent = 'training...';
|
767 |
+
const summary = await nn.train(trainingData, options);
|
768 |
+
trainButton.disabled = false;
|
769 |
+
trainButton.textContent = 'train';
|
770 |
+
// Display final summary
|
771 |
+
document.getElementById('stats').innerHTML += '<strong>Model trained</strong>';
|
772 |
+
} catch (error) {
|
773 |
+
console.error('Training error:', error);
|
774 |
+
document.getElementById('trainButton').disabled = false;
|
775 |
+
document.getElementById('trainButton').textContent = 'train';
|
776 |
+
}
|
777 |
+
});
|
778 |
+
|
779 |
+
function drawNetwork() {
|
780 |
+
const canvas = document.getElementById('networkGraph');
|
781 |
+
const ctx = canvas.getContext('2d');
|
782 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
783 |
+
if (!nn.lastActivations) return; // Don't draw if no predictions made yet
|
784 |
+
const padding = 40;
|
785 |
+
const width = canvas.width - padding * 2;
|
786 |
+
const height = canvas.height - padding * 2;
|
787 |
+
// Calculate node positions
|
788 |
+
const layerPositions = [];
|
789 |
+
// Add input layer explicitly
|
790 |
+
const inputLayer = [];
|
791 |
+
const inputX = padding;
|
792 |
+
const inputSize = nn.layers[0].inputSize;
|
793 |
+
for (let i = 0; i < inputSize; i++) {
|
794 |
+
const inputY = padding + (height * i) / (inputSize - 1);
|
795 |
+
inputLayer.push({
|
796 |
+
x: inputX,
|
797 |
+
y: inputY,
|
798 |
+
value: nn.lastActivations[0][i]
|
799 |
+
});
|
800 |
+
}
|
801 |
+
layerPositions.push(inputLayer);
|
802 |
+
// Add hidden layers
|
803 |
+
for (let i = 1; i < nn.lastActivations.length - 1; i++) {
|
804 |
+
const layer = nn.lastActivations[i];
|
805 |
+
const layerNodes = [];
|
806 |
+
const layerX = padding + (width * i) / (nn.lastActivations.length - 1);
|
807 |
+
for (let j = 0; j < layer.length; j++) {
|
808 |
+
const nodeY = padding + (height * j) / (layer.length - 1);
|
809 |
+
layerNodes.push({
|
810 |
+
x: layerX,
|
811 |
+
y: nodeY,
|
812 |
+
value: layer[j]
|
813 |
+
});
|
814 |
+
}
|
815 |
+
layerPositions.push(layerNodes);
|
816 |
+
}
|
817 |
+
// Add output layer explicitly
|
818 |
+
const outputLayer = [];
|
819 |
+
const outputX = canvas.width - padding;
|
820 |
+
const outputY = padding + height / 2; // Center the output node
|
821 |
+
outputLayer.push({
|
822 |
+
x: outputX,
|
823 |
+
y: outputY,
|
824 |
+
value: nn.lastActivations[nn.lastActivations.length - 1][0]
|
825 |
+
});
|
826 |
+
layerPositions.push(outputLayer);
|
827 |
+
// Draw connections
|
828 |
+
ctx.lineWidth = 1;
|
829 |
+
for (let i = 0; i < layerPositions.length - 1; i++) {
|
830 |
+
const currentLayer = layerPositions[i];
|
831 |
+
const nextLayer = layerPositions[i + 1];
|
832 |
+
const weights = nn.weights[i];
|
833 |
+
for (let j = 0; j < currentLayer.length; j++) {
|
834 |
+
const nextLayerSize = nextLayer.length;
|
835 |
+
for (let k = 0; k < nextLayerSize; k++) {
|
836 |
+
const weight = weights[k][j];
|
837 |
+
const signal = Math.abs(currentLayer[j].value * weight);
|
838 |
+
const opacity = Math.min(Math.max(signal, 0.01), 1);
|
839 |
+
ctx.strokeStyle = `rgba(255, 255, 255, ${opacity})`;
|
840 |
+
ctx.beginPath();
|
841 |
+
ctx.moveTo(currentLayer[j].x, currentLayer[j].y);
|
842 |
+
ctx.lineTo(nextLayer[k].x, nextLayer[k].y);
|
843 |
+
ctx.stroke();
|
844 |
+
}
|
845 |
+
}
|
846 |
+
}
|
847 |
+
// Draw nodes
|
848 |
+
for (const layer of layerPositions) {
|
849 |
+
for (const node of layer) {
|
850 |
+
const value = Math.abs(node.value);
|
851 |
+
const radius = 4;
|
852 |
+
// Node fill
|
853 |
+
ctx.fillStyle = `rgba(255, 255, 255, ${Math.min(Math.max(value, 0.2), 1)})`;
|
854 |
+
ctx.beginPath();
|
855 |
+
ctx.arc(node.x, node.y, radius, 0, Math.PI * 2);
|
856 |
+
ctx.fill();
|
857 |
+
// Node border
|
858 |
+
ctx.strokeStyle = 'rgba(255, 255, 255, 1.0)';
|
859 |
+
ctx.lineWidth = 1;
|
860 |
+
ctx.stroke();
|
861 |
+
}
|
862 |
+
}
|
863 |
+
}
|
864 |
+
// Modify the predict button event listener
|
865 |
+
document.getElementById('predictButton').addEventListener('click', () => {
|
866 |
+
const input = document.getElementById('predictionInput').value
|
867 |
+
.split(',').map(Number);
|
868 |
+
const prediction = nn.predict(input);
|
869 |
+
document.getElementById('predictionResult').innerHTML =
|
870 |
+
`Prediction: ${prediction[0].toFixed(6)}`;
|
871 |
+
drawNetwork(); // Draw the network visualization
|
872 |
+
});
|
873 |
+
// Add network canvas resize handling
|
874 |
+
function resizeCanvases() {
|
875 |
+
const lossCanvas = document.getElementById('lossGraph');
|
876 |
+
const networkCanvas = document.getElementById('networkGraph');
|
877 |
+
lossCanvas.width = lossCanvas.parentElement.clientWidth;
|
878 |
+
lossCanvas.height = lossCanvas.parentElement.clientHeight;
|
879 |
+
networkCanvas.width = networkCanvas.parentElement.clientWidth;
|
880 |
+
networkCanvas.height = networkCanvas.parentElement.clientHeight;
|
881 |
+
drawNetwork(); // Redraw network when canvas is resized
|
882 |
+
}
|
883 |
+
window.addEventListener('resize', resizeCanvases);
|
884 |
+
resizeCanvases();
|
885 |
+
// Save button functionality
|
886 |
+
document.getElementById('saveButton').addEventListener('click', () => {
|
887 |
+
nn.save('model');
|
888 |
+
});
|
889 |
+
// Load button functionality
|
890 |
+
document.getElementById('loadButton').addEventListener('click', () => {
|
891 |
+
nn.load(() => {
|
892 |
+
console.log('Model loaded successfully!');
|
893 |
+
// Optionally, you can add a message to the UI indicating that the model has been loaded
|
894 |
+
document.getElementById('stats').innerHTML += '<p><strong>Model loaded successfully!</strong></p>';
|
895 |
+
});
|
896 |
+
});
|
897 |
+
</script>
|
898 |
+
</body>
|
899 |
+
|
900 |
+
</html>
|