-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
268 lines (202 loc) · 5.39 KB
/
index.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
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
import * as vis from 'vis';
import {
GoogleCharts
} from 'google-charts';
const gradientDiv = document.getElementById('gradient_div'),
functionLbl = document.getElementById('functionLbl'),
aRange = document.getElementById('aRange'),
aLabel = document.getElementById('aLabel'),
expRange = document.getElementById('expRange'),
expLabel = document.getElementById('expLabel');
let a, exp;
/**
*
* Draw the function containing a and exp to the Google line chart. Calulates the values in steps of .01
*
*/
async function drawChart() {
//Load the charts library with a callback
GoogleCharts.load(() => {
const data = [];
data.push(['x', 'y']);
for (let x = 0; x <= 1; x += .01) {
const dataObject = [x, f(x)];
data.push(dataObject);
}
const dataArray = google.visualization.arrayToDataTable(data);
const options = {
legend: 'none',
width: 500,
height: 500,
hAxis: {
title: 'x',
viewWindow: {
min: 0,
max: 1
}
},
vAxis: {
title: 'y',
viewWindow: {
min: 0,
max: 1
}
}
};
const chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(dataArray, options);
});
}
/**
*
* Draw a 3dGraph of the loss function for the given function
*
*/
async function drawLoss() {
// Create and populate a data table.
let data = new vis.DataSet();
const steps = 30; // Number of datapoints will be steps*steps, if higher you will see more
const axisMax = 3;
const axisStep = axisMax / steps;
for (let a = 0; a < axisMax; a += axisStep) {
for (let exp = 0; exp < axisMax; exp += axisStep) {
// Calculate the error for these values
let error = meanSquaredError(a, exp);
data.add({
x: a,
y: exp,
z: error
});
}
}
// Specify options
let options = {
onclick: onGraphClick,
xLabel: 'a',
yLabel: 'exponent',
zLabel: 'error',
style: 'surface',
showPerspective: true,
showGrid: true,
showShadow: false,
keepAspectRatio: true,
verticalRatio: 0.5
};
// Create a graph3d
new vis.Graph3d(gradientDiv, data, options);
}
/**
*
* Fetch the input from the inputFields and draw function parameter values to the ui
*
* @param event when touching ne of the two range bars
*
*/
async function fetchFunction(event) {
a = parseFloat(aRange.value);
exp = parseFloat(expRange.value);
if (event.target.id === 'aRange')
a = parseFloat(event.target.value);
else
exp = parseFloat(event.target.value);
aLabel.innerText = `a(${a})`;
expLabel.innerText = `exponent(${exp})`;
functionLbl.innerText = `f(x) = ${a} * x^${exp}`;
}
/**
*
* Draw a given polynomial function to th ui
*
*/
async function drawFunction() {
// Definition the polynomial function
// y = a * x^exp
let width = overlayDiv.width;
// Get context
let ctx = overlayFunctionDiv.getContext('2d');
ctx.clearRect(0, 0, overlayFunctionDiv.width, overlayFunctionDiv.height);
ctx.beginPath();
for (let x = 0; x <= 1; x += .1) {
const y = a * Math.pow(x, exp);
const pixelX = width * x,
pixelY = width * (1 - y);
// Every line starts with moveTo and continues with lineTo
if (x === 0)
ctx.moveTo(pixelX, pixelY);
else
ctx.lineTo(pixelX, pixelY);
}
// Finally draw the line to canvas
ctx.stroke();
}
/**
*
* Calculate the meanSquaredError up to xMax in steps
*
* @param m slope of the line
* @param b intercept of the line
*
*/
function meanSquaredError(a, exp) {
let meanSquared = 0,
xMax = 3,
steps = .1,
n = xMax / steps;
for (let x = 0; x <= xMax; x += steps) {
// Calculate y
let guessY = a * Math.pow(x, exp),
labelY = f(x);
// Calculate error for this step
let error = guessY - labelY;
error *= error;
meanSquared += error;
// Increase n(counter)
n++;
}
// Divide summed error by the number of operations to get the mean
meanSquared /= n;
// Return 15 if error is bigger than that(just for a good looking gradient)
if (meanSquared > 15)
return 15;
else
return meanSquared;
}
/**
*
* Handles a click event in a 3dGraph
*
* @param point contains click information(See http://visjs.org/docs/graph3d/)
*
*/
async function onGraphClick(point) {
let a = point.x.toFixed(1),
exp = point.y.toFixed(1),
error = point.z.toFixed(1);
console.log(a, exp, error);
}
/**
*
* Calculate y for given x value
*
* @param x
*
* @return y
*/
function f(x) {
return a * Math.pow(x, exp);
}
/**
*
* Visualize live parameters
*
* @param event when touching ne of the two range bars
*
*/
function visualize(event) {
fetchFunction(event);
drawChart();
drawLoss();
}
visualize();
aRange.addEventListener('input', event => visualize(event));
expRange.addEventListener('input', event => visualize(event));