-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizer.js
More file actions
159 lines (135 loc) · 5.14 KB
/
visualizer.js
File metadata and controls
159 lines (135 loc) · 5.14 KB
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
class Visualizer{
static drawNetwork(context, network){
// Styling
const margin = 50;
const width = context.canvas.width - margin*2;
const height = context.canvas.height - margin*2;
const left = margin;
const top = margin;
const nodeRadius = 10;
const nodeStrokeWidth = 2;
const levelHeight = height/(network.levels.length);
// Draw all levels by looping through the levels
// Draw from Reverse:
for(let i=network.levels.length-1; i>=0; i--){
const levelTop = top +
// Using Interpolation to find even y-spacing
// between levels
lerp(
height-levelHeight,
0,
network.levels.length == 1
? 0.5 // If there is only one level, center it
: i/(network.levels.length-1)
);
// Connection Dashes
context.setLineDash([7,2]);
// Draw Level [input visualize error]
Visualizer.drawLevel(
context, network.levels[i],
width, levelHeight, left, levelTop,
nodeRadius, nodeStrokeWidth,
// If it is last output level,
// Draw the Output Labels
i == network.levels.length-1
// Up, Down, Left, Right Symbols
? ["↑","↓","←","→"]
:[]
);
}
// Visualizer.drawLevel(
// context, network.levels[0],
// width, height, left, top, right, bottom,
// nodeRadius, nodeColor, nodeStrokeWidth);
}
static drawLevel(
context, level,
width, height, left, top,
nodeRadius, nodeStrokeWidth,
outputLabels){
// Redefine the right and bottom position for each Node
const right = left + width;
const bottom = top + height;
const {inputs, outputs, weights, biases} = level;
//// Draw Connections
for(let i=0; i<inputs.length; i++){
for(let j=0; j<outputs.length; j++){
context.beginPath();
context.moveTo(
Visualizer.#getNodeX(inputs, i, left, right),
bottom
);
context.lineTo(
Visualizer.#getNodeX(outputs, j, left, right),
top
);
context.lineWidth = nodeStrokeWidth;
// Draw Connection
context.strokeStyle = getRGBA(weights[i][j]);
context.stroke();
}
}
//// Find x-coordinates of the input nodes
// using Interpolation
for(let i=0; i<inputs.length; i++){
const x = Visualizer.#getNodeX(inputs, i, left, right);
// Draw Input Nodes
context.beginPath();
context.arc(x, bottom, nodeRadius*1.6, 0, Math.PI*2);
context.fillStyle = "black";
context.fill();
// Draw Input Nodes
context.beginPath();
context.arc(x, bottom, nodeRadius, 0, Math.PI*2);
context.fillStyle = getRGBA(inputs[i]);
context.fill();
}
//// Find x-coordinates of the output nodes
// using Interpolation
for(let i=0; i<outputs.length; i++){
const x = Visualizer.#getNodeX(outputs, i, left, right)
// Draw Output Nodes
context.beginPath();
context.arc(x, top, nodeRadius*1.6, 0, Math.PI*2);
context.fillStyle = "black";
context.fill();
// Draw Output Nodes
context.beginPath();
context.arc(x, top, nodeRadius, 0, Math.PI*2);
context.fillStyle = getRGBA(outputs[i]);
context.fill();
// Draw Biases Circle Arc
context.beginPath();
context.linewidth = nodeStrokeWidth;
context.arc(x, top, nodeRadius*1.5, 0, Math.PI*2);
context.strokeStyle = getRGBA(biases[i]);
context.setLineDash([3, 3]);
context.stroke();
context.setLineDash([]);
// Draw Output Labels
if(outputLabels[i]){
context.beginPath();
context.textAlign = "center";
context.textBaseline = "middle";
context.strokeStyle = "silver";
context.font = (nodeRadius*1.5)+"px Arial";
context.fillText(outputLabels[i], x, top);
context.strokeText(outputLabels[i], x, top);
}
// Debugger in console to check biases
// console.table(car.brain.levels[0])
}
}
// Find x-coordinates of the nodes
// using Interpolation
static #getNodeX(nodes, index, left, right){
return lerp(
left,
right,
// Input Node goes to middle of only 1
(nodes.length == 1)
? 0.5
: index/(nodes.length-1)
);
}
}