Spaces:
Running
Running
Create script.js
Browse files
script.js
ADDED
@@ -0,0 +1,214 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
// colab link: [...]
|
2 |
+
|
3 |
+
function MnistRNN() {
|
4 |
+
var model = this;
|
5 |
+
|
6 |
+
this.weights_meta = {
|
7 |
+
'(MnistNet).dropout(Dropout).keygen(Generator)._key': [[1973249, 1973251], [2]],
|
8 |
+
'(MnistNet).lstm_core(LSTMCore).fc(Linear).b': [[266496, 268544], [2048]],
|
9 |
+
'(MnistNet).lstm_core(LSTMCore).fc(Linear).w': [[268544, 1841408], [768, 2048]],
|
10 |
+
'(MnistNet).output_head(Linear).b': [[1841408, 1841665], [257]],
|
11 |
+
'(MnistNet).output_head(Linear).w': [[1841665, 1973249], [512, 257]],
|
12 |
+
'(MnistNet).pos_embed(Embed).embeddings': [[0, 200704], [784, 256]],
|
13 |
+
'(MnistNet).value_embed(Embed).embeddings': [[200704, 266496], [257, 256]]
|
14 |
+
};
|
15 |
+
|
16 |
+
this.is_model_ready = false;
|
17 |
+
|
18 |
+
this.embed_lookup = function(index, weights) {
|
19 |
+
return tf.slice(weights, [index], [1]);
|
20 |
+
};
|
21 |
+
|
22 |
+
this.pos = 0;
|
23 |
+
this.state = null;
|
24 |
+
this.start_token = 256;
|
25 |
+
this.hidden_size = this.weights_meta['(MnistNet).lstm_core(LSTMCore).fc(Linear).b'][1][0] / 4;
|
26 |
+
|
27 |
+
this.initialize_state = function() {
|
28 |
+
this.pos = 0;
|
29 |
+
this.token = this.start_token;
|
30 |
+
var hidden = tf.zeros([1, this.hidden_size]);
|
31 |
+
var cell = tf.zeros([1, this.hidden_size]);
|
32 |
+
this.state = [hidden, cell];
|
33 |
+
};
|
34 |
+
|
35 |
+
this.lstm_core = function(inputs, state, weights) {
|
36 |
+
const [hidden, cell] = state;
|
37 |
+
const [w, b] = weights;
|
38 |
+
const i_and_h =tf.concat([inputs, hidden], 1);
|
39 |
+
const gated = tf.add(tf.matMul(i_and_h, w), b);
|
40 |
+
const [i, g, f, o] = tf.split(gated, 4, 1);
|
41 |
+
const f_ = tf.sigmoid(tf.add(f, 1.));
|
42 |
+
const i_ = tf.sigmoid(i);
|
43 |
+
const g_ = tf.tanh(g);
|
44 |
+
const c = tf.add(
|
45 |
+
tf.mul(i_, g_),
|
46 |
+
tf.mul(cell, f_)
|
47 |
+
);
|
48 |
+
const h = tf.mul(
|
49 |
+
tf.sigmoid(o),
|
50 |
+
tf.tanh(c)
|
51 |
+
);
|
52 |
+
return [h, c];
|
53 |
+
};
|
54 |
+
|
55 |
+
this.step = function() {
|
56 |
+
const [token, h, c] = tf.tidy( function() {
|
57 |
+
const lstm_b = model.MODEL_WEIGHTS['(MnistNet).lstm_core(LSTMCore).fc(Linear).b'];
|
58 |
+
const lstm_w = model.MODEL_WEIGHTS['(MnistNet).lstm_core(LSTMCore).fc(Linear).w'];
|
59 |
+
const output_b = model.MODEL_WEIGHTS['(MnistNet).output_head(Linear).b'];
|
60 |
+
const output_w = model.MODEL_WEIGHTS['(MnistNet).output_head(Linear).w'];
|
61 |
+
const pos_embed = model.MODEL_WEIGHTS['(MnistNet).pos_embed(Embed).embeddings'];
|
62 |
+
const value_embed = model.MODEL_WEIGHTS['(MnistNet).value_embed(Embed).embeddings'];
|
63 |
+
const v = model.embed_lookup(model.token, value_embed);
|
64 |
+
const p = model.embed_lookup(model.pos, pos_embed);
|
65 |
+
const x = tf.add(v, p);
|
66 |
+
const [h, c] = model.lstm_core(x, model.state, [lstm_w, lstm_b]);
|
67 |
+
tf.dispose(model.state[0]);
|
68 |
+
tf.dispose(model.state[1]);
|
69 |
+
const logits = tf.add(
|
70 |
+
tf.matMul(h, output_w),
|
71 |
+
output_b
|
72 |
+
);
|
73 |
+
const token = tf.multinomial(logits, 1).dataSync()[0];
|
74 |
+
|
75 |
+
return [token, h, c];
|
76 |
+
});
|
77 |
+
|
78 |
+
this.clean_memory();
|
79 |
+
this.token = token;
|
80 |
+
this.state = [h, c];
|
81 |
+
canvas.plot_xyc(this.pos, token);
|
82 |
+
this.pos = this.pos + 1;
|
83 |
+
};
|
84 |
+
|
85 |
+
this.MODEL_WEIGHTS = {};
|
86 |
+
this.clean_memory = function() {
|
87 |
+
tf.dispose(model.state[0]);
|
88 |
+
tf.dispose(model.state[1]);
|
89 |
+
};
|
90 |
+
|
91 |
+
this.loop = function() {
|
92 |
+
this.step();
|
93 |
+
if (this.pos >=28*28) {
|
94 |
+
setTimeout(function(){
|
95 |
+
model.clean_memory();
|
96 |
+
model.initialize_state();
|
97 |
+
canvas.plot_grid();
|
98 |
+
model.loop();
|
99 |
+
}, 3000);
|
100 |
+
} else {
|
101 |
+
canvas.plot_xyc(this.pos, 255);
|
102 |
+
setTimeout(function(){model.loop();}, 0);
|
103 |
+
}
|
104 |
+
};
|
105 |
+
|
106 |
+
this.load_model_weights = function() {
|
107 |
+
var req = new XMLHttpRequest();
|
108 |
+
req.open("GET", "weights.bin", true);
|
109 |
+
console.log('loading weights...');
|
110 |
+
req.responseType = "arraybuffer";
|
111 |
+
var this_ = this;
|
112 |
+
req.onload = function (event) {
|
113 |
+
var buff = req.response;
|
114 |
+
if (buff) {
|
115 |
+
var W = new Float32Array(buff);
|
116 |
+
for(var k in this_.weights_meta) {
|
117 |
+
info = this_.weights_meta[k];
|
118 |
+
offset = info[0];
|
119 |
+
shape = info[1];
|
120 |
+
this_.MODEL_WEIGHTS[k] = tf.tensor(W.subarray(offset[0], offset[1]), shape);
|
121 |
+
}
|
122 |
+
this_.is_model_ready = true;
|
123 |
+
} else {
|
124 |
+
alert('Error while loading weights...');
|
125 |
+
}
|
126 |
+
};
|
127 |
+
req.send(null);
|
128 |
+
};
|
129 |
+
|
130 |
+
this.load_when_ready = function() {
|
131 |
+
tf.ready().then( function() {
|
132 |
+
tf.enableProdMode();
|
133 |
+
console.log('tf is ready');
|
134 |
+
model.initialize_state()
|
135 |
+
model.load_model_weights();
|
136 |
+
console.log(model.hidden_size);
|
137 |
+
});
|
138 |
+
};
|
139 |
+
}
|
140 |
+
|
141 |
+
|
142 |
+
function MnistCanvas() {
|
143 |
+
var canvas = document.getElementById("mnist-canvas");
|
144 |
+
canvas.width = window.innerWidth;
|
145 |
+
canvas.height = window.innerHeight;
|
146 |
+
context=canvas.getContext('2d');
|
147 |
+
context.translate(canvas.width/2,canvas.height/2);
|
148 |
+
var scale = Math.floor(Math.min(canvas.width, canvas.height) / (28*2) ) * 28;
|
149 |
+
console.log(scale);
|
150 |
+
context.scale(scale, scale)
|
151 |
+
context.imageSmoothingEnabled = false;
|
152 |
+
|
153 |
+
this.clear = function() {
|
154 |
+
context.clearRect(-1, -1, 2., 2.);
|
155 |
+
context.fillStyle = "rgb(0, 0, 0)";
|
156 |
+
context.fillRect(-10, -10, 20, 20);
|
157 |
+
};
|
158 |
+
|
159 |
+
this.plot_grid = function() {
|
160 |
+
for (var i=0; i< 28*28; i++) this.plot_xyc(i, 0);
|
161 |
+
};
|
162 |
+
|
163 |
+
this.plot_xyc = function (pos, color) {
|
164 |
+
color = Math.max(20, color);
|
165 |
+
var step = 1. / 28;
|
166 |
+
var y = Math.floor(pos / 28 - 14) * step;
|
167 |
+
var x = (pos % 28 - 14) * step;
|
168 |
+
context.fillStyle = "rgb(0, " + color + ", 0)";
|
169 |
+
context.fillRect(x, y, step, step);
|
170 |
+
context.strokeStyle = "rgb(0, 0, 0)";
|
171 |
+
context.lineWidth = 0.008;
|
172 |
+
context.strokeRect(x, y, step, step);
|
173 |
+
};
|
174 |
+
|
175 |
+
this.loading_animation = function() {
|
176 |
+
var counter = 0;
|
177 |
+
var this_ = this;
|
178 |
+
this_.plot_grid();
|
179 |
+
|
180 |
+
var draw = function() {
|
181 |
+
if (model.is_model_ready) {
|
182 |
+
console.log('stopping animation.');
|
183 |
+
model.loop();
|
184 |
+
return;
|
185 |
+
}
|
186 |
+
if (counter >= 28*28) {
|
187 |
+
this_.plot_grid();
|
188 |
+
counter = 0;
|
189 |
+
}
|
190 |
+
this_.plot_xyc(counter, 255);
|
191 |
+
if (counter < 28*28-1) {
|
192 |
+
this_.plot_xyc(counter+1, 255);
|
193 |
+
}
|
194 |
+
counter = counter+1;
|
195 |
+
window.requestAnimationFrame(draw);
|
196 |
+
};
|
197 |
+
window.requestAnimationFrame(draw);
|
198 |
+
};
|
199 |
+
}
|
200 |
+
|
201 |
+
|
202 |
+
var model = null;
|
203 |
+
var canvas = null;
|
204 |
+
|
205 |
+
window.onload = function() {
|
206 |
+
setTimeout(function() {
|
207 |
+
model = new MnistRNN();
|
208 |
+
canvas = new MnistCanvas();
|
209 |
+
console.log("init...");
|
210 |
+
canvas.clear();
|
211 |
+
canvas.loading_animation();
|
212 |
+
model.load_when_ready();
|
213 |
+
}, 500);
|
214 |
+
};
|