-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.js
162 lines (132 loc) · 5.53 KB
/
visualizer.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
class Visualizer {
static drawNetwork(ctx, network) {
const margin = 50;
const left = margin;
const top = margin;
const width = ctx.canvas.width - margin * 2;
const right = left + width;
const height = ctx.canvas.height - margin * 2;
// const levelHeight=height/network.levels.length;
const layerTops = [];
for (let i = 0; i < network.levels.length + 1; i++) {
layerTops.push(top + lerp(height, 0, i / network.levels.length))
}
for (let i = 0; i < network.levels.length; i++) {
Visualizer.drawWeights(ctx, network.levels[i], left, right, layerTops[i], layerTops[i + 1]);
}
//draw input layer
Visualizer.drawNodes(ctx, network.levels[0].inputs, left, layerTops[0], right);
//draw output layer including hidden
for (let i = 0; i < network.levels.length; i++) {
Visualizer.drawNodes(ctx, network.levels[i].outputs, left, layerTops[i + 1], right);
Visualizer.drawBiases(ctx, network.levels[i].biases, left, layerTops[i + 1], right);
}
// Visualizer.drawLevel(ctx, network.levels[0], left, top, width, height);
//label output
Visualizer.labelOutput(ctx, ['\uD83E\uDC09', '\uD83E\uDC08', '\uD83E\uDC0A', '\uD83E\uDC0B'], left, layerTops[layerTops.length - 1], right);
}
static drawNodes(ctx, values, left, top, right) {
const nodeRadius = 18;
for (let i = 0; i < values.length; i++) {
const x = lerp(left, right, Visualizer.#getLerpT(values, i));
ctx.beginPath();
ctx.arc(x, top, nodeRadius, 0, Math.PI * 2);
ctx.fillStyle = "black";
ctx.fill();
ctx.beginPath();
ctx.arc(x, top, nodeRadius * 0.6, 0, Math.PI * 2);
ctx.fillStyle = getRGBA(values[i]);
ctx.fill();
}
}
static drawBiases(ctx, values, left, top, right) {
const nodeRadius = 18;
for (let i = 0; i < values.length; i++) {
const x = lerp(left, right, Visualizer.#getLerpT(values, i));
ctx.beginPath();
ctx.lineWidth = 2
ctx.arc(x, top, nodeRadius * 0.8, 0, Math.PI * 2);
ctx.strokeStyle = getRGBA(values[i]);
// ctx.setLineDash([3, 3]);
ctx.stroke();
// ctx.setLineDash([]);
}
}
static drawWeights(ctx, level, left, right, top, bottom) {
const { inputs, outputs, weights } = level;
for (let i = 0; i < inputs.length; i++) {
for (let j = 0; j < outputs.length; j++) {
ctx.beginPath();
ctx.moveTo(lerp(left, right, Visualizer.#getLerpT(inputs, i)), top);
ctx.lineTo(lerp(left, right, Visualizer.#getLerpT(outputs, j)), bottom);
ctx.lineWidth = 2;
ctx.strokeStyle = getRGBA(weights[i][j]);
ctx.setLineDash([7, 3]);
ctx.stroke();
}
}
}
static labelOutput(ctx, labels, left, top, right) {
const nodeRadius = 18;
for (let i = 0; i < labels.length; i++) {
const x = lerp(left, right, Visualizer.#getLerpT(labels, i));
ctx.beginPath();
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.fillStyle = "black";
ctx.strokeStyle = "white";
ctx.font = (nodeRadius * 1.5) + "px Arial";
ctx.fillText(labels[i], x, top + nodeRadius * 0.1);
ctx.lineWidth = 0.5;
ctx.strokeText(labels[i], x, top + nodeRadius * 0.1);
}
}
static drawLevel(ctx, level, left, top, width, height) {
const right = left + width;
const bottom = top + height;
const { inputs, outputs, weights, biases } = level;
for (let i = 0; i < inputs.length; i++) {
for (let j = 0; j < outputs.length; j++) {
ctx.beginPath();
ctx.moveTo(lerp(left, right, Visualizer.#getLerpT(inputs, i)), bottom);
ctx.lineTo(lerp(left, right, Visualizer.#getLerpT(outputs, j)), top);
ctx.lineWidth = 2;
ctx.strokeStyle = getRGBA(weights[i][j]);
ctx.stroke();
}
}
const nodeRadius = 18;
for (let i = 0; i < inputs.length; i++) {
const x = lerp(left, right, Visualizer.#getLerpT(inputs, i));
ctx.beginPath();
ctx.arc(x, bottom, nodeRadius, 0, Math.PI * 2);
ctx.fillStyle = "black";
ctx.fill();
ctx.beginPath();
ctx.arc(x, bottom, nodeRadius * 0.6, 0, Math.PI * 2);
ctx.fillStyle = getRGBA(inputs[i]);
ctx.fill();
}
for (let i = 0; i < outputs.length; i++) {
const x = lerp(left, right, Visualizer.#getLerpT(outputs, i));
ctx.beginPath();
ctx.arc(x, top, nodeRadius, 0, Math.PI * 2);
ctx.fillStyle = "black";
ctx.fill();
ctx.beginPath();
ctx.arc(x, top, nodeRadius * 0.6, 0, Math.PI * 2);
ctx.fillStyle = getRGBA(outputs[i]);
ctx.fill();
ctx.beginPath();
ctx.lineWidth = 2
ctx.arc(x, top, nodeRadius * 0.8, 0, Math.PI * 2);
ctx.strokeStyle = getRGBA(biases[i]);
ctx.setLineDash([3, 3]);
ctx.stroke();
ctx.setLineDash([]);
}
}
static #getLerpT(nodes, index) {
return nodes.length == 1 ? 0.5 : index / (nodes.length - 1);
}
}