-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlpview.js
162 lines (133 loc) · 5.35 KB
/
mlpview.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
//requires: MultiLayerPerceptron
//class enclosure
(function() {
"use strict";
//constructor
const MlpView = function(mlp, parentElement = document.body, width = 320, height = 180) {
this.mlp = mlp;
this.parentElement = parentElement;
//setup canvas
this.canvas = document.createElement("canvas");
this.parentElement.appendChild(this.canvas);
this.canvas.setAttribute("style", "display: block; margin: auto; background-color: #000;");
this.context = this.canvas.getContext("2d");
//get largest layer size
let biggest = mlp.numInputs;
for (let i = 0; i < mlp.numLayers; i++) {
const layerSize = mlp.layers[i].numOutputs;
if (layerSize > biggest) {
biggest = layerSize;
}
};
this.biggestLayerSize = biggest;
//create node position matrix
this.nodePositions = [];
for (let i = 0; i < mlp.numLayers + 1; i++) {
this.nodePositions[i] = [];
}
//default settings
this.nodeWidth = 10;
this.absRange = 1;
this.negColor = { r: 1, g: 0, b: 0, a: 1 };
this.zeroColor = { r: 0, g: 0, b: 0, a: 0 };
this.posColor = { r: 1, g: 1, b: 1, a: 1 };
this.resize(width, height);
};
const proto = MlpView.prototype;
//public
proto.resize = function(width, height) {
this.canvas.width = width;
this.canvas.height = height;
const mlp = this.mlp;
const xSpacing = width / (mlp.numLayers + 2);
const ySpacing = height / (this.biggestLayerSize + 1);
let nodeX = xSpacing;
let inodeY = (height - ySpacing * (mlp.numInputs - 1)) / 2;
for (let j = 0; j < mlp.numInputs; j++) {
this.nodePositions[0][j] = { x: nodeX, y: inodeY };
inodeY += ySpacing;
}
nodeX += xSpacing;
for (let i = 0; i < mlp.numLayers; i++) {
const layer = mlp.layers[i];
const colLength = layer.numOutputs;
let nodeY = (height - ySpacing * (colLength - 1)) / 2;
for (let j = 0; j < colLength; j++) {
this.nodePositions[i + 1][j] = { x: nodeX, y: nodeY };
nodeY += ySpacing;
}
nodeX += xSpacing;
}
};
proto.draw = function() {
const ctx = this.context;
const previousState = this.mlp.previousState;
const width = this.canvas.width;
const height = this.canvas.height;
ctx.lineWidth = 0.3;
ctx.clearRect(0, 0, width, height);
//draw synapses
for (let i = 0; i < previousState.length; i++) {
const layerState = previousState[i];
const layerPos1 = this.nodePositions[i];
const layerPos2 = this.nodePositions[i + 1];
const numOut = layerState.outputs.length;
const numIn = layerState.inputs.length;
for (let opt = 0; opt < numOut; opt++) {
for (let ipt = 0; ipt < numIn; ipt++) {
const wt = opt * numIn + ipt;
const pos1 = layerPos1[ipt];
const pos2 = layerPos2[opt];
const value = layerState.synapses[wt];
const color = this._gradientMap(value);
ctx.strokeStyle = `rgba(${color.r*255},${color.g*255},${color.b*255},${color.a})`;
ctx.beginPath();
ctx.moveTo(pos1.x, pos1.y);
ctx.lineTo(pos2.x, pos2.y);
ctx.stroke();
}
}
}
//draw nodes
for (let i = 0; i < this.nodePositions.length; i++) {
const col = this.nodePositions[i];
for (let j = 0; j < col.length; j++) {
let value;
if (i === 0) {
value = previousState[0].inputs[j];
} else {
value = previousState[i - 1].outputs[j];
};
const color = this._gradientMap(value);
ctx.fillStyle = `rgba(${color.r*255},${color.g*255},${color.b*255},${color.a})`;
ctx.beginPath();
const pos = col[j];
ctx.arc(pos.x, pos.y, this.nodeWidth / 2, 0, Math.PI * 2);
ctx.closePath();
ctx.fill();
}
}
};
//private
proto._colorLerp = function(color1, color2, ratio) {
return {
r: color1.r + (color2.r - color1.r) * ratio,
g: color1.g + (color2.g - color1.g) * ratio,
b: color1.b + (color2.b - color1.b) * ratio,
a: color1.a + (color2.a - color1.a) * ratio,
};
};
proto._gradientMap = function(x) {
let color;
if (x < 0) {
color = this._colorLerp(this.negColor, this.zeroColor, (x + this.absRange) / this.absRange);
} else if (x > 0) {
color = this._colorLerp(this.zeroColor, this.posColor, x / this.absRange);
} else {
color = this.zeroColor;
}
return color;
};
//attach class to namespace
ml.MlpView = MlpView;
})();