-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2layers.html
565 lines (546 loc) · 18.3 KB
/
2layers.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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Activation Function Demo with Weighted Plot</title>
<!-- Include Plotly.js -->
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<style>
body,
html {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
/* Header at the top with dropdown */
.header {
width: 100vw;
text-align: center;
padding: 10px;
background-color: #ddd;
font-family: sans-serif;
}
.grid-container {
display: grid;
grid-template-columns: 50% 50%;
grid-template-rows: 1fr 1fr;
width: 100vw;
height: 90vh; /* slightly less to accommodate header */
}
.grid-item {
padding: 10px;
}
/* Left column items: the two activation function plots (sigmoid or relu) */
.left-column {
grid-column: 1;
}
/* First plot on the left (cell 1) */
#cell-1 {
grid-row: 1;
display: flex;
flex-direction: column;
justify-content: space-between;
}
/* Second plot on the left (cell 2) */
#cell-2 {
grid-row: 2;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.plot-container {
flex-grow: 1;
width: 100%;
height: 100%;
}
.slider-container {
display: flex;
justify-content: space-around;
padding: 10px;
}
.slider-group {
display: flex;
flex-direction: column;
align-items: center;
}
/* Right column: weighted plot centered vertically */
.right-column {
grid-column: 2;
grid-row: 1 / span 2;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.footer {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
background-color: #f8f9fa;
color: #6c757d;
text-align: center;
padding: 10px 0;
grid-column: span 2;
}
.footer a {
color: #007bff;
text-decoration: none;
}
.footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<!-- Header with dropdown to select activation function -->
<div class="header">
<label for="activationSelect">First-layer Activation Function: </label>
<select id="activationSelect" onchange="changeActivation()">
<option value="sigmoid" selected>Sigmoid</option>
<option value="relu">ReLU</option>
</select>
</div>
<div class="grid-container">
<!-- Left column, first row: First activation function plot and sliders -->
<div id="cell-1" class="grid-item left-column">
<div class="plot-container" id="plot3d"></div>
<div class="slider-container">
<div class="slider-group">
<label for="theta_11">θ₁₁</label>
<input
type="range"
id="theta_11"
min="-5"
max="5"
value="5"
step="0.1"
oninput="updatez1()"
/>
<span id="theta_11_value">5</span>
</div>
<div class="slider-group">
<label for="theta_12">θ₁₂</label>
<input
type="range"
id="theta_12"
min="-5"
max="5"
value="5"
step="0.1"
oninput="updatez1()"
/>
<span id="theta_12_value">5</span>
</div>
<div class="slider-group">
<label for="theta_10">θ₁₀</label>
<input
type="range"
id="theta_10"
min="-1"
max="1"
value="-1"
step="0.1"
oninput="updatez1()"
/>
<span id="theta_10_value">-1</span>
</div>
</div>
</div>
<!-- Left column, second row: Second activation function plot and sliders -->
<div id="cell-2" class="grid-item left-column">
<div class="plot-container" id="plot3d_2"></div>
<div class="slider-container">
<div class="slider-group">
<label for="theta_21">θ₂₁</label>
<input
type="range"
id="theta_21"
min="-5"
max="5"
value="-5"
step="0.1"
oninput="updatez2()"
/>
<span id="theta_21_value">-5</span>
</div>
<div class="slider-group">
<label for="theta_22">θ₂₂</label>
<input
type="range"
id="theta_22"
min="-5"
max="5"
value="-5"
step="0.1"
oninput="updatez2()"
/>
<span id="theta_22_value">-5</span>
</div>
<div class="slider-group">
<label for="theta_20">θ₂₀</label>
<input
type="range"
id="theta_20"
min="-1"
max="1"
value="-1"
step="0.1"
oninput="updatez2()"
/>
<span id="theta_20_value">-1</span>
</div>
</div>
</div>
<!-- Right column, spanning both rows: Weighted plot, centered vertically -->
<div class="grid-item right-column">
<div class="plot-container" id="plot_weighted"></div>
<div class="slider-container">
<div class="slider-group">
<label for="w1">w₁</label>
<input
type="range"
id="w1"
min="-5"
max="5"
value="5"
step="0.1"
oninput="update_layer2()"
/>
<span id="w1_value">5</span>
</div>
<div class="slider-group">
<label for="w2">w₂</label>
<input
type="range"
id="w2"
min="-5"
max="5"
value="5"
step="0.1"
oninput="update_layer2()"
/>
<span id="w2_value">5</span>
</div>
<div class="slider-group">
<label for="w0">w₀</label>
<input
type="range"
id="w0"
min="-5"
max="5"
value="-4"
step="0.1"
oninput="update_layer2()"
/>
<span id="w0_value">-4</span>
</div>
</div>
</div>
</div>
<div class="footer">
Built with ❤️ by <a href="https://shenshen.mit.edu" target="_blank">Shen²</a>
<span> | </span>
<a href="https://github.com/shensquared/demos" target="_blank">Report a Bug</a>
</div>
<script>
// Global variable for activation function
var activationFunction = "sigmoid";
// Change activation based on dropdown selection and update the plots
function changeActivation() {
activationFunction = document.getElementById("activationSelect").value;
updatez1();
updatez2();
}
// Fixed camera settings for 3D plots
const fixedCameraSettings = {
eye: { x: 1.5, y: 1.5, z: 1.5 },
center: { x: 0, y: 0, z: 0 },
up: { x: 0, y: 0, z: 1 },
};
// Increase grid resolution for a smoother surface
const gridStep = 0.05;
const numPoints = Math.floor(4 / gridStep) + 1;
const x1Grid = Array.from({ length: numPoints }, (_, i) => -2 + i * gridStep);
const x2Grid = Array.from({ length: numPoints }, (_, i) => -2 + i * gridStep);
let z1Data, z2Data; // Store data for weighted plot
// Compute activation value based on the selected function
function computeActivation(x1, x2, theta1, theta2, theta0) {
const linear = theta1 * x1 + theta2 * x2 + theta0;
if (activationFunction === "sigmoid") {
return 1 / (1 + Math.exp(-linear));
} else if (activationFunction === "relu") {
return Math.max(0, linear);
}
}
// Generate data for the first activation function plot (z₁)
function generatez1_2(theta_11, theta_12, theta_10) {
let z = [];
for (let i = 0; i < x1Grid.length; i++) {
let zRow = [];
for (let j = 0; j < x2Grid.length; j++) {
zRow.push(computeActivation(x1Grid[i], x2Grid[j], theta_11, theta_12, theta_10));
}
z.push(zRow);
}
return { x1: x1Grid, y: x2Grid, z: z, showscale: false };
}
// Generate data for the second activation function plot (z₂)
function generatez2(theta_21, theta_22, theta_20) {
let z = [];
for (let i = 0; i < x1Grid.length; i++) {
let zRow = [];
for (let j = 0; j < x2Grid.length; j++) {
zRow.push(computeActivation(x1Grid[i], x2Grid[j], theta_21, theta_22, theta_20));
}
z.push(zRow);
}
return { x1: x1Grid, y: x2Grid, z: z, showscale: false };
}
// Create/update the weighted 3D plot (w₁z₁ + w₂z₂ + w₀)
function update_layer2() {
var w1 = parseFloat(document.getElementById("w1").value);
var w2 = parseFloat(document.getElementById("w2").value);
var w0 = parseFloat(document.getElementById("w0").value);
let zWeighted = [];
for (let i = 0; i < z1Data.length; i++) {
let zRow = [];
for (let j = 0; j < z1Data[i].length; j++) {
zRow.push(w1 * z1Data[i][j] + w2 * z2Data[i][j] + w0);
}
zWeighted.push(zRow);
}
var layout = {
margin: { l: 0, r: 0, t: 0, b: 0 },
scene: {
xaxis: {
title: "x₁",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
yaxis: {
title: "x₂",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
zaxis: {
title: "w₁z₁ + w₂z₂ + w₀",
range: [-5, 5],
tickmode: "array",
tickvals: [-1, 0, 1]
},
aspectmode: "cube",
camera: fixedCameraSettings
}
};
Plotly.react("plot_weighted", [{
x: x1Grid,
y: x2Grid,
z: zWeighted,
type: "surface",
showscale: false,
}], layout);
document.getElementById("w1_value").textContent = w1;
document.getElementById("w2_value").textContent = w2;
document.getElementById("w0_value").textContent = w0;
}
// Create the first 3D plot (z₁)
function createz1() {
var theta_11 = parseFloat(document.getElementById("theta_11").value);
var theta_12 = parseFloat(document.getElementById("theta_12").value);
var theta_10 = parseFloat(document.getElementById("theta_10").value);
var data = generatez1_2(theta_11, theta_12, theta_10);
z1Data = data.z;
var layout = {
margin: { l: 0, r: 0, t: 0, b: 0 },
scene: {
xaxis: {
title: "x₁",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
yaxis: {
title: "x₂",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
zaxis: {
title: "z₁ = " + (activationFunction === "sigmoid" ? "σ" : "ReLU") +
"(θ₁₁x₁ + θ₁₂x₂ + θ₁₀)",
range: [0, activationFunction === "sigmoid" ? 1 : 10],
tickmode: "array",
tickvals: [-1, 0, 1]
},
aspectmode: "cube",
showscale: false
}
};
Plotly.newPlot("plot3d", [{
x: x1Grid,
y: x2Grid,
z: data.z,
type: "surface",
showscale: false
}], layout);
}
// Create the second 3D plot (z₂)
function createz2() {
var theta_21 = parseFloat(document.getElementById("theta_21").value);
var theta_22 = parseFloat(document.getElementById("theta_22").value);
var theta_20 = parseFloat(document.getElementById("theta_20").value);
var data = generatez2(theta_21, theta_22, theta_20);
z2Data = data.z;
var layout = {
margin: { l: 0, r: 0, t: 0, b: 0 },
scene: {
xaxis: {
title: "x₁",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
yaxis: {
title: "x₂",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
zaxis: {
title: "z₂ = " + (activationFunction === "sigmoid" ? "σ" : "ReLU") +
"(θ₂₁x₁ + θ₂₂x₂ + θ₂₀)",
range: [0, activationFunction === "sigmoid" ? 1 : 10],
tickmode: "array",
tickvals: [-1, 0, 1]
},
aspectmode: "cube",
camera: fixedCameraSettings
}
};
Plotly.newPlot("plot3d_2", [{
x: x1Grid,
y: x2Grid,
z: data.z,
type: "surface",
showscale: false
}], layout);
}
// Update the first 3D plot (z₁)
function updatez1() {
var theta_11 = parseFloat(document.getElementById("theta_11").value);
var theta_12 = parseFloat(document.getElementById("theta_12").value);
var theta_10 = parseFloat(document.getElementById("theta_10").value);
document.getElementById("theta_11_value").textContent = theta_11;
document.getElementById("theta_12_value").textContent = theta_12;
document.getElementById("theta_10_value").textContent = theta_10;
var data = generatez1_2(theta_11, theta_12, theta_10);
z1Data = data.z;
var layout = {
margin: { l: 0, r: 0, t: 0, b: 0 },
scene: {
xaxis: {
title: "x₁",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
yaxis: {
title: "x₂",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
zaxis: {
title: "z₁ = " + (activationFunction === "sigmoid" ? "σ" : "ReLU") +
"(θ₁₁x₁ + θ₁₂x₂ + θ₁₀)",
range: [0, activationFunction === "sigmoid" ? 1 : 10],
tickmode: "array",
tickvals: [-1, 0, 1]
},
aspectmode: "cube",
showscale: false
}
};
Plotly.react("plot3d", [{
x: x1Grid,
y: x2Grid,
z: data.z,
type: "surface",
showscale: false
}], layout);
update_layer2();
}
// Update the second 3D plot (z₂)
function updatez2() {
var theta_21 = parseFloat(document.getElementById("theta_21").value);
var theta_22 = parseFloat(document.getElementById("theta_22").value);
var theta_20 = parseFloat(document.getElementById("theta_20").value);
document.getElementById("theta_21_value").textContent = theta_21;
document.getElementById("theta_22_value").textContent = theta_22;
document.getElementById("theta_20_value").textContent = theta_20;
var data = generatez2(theta_21, theta_22, theta_20);
z2Data = data.z;
var layout = {
margin: { l: 0, r: 0, t: 0, b: 0 },
scene: {
xaxis: {
title: "x₁",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
yaxis: {
title: "x₂",
range: [-2, 2],
tickmode: "array",
tickvals: [-1, 0, 1]
},
zaxis: {
title: "z₂ = " + (activationFunction === "sigmoid" ? "σ" : "ReLU") +
"(θ₂₁x₁ + θ₂₂x₂ + θ₂₀)",
range: [0, activationFunction === "sigmoid" ? 1 : 10],
tickmode: "array",
tickvals: [-1, 0, 1]
},
aspectmode: "cube",
camera: fixedCameraSettings
}
};
Plotly.react("plot3d_2", [{
x: x1Grid,
y: x2Grid,
z: data.z,
type: "surface",
showscale: false
}], layout);
update_layer2();
}
// Initialize the plots on window load
window.onload = function () {
document.getElementById("theta_11_value").textContent = document.getElementById("theta_11").value;
document.getElementById("theta_12_value").textContent = document.getElementById("theta_12").value;
document.getElementById("theta_10_value").textContent = document.getElementById("theta_10").value;
document.getElementById("theta_21_value").textContent = document.getElementById("theta_21").value;
document.getElementById("theta_22_value").textContent = document.getElementById("theta_22").value;
document.getElementById("theta_20_value").textContent = document.getElementById("theta_20").value;
document.getElementById("w1_value").textContent = document.getElementById("w1").value;
document.getElementById("w2_value").textContent = document.getElementById("w2").value;
document.getElementById("w0_value").textContent = document.getElementById("w0").value;
createz1();
createz2();
update_layer2();
};
// Optional: Resize plots on window resize
window.addEventListener("resize", function () {
Plotly.Plots.resize(document.getElementById("plot3d"));
Plotly.Plots.resize(document.getElementById("plot3d_2"));
Plotly.Plots.resize(document.getElementById("plot_weighted"));
});
</script>
</body>
</html>