-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfourth.html
416 lines (359 loc) · 16.4 KB
/
fourth.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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
<!DOCTYPE html>
<html lang="en">
<head>
<title>[email protected]</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
<script src="https://cdn.plot.ly/plotly-1.2.0.min.js"></script>
<script src="generators.js"></script>
<script src="model.js"></script>
<script type="text/javascript">
var input_dataset = [], result = [];
var data_raw = []; var sma_vec = [];
/*data_raw.push({id:0,price:7.0,timestamp:'2022-11-2 [2:3:0]'},
{id:1,price:6.5,timestamp:'2021-4-25 [22:4:0]'},
{id:2,price:6.5,timestamp:'2021-4-25 [22:2:0]'},
{id:3,price:6.5,timestamp:'2021-4-25 [22:24:0]'},
{id:4,price:6.5,timestamp:'2021-4-25 [22:28:0]'},
{id:5,price:6.5,timestamp:'2021-4-25 [22:26:0]'},
{id:6,price:6.5,timestamp:'2021-4-25 [22:25:0]'},
{id:7,price:6.5,timestamp:'2021-4-25 [22:27:0]'},
{id:8,price:6.8,timestamp:'2021-4-25 [22:9:0]'},
{id:9,price:6.5,timestamp:'2021-4-25 [22:8:0]'},
{id:10,price:6.5,timestamp:'2021-4-25 [22:18:0]'},
{id:11,price:6.5,timestamp:'2021-4-25 [22:38:0]'},
{id:12,price:6.5,timestamp:'2021-4-25 [22:58:0]'})
sma_vec.push({set:'[7.0,6.5]',avg: 2.00},
{set:['6.5','6.5'],avg: 0.80},
{set:['6.5','6.5'],avg:3.00},
{set:['6.5','6.5'],avg:0.40},
{set:['6.5','6.5'],avg:0.60},
{set:['6.5','6.5'],avg:2.00},
{set:['6.5','6.5'],avg:3.40},
{set:['6.5','6.8'],avg:0.20},
{set:['6.8','6.5'],avg:0.80},
{set:['6.5','6.5'],avg:0.60},
{set:['6.5','6.5'],avg:1.40},
{set:['6.5','6.5'],avg:1.80})*/
function Init() {
initTabs('Dataset'); initDataset();
document.getElementById("n-items").value = "50";
document.getElementById("window-size").value = "12";
document.getElementById('input-data').addEventListener('change', readInputFile, false);
}
function initTabs(tab) {
var navbar = document.getElementsByClassName("nav navbar-nav");
navbar[0].getElementsByTagName("li")[0].className += "active";
document.getElementById("dataset").style.display = "none";
document.getElementById("graph-plot").style.display = "none";
setContentView(tab);
}
function setTabActive(event, tab) {
var navbar = document.getElementsByClassName("nav navbar-nav");
var tabs = navbar[0].getElementsByTagName("li");
for (var index = 0; index < tabs.length; index++)
if (tabs[index].className == "active")
tabs[index].className = "";
if (event.currentTarget != null) {
event.currentTarget.className += "active";
}
var callback = null;
if (tab == "Neural Network") {
callback = function () {
document.getElementById("train_set").innerHTML = getSMATable(1);
}
}
setContentView(tab, callback);
}
function setContentView(tab, callback) {
var tabs_content = document.getElementsByClassName("container");
for (var index = 0; index < tabs_content.length; index++)
tabs_content[index].style.display = "none";
if (document.getElementById(tab).style.display == "none")
document.getElementById(tab).style.display = "block";
if (callback != null) {
callback();
}
}
function readInputFile(e) {
var file = e.target.files[0];
var reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
document.getElementById("input-data").value = "";
parseCSVData(contents);
};
reader.readAsText(file);
}
function parseCSVData(contents) {
data_raw = [];
sma_vec = [];
var rows = contents.split("\n");
var params = rows[0].split(",");
var size = parseInt(params[0].split("=")[1]);
var window_size = parseInt(params[1].split("=")[1]);
document.getElementById("n-items").value = size.toString();
document.getElementById("window-size").value = window_size.toString();
for (var index = 1; index < size + 1; index++) {
var cols = rows[index].split(",");
data_raw.push({ id: cols[0], timestamp: cols[1], price: cols[2] });
}
sma_vec = ComputeSMA(data_raw, window_size);
onInputDataClick();
}
function initDataset() {
var n_items = parseInt(document.getElementById("n-items").value);
var window_size = parseInt(document.getElementById("window-size").value);
data_raw = GenerateDataset(n_items);
//document.write(data_raw[1].value);
sma_vec = ComputeSMA(data_raw, window_size);
onInputDataClick();
}
async function onTrainClick() {
var inputs = sma_vec.map(function(inp_f) {
return inp_f['set'].map(function(val) { return val['price']; })});
var outputs = sma_vec.map(function(outp_f) { return outp_f['avg']; });
var n_epochs = parseInt(document.getElementById("n-epochs").value);
var window_size = parseInt(document.getElementById("window-size").value);
var lr_rate = parseFloat(document.getElementById("learning-rate").value);
var n_hl = parseInt(document.getElementById("hidden-layers").value);
var n_items = parseInt(document.getElementById("n-items-percent").value);
var callback = function(epoch, log) {
var log_nn = document.getElementById("nn_log").innerHTML;
log_nn += "<div>Epoch: " + (epoch + 1) + " Loss: " + log.loss + "</div>";
document.getElementById("nn_log").innerHTML = log_nn;
document.getElementById("training_pg").style.width = ((epoch + 1) * (100 / n_epochs)).toString() + "%";
document.getElementById("training_pg").innerHTML = ((epoch + 1) * (100 / n_epochs)).toString() + "%";
}
result = await trainModel(inputs, outputs,
n_items, window_size, n_epochs, lr_rate, n_hl, callback);
alert('Your model has been successfully trained...');
}
function onPredictClick(view) {
var inputs = sma_vec.map(function(inp_f) {
return inp_f['set'].map(function (val) { return val['price']; }); });
var outputs = sma_vec.map(function(outp_f) { return outp_f['avg']; });
var n_items = parseInt(document.getElementById("n-items-percent").value);
var outps = outputs.slice(Math.floor(n_items / 100 * outputs.length), outputs.length);
var pred_vals = Predict(inputs, n_items, result['model']);
var data_output = "";
for (var index = 0; index < pred_vals.length; index++) {
data_output += "<tr><td>" + (index + 1) + "</td><td>" +
outps[index] + "</td><td>" + pred_vals[index] + "</td></tr>";
}
document.getElementById("pred-res").innerHTML = "<table class=\"table\"><thead><tr><th scope=\"col\">#</th><th scope=\"col\">Real Value</th> \
<th scope=\"col\">Predicted Value</th></thead><tbody>" + data_output + "</tbody></table>";
var window_size = parseInt(document.getElementById("window-size").value);
var timestamps_a = data_raw.map(function (val) { return val['timestamp']; });
var timestamps_b = data_raw.map(function (val) {
return val['timestamp']; }).splice(window_size, data_raw.length);
var timestamps_c = data_raw.map(function (val) {
return val['timestamp']; }).splice(window_size + Math.floor(n_items / 100 * outputs.length), data_raw.length);
var sma = sma_vec.map(function (val) { return val['avg']; });
var prices = data_raw.map(function (val) { return val['price']; });
var graph_plot = document.getElementById('graph-pred');
Plotly.newPlot( graph_plot, [{ x: timestamps_a, y: prices, name: "Series" }], { margin: { t: 0 } } );
Plotly.plot( graph_plot, [{ x: timestamps_b, y: sma, name: "SMA" }], { margin: { t: 0 } } );
Plotly.plot( graph_plot, [{ x: timestamps_c, y: pred_vals, name: "Predicted" }], { margin: { t: 0 } } );
}
function getInputDataTable() {
var data_output = "";
for (var index = 0; index < data_raw.length; index++)
{
data_output += "<tr><td>" + data_raw[index]['id'] + "</td><td>" +
data_raw[index]['timestamp'] + "</td><td>" + data_raw[index]['price'] + "</td></tr>";
}
return "<table class=\"table\"><thead><tr><th scope=\"col\">#</th><th scope=\"col\">Timestamp</th> \
<th scope=\"col\">Feature</th></thead><tbody>" + data_output + "</tbody></table>";
}
function getSMATable(view) {
var data_output = "";
if (view == 0) {
for (var index = 0; index < sma_vec.length; index++)
{
var set_output = "";
var set = sma_vec[index]['set'];
for (var t = 0; t < set.length; t++) {
set_output += "<tr><td width=\"30px\">" + set[t]['price'] +
"</td><td>" + set[t]['timestamp'] + "</td></tr>";
}
data_output += "<tr><td width=\"20px\">" + (index + 1) +
"</td><td>" + "<table width=\"100px\" class=\"table\">" + set_output +
"<tr><td><b>" + "SMA(t) = " + sma_vec[index]['avg'] + "</b></tr></td></table></td></tr>";
}
return "<table class=\"table\"><thead><tr><th scope=\"col\">#</th><th scope=\"col\">Time Series</th>\
</thead><tbody>" + data_output + "</tbody></table>";
}
else if (view == 1) {
var set = sma_vec.map(function (val) { return val['set']; });
for (var index = 0; index < sma_vec.length; index++)
{
data_output += "<tr><td width=\"20px\">" + (index + 1) +
"</td><td>[ " + set[index].map(function (val) {
return (Math.round(val['price'] * 10000) / 10000).toString(); }).toString() +
" ]</td><td>" + sma_vec[index]['avg'] + "</td></tr>";
}
return "<table class=\"table\"><thead><tr><th scope=\"col\">#</th><th scope=\"col\">\
Input</th><th scope=\"col\">Output</th></thead><tbody>" + data_output + "</tbody></table>";
}
}
function onInputDataClick() {
document.getElementById("dataset").style.display = "block";
document.getElementById("graph-plot").style.display = "block";
document.getElementById("data").innerHTML = getInputDataTable();
var timestamps = data_raw.map(function (val) { return val['timestamp']; });
var prices = data_raw.map(function (val) { return val['price']; });
var graph_plot = document.getElementById('graph');
Plotly.newPlot( graph_plot, [{ x: timestamps, y: prices, name: "Stocks Prices" }], { margin: { t: 0 } } );
}
function onSMAClick() {
document.getElementById("data").innerHTML = getSMATable(0);
var sma = sma_vec.map(function (val) { return val['avg']; });
var prices = data_raw.map(function (val) { return val['price']; });
var window_size = parseInt(document.getElementById("window-size").value);
var timestamps_a = data_raw.map(function (val) { return val['timestamp']; });
var timestamps_b = data_raw.map(function (val) {
return val['timestamp']; }).splice(window_size, data_raw.length);
var graph_plot = document.getElementById('graph');
Plotly.newPlot( graph_plot, [{ x: timestamps_a, y: prices, name: "Series" }], { margin: { t: 0 } } );
Plotly.plot( graph_plot, [{ x: timestamps_b, y: sma, name: "SMA" }], { margin: { t: 0 } } );
}
</script>
</head>
<body onload="Init()">
<table>
<tbody>
<tr>
<td>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">[email protected]</a>
</div>
<ul class="nav navbar-nav">
<li onclick="setTabActive(event, 'Dataset')"><a href="#">Dataset</a></li>
<li onclick="setTabActive(event, 'Neural Network')"><a href="#">Neural Network</a></li>
<li onclick="setTabActive(event, 'Prediction')"><a href="#">Prediction</a></li>
</ul>
</div>
</nav>
</td>
</tr>
<tr>
<td>
<div id="Dataset" class="container">
<table width="100%">
<tr>
<td>
<table width="100%">
<tr>
<td width="60%" align="left">
<table width="100%">
<tr>
<td width="10px"><b> N-Items: </b></td>
<td width="120px"><input class="form-control input-sm" id="n-items" type="text" size="1" value="500"></td>
<td width="120px"><b> Window Size: </b></td>
<td width="100px"><input class="form-control input-sm" id="window-size" type="text" size="1" value="12"></td>
<td width="180px" align="center"><button type="button" class="btn btn-primary" onclick="initDataset()">Generate Data...</button></td>
</tr>
</table>
</td>
<td width="40%" align="right">
<form class="md-form">
<div class="file-field">
<div class="btn btn-primary btn-sm float-left">
<span>select *.csv data file</span>
<input type="file" id="input-data">
</div>
</div>
</form>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="100%" id="dataset"><hr/>
<table width="50%">
<tr>
<td align="left"><button type="button" class="btn btn-primary" onclick="onInputDataClick()">Input Data</button></td>
<td align="right"><button type="button" class="btn btn-primary" onclick="onSMAClick()">Simple Moving Average</button></td>
</tr>
</table>
<hr/>
<div id="data" style="overflow-y: scroll; max-height: 300px;"></div>
</td>
</tr>
<tr><td width="100%" id="graph-plot"><hr/><div id="graph" style="width:100%; height:350px;"></div></td></tr>
</table>
</div>
<div id="Neural Network" class="container">
<table width="100%">
<tr>
<td>
<button type="button" class="btn btn-primary" onclick="onTrainClick()">Train Model...</button><hr/>
<div class="progress">
<div id="training_pg" class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width:0%"></div>
</div>
<hr/>
</td>
</tr>
<tr>
<td>
<table width="100%" height="100%">
<tr>
<td width="80%"><div id="train_set" style="overflow-x: scroll; overflow-y: scroll; max-width: 900px; max-height: 300px;"></div></td>
<td>
<table width="100%" class="table">
<tr>
<td>
<label>Size (%):</label>
<input class="form-control input-sm" id="n-items-percent" type="text" size="1" value="50">
</td>
</tr>
<tr>
<td>
<label>Epochs:</label>
<input class="form-control input-sm" id="n-epochs" type="text" size="1" value="200">
</td>
</tr>
<tr>
<td>
<label>Learning Rate:</label>
<input class="form-control input-sm" id="learning-rate" type="text" size="1" value="0.01">
</td>
</tr>
<tr>
<td>
<label>Hidden Layers:</label>
<input class="form-control input-sm" id="hidden-layers" type="text" size="1" value="4">
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td><hr/><div id="nn_log" style="overflow-x: scroll; overflow-y: scroll; max-width: 900px; max-height: 250px;"></div></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<div id="Prediction" class="container">
<table width="100%">
<tr><td><button type="button" class="btn btn-primary" onclick="onPredictClick()">Predict</button><hr/></td></tr>
<tr><td><div id="pred-res" style="overflow-x: scroll; overflow-y: scroll; max-height: 300px;"></div></td></tr>
<tr><td id="graph-pred"><hr/><div id="graph" style="height:300px;"></div></td></tr>
</table>
</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>