trituenhantaoio ntt123 commited on
Commit
7a00d6f
0 Parent(s):

Duplicate from ntt123/vietnamese-handwriting

Browse files

Co-authored-by: Thông Nguyễn <ntt123@users.noreply.huggingface.co>

Files changed (7) hide show
  1. .gitattributes +27 -0
  2. README.md +12 -0
  3. index.html +244 -0
  4. model_weights.js +51 -0
  5. script.js +246 -0
  6. style.css +28 -0
  7. weights.bin +3 -0
.gitattributes ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.onnx filter=lfs diff=lfs merge=lfs -text
13
+ *.ot filter=lfs diff=lfs merge=lfs -text
14
+ *.parquet filter=lfs diff=lfs merge=lfs -text
15
+ *.pb filter=lfs diff=lfs merge=lfs -text
16
+ *.pt filter=lfs diff=lfs merge=lfs -text
17
+ *.pth filter=lfs diff=lfs merge=lfs -text
18
+ *.rar filter=lfs diff=lfs merge=lfs -text
19
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
20
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
21
+ *.tflite filter=lfs diff=lfs merge=lfs -text
22
+ *.tgz filter=lfs diff=lfs merge=lfs -text
23
+ *.wasm filter=lfs diff=lfs merge=lfs -text
24
+ *.xz filter=lfs diff=lfs merge=lfs -text
25
+ *.zip filter=lfs diff=lfs merge=lfs -text
26
+ *.zstandard filter=lfs diff=lfs merge=lfs -text
27
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Vietnamese Handwriting
3
+ emoji: 🏃
4
+ colorFrom: indigo
5
+ colorTo: blue
6
+ sdk: static
7
+ pinned: false
8
+ license: cc-by-nc-4.0
9
+ duplicated_from: ntt123/vietnamese-handwriting
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference
index.html ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html>
3
+
4
+ <head>
5
+ <meta charset="utf-8">
6
+ <meta name="viewport" content="width=device-width">
7
+ <title>Online Handwriting</title>
8
+ <link href="/style.css" rel="stylesheet" type="text/css" />
9
+ <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
10
+ <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs-backend-wasm/dist/tf-backend-wasm.js"></script>
11
+ <script src="model_weights.js"></script>
12
+ <script src="script.js"></script>
13
+ </head>
14
+
15
+ <body>
16
+ <input placeholder="Nhập câu tiếng việt" id="user-input"/>
17
+ <select id="writers" name="writers">
18
+ <option value="0"> Writer 0</option>
19
+ <option value="1"> Writer 1</option>
20
+ <option value="2"> Writer 2</option>
21
+ <option value="3"> Writer 3</option>
22
+ <option value="4"> Writer 4</option>
23
+ <option value="5"> Writer 5</option>
24
+ <option value="6"> Writer 6</option>
25
+ <option value="7"> Writer 7</option>
26
+ <option value="8"> Writer 8</option>
27
+ <option value="9"> Writer 9</option>
28
+ <option value="10"> Writer 10</option>
29
+ <option value="11"> Writer 11</option>
30
+ <option value="12"> Writer 12</option>
31
+ <option value="13"> Writer 13</option>
32
+ <option value="14"> Writer 14</option>
33
+ <option value="15"> Writer 15</option>
34
+ <option value="16"> Writer 16</option>
35
+ <option value="17"> Writer 17</option>
36
+ <option value="18"> Writer 18</option>
37
+ <option value="19"> Writer 19</option>
38
+ <option value="20"> Writer 20</option>
39
+ <option value="21"> Writer 21</option>
40
+ <option value="22"> Writer 22</option>
41
+ <option value="23"> Writer 23</option>
42
+ <option value="24"> Writer 24</option>
43
+ <option value="25"> Writer 25</option>
44
+ <option value="26"> Writer 26</option>
45
+ <option value="27"> Writer 27</option>
46
+ <option value="28"> Writer 28</option>
47
+ <option value="29"> Writer 29</option>
48
+ <option value="30"> Writer 30</option>
49
+ <option value="31"> Writer 31</option>
50
+ <option value="32"> Writer 32</option>
51
+ <option value="33"> Writer 33</option>
52
+ <option value="34"> Writer 34</option>
53
+ <option value="35"> Writer 35</option>
54
+ <option value="36"> Writer 36</option>
55
+ <option value="37"> Writer 37</option>
56
+ <option value="38"> Writer 38</option>
57
+ <option value="39"> Writer 39</option>
58
+ <option value="40"> Writer 40</option>
59
+ <option value="41"> Writer 41</option>
60
+ <option value="42"> Writer 42</option>
61
+ <option value="43"> Writer 43</option>
62
+ <option value="44"> Writer 44</option>
63
+ <option value="45"> Writer 45</option>
64
+ <option value="46"> Writer 46</option>
65
+ <option value="47"> Writer 47</option>
66
+ <option value="48"> Writer 48</option>
67
+ <option value="49"> Writer 49</option>
68
+ <option value="50"> Writer 50</option>
69
+ <option value="51"> Writer 51</option>
70
+ <option value="52"> Writer 52</option>
71
+ <option value="53"> Writer 53</option>
72
+ <option value="54"> Writer 54</option>
73
+ <option value="55"> Writer 55</option>
74
+ <option value="56"> Writer 56</option>
75
+ <option value="57"> Writer 57</option>
76
+ <option value="58"> Writer 58</option>
77
+ <option value="59"> Writer 59</option>
78
+ <option value="60"> Writer 60</option>
79
+ <option value="61"> Writer 61</option>
80
+ <option value="62"> Writer 62</option>
81
+ <option value="63"> Writer 63</option>
82
+ <option value="64"> Writer 64</option>
83
+ <option value="65"> Writer 65</option>
84
+ <option value="66"> Writer 66</option>
85
+ <option value="67"> Writer 67</option>
86
+ <option value="68"> Writer 68</option>
87
+ <option value="69"> Writer 69</option>
88
+ <option value="70"> Writer 70</option>
89
+ <option value="71"> Writer 71</option>
90
+ <option value="72"> Writer 72</option>
91
+ <option value="73"> Writer 73</option>
92
+ <option value="74"> Writer 74</option>
93
+ <option value="75"> Writer 75</option>
94
+ <option value="76"> Writer 76</option>
95
+ <option value="77"> Writer 77</option>
96
+ <option value="78"> Writer 78</option>
97
+ <option value="79"> Writer 79</option>
98
+ <option value="80"> Writer 80</option>
99
+ <option value="81"> Writer 81</option>
100
+ <option value="82"> Writer 82</option>
101
+ <option value="83"> Writer 83</option>
102
+ <option value="84"> Writer 84</option>
103
+ <option value="85"> Writer 85</option>
104
+ <option value="86"> Writer 86</option>
105
+ <option value="87"> Writer 87</option>
106
+ <option value="88"> Writer 88</option>
107
+ <option value="89"> Writer 89</option>
108
+ <option value="90"> Writer 90</option>
109
+ <option value="91"> Writer 91</option>
110
+ <option value="92"> Writer 92</option>
111
+ <option value="93"> Writer 93</option>
112
+ <option value="94"> Writer 94</option>
113
+ <option value="95"> Writer 95</option>
114
+ <option value="96"> Writer 96</option>
115
+ <option value="97"> Writer 97</option>
116
+ <option value="98"> Writer 98</option>
117
+ <option value="99"> Writer 99</option>
118
+ <option value="100"> Writer 100</option>
119
+ <option value="101"> Writer 101</option>
120
+ <option value="102"> Writer 102</option>
121
+ <option value="103"> Writer 103</option>
122
+ <option value="104"> Writer 104</option>
123
+ <option value="105"> Writer 105</option>
124
+ <option value="106"> Writer 106</option>
125
+ <option value="107"> Writer 107</option>
126
+ <option value="108"> Writer 108</option>
127
+ <option value="109"> Writer 109</option>
128
+ <option value="110"> Writer 110</option>
129
+ <option value="111"> Writer 111</option>
130
+ <option value="112"> Writer 112</option>
131
+ <option value="113"> Writer 113</option>
132
+ <option value="114"> Writer 114</option>
133
+ <option value="115"> Writer 115</option>
134
+ <option value="116"> Writer 116</option>
135
+ <option value="117"> Writer 117</option>
136
+ <option value="118"> Writer 118</option>
137
+ <option value="119"> Writer 119</option>
138
+ <option value="120"> Writer 120</option>
139
+ <option value="121"> Writer 121</option>
140
+ <option value="122"> Writer 122</option>
141
+ <option value="123"> Writer 123</option>
142
+ <option value="124"> Writer 124</option>
143
+ <option value="125"> Writer 125</option>
144
+ <option value="126"> Writer 126</option>
145
+ <option value="127"> Writer 127</option>
146
+ <option value="128"> Writer 128</option>
147
+ <option value="129"> Writer 129</option>
148
+ <option value="130"> Writer 130</option>
149
+ <option value="131"> Writer 131</option>
150
+ <option value="132"> Writer 132</option>
151
+ <option value="133"> Writer 133</option>
152
+ <option value="134"> Writer 134</option>
153
+ <option value="135"> Writer 135</option>
154
+ <option value="136"> Writer 136</option>
155
+ <option value="137"> Writer 137</option>
156
+ <option value="138"> Writer 138</option>
157
+ <option value="139"> Writer 139</option>
158
+ <option value="140"> Writer 140</option>
159
+ <option value="141"> Writer 141</option>
160
+ <option value="142"> Writer 142</option>
161
+ <option value="143"> Writer 143</option>
162
+ <option value="144"> Writer 144</option>
163
+ <option value="145"> Writer 145</option>
164
+ <option value="146"> Writer 146</option>
165
+ <option value="147"> Writer 147</option>
166
+ <option value="148"> Writer 148</option>
167
+ <option value="149"> Writer 149</option>
168
+ <option value="150"> Writer 150</option>
169
+ <option value="151"> Writer 151</option>
170
+ <option value="152"> Writer 152</option>
171
+ <option value="153"> Writer 153</option>
172
+ <option value="154"> Writer 154</option>
173
+ <option value="155"> Writer 155</option>
174
+ <option value="156"> Writer 156</option>
175
+ <option value="157"> Writer 157</option>
176
+ <option value="158"> Writer 158</option>
177
+ <option value="159"> Writer 159</option>
178
+ <option value="160"> Writer 160</option>
179
+ <option value="161"> Writer 161</option>
180
+ <option value="162"> Writer 162</option>
181
+ <option value="163"> Writer 163</option>
182
+ <option value="164"> Writer 164</option>
183
+ <option value="165"> Writer 165</option>
184
+ <option value="166"> Writer 166</option>
185
+ <option value="167"> Writer 167</option>
186
+ <option value="168"> Writer 168</option>
187
+ <option value="169"> Writer 169</option>
188
+ <option value="170"> Writer 170</option>
189
+ <option value="171"> Writer 171</option>
190
+ <option value="172"> Writer 172</option>
191
+ <option value="173"> Writer 173</option>
192
+ <option value="174"> Writer 174</option>
193
+ <option value="175"> Writer 175</option>
194
+ <option value="176"> Writer 176</option>
195
+ <option value="177"> Writer 177</option>
196
+ <option value="178"> Writer 178</option>
197
+ <option value="179"> Writer 179</option>
198
+ <option value="180"> Writer 180</option>
199
+ <option value="181"> Writer 181</option>
200
+ <option value="182"> Writer 182</option>
201
+ <option value="183"> Writer 183</option>
202
+ <option value="184"> Writer 184</option>
203
+ <option value="185"> Writer 185</option>
204
+ <option value="186"> Writer 186</option>
205
+ <option value="187"> Writer 187</option>
206
+ <option value="188"> Writer 188</option>
207
+ <option value="189"> Writer 189</option>
208
+ <option value="190"> Writer 190</option>
209
+ <option value="191"> Writer 191</option>
210
+ <option value="192"> Writer 192</option>
211
+ <option value="193"> Writer 193</option>
212
+ <option value="194"> Writer 194</option>
213
+ <option value="195"> Writer 195</option>
214
+ <option value="196"> Writer 196</option>
215
+ <option value="197"> Writer 197</option>
216
+ <option value="198"> Writer 198</option>
217
+ <option value="199"> Writer 199</option>
218
+ <option value="200"> Writer 200</option>
219
+ <option value="201"> Writer 201</option>
220
+ <option value="202"> Writer 202</option>
221
+ <option value="203"> Writer 203</option>
222
+ <option value="204"> Writer 204</option>
223
+ <option value="205"> Writer 205</option>
224
+ <option value="206"> Writer 206</option>
225
+ <option value="207"> Writer 207</option>
226
+ <option value="208"> Writer 208</option>
227
+ <option value="209"> Writer 209</option>
228
+ <option value="210"> Writer 210</option>
229
+ <option value="211"> Writer 211</option>
230
+ <option value="212"> Writer 212</option>
231
+ <option value="213"> Writer 213</option>
232
+ <option value="214"> Writer 214</option>
233
+ <option value="215"> Writer 215</option>
234
+ <option value="216"> Writer 216</option>
235
+ <option value="217"> Writer 217</option>
236
+ <option value="218"> Writer 218</option>
237
+ <option value="250">Writer 250</option>
238
+ </select>
239
+ <input id="bias" type="range" min="0" max="100" value="70">
240
+ <button style="margin-left:20px;" class="button button2" id="btn" class="button7" disabled onclick="generate()">Loading model...</button>
241
+ <canvas id="hw-canvas">
242
+ </canvas>
243
+ </body>
244
+ </html>
model_weights.js ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var oReq = new XMLHttpRequest();
2
+ oReq.open("GET", "weights.bin", true);
3
+ oReq.responseType = "arraybuffer";
4
+
5
+ var weights_meta={'rnn/~/attention_core/~/gru__b': [[0, 1200], [1200]], 'rnn/~/attention_core/~/gru__w_h': [[1200, 481200], [400, 1200]], 'rnn/~/attention_core/~/gru__w_i': [[481200, 682800], [168, 1200]], 'rnn/~/attention_core/~/gru_1__b': [[682800, 684000], [1200]], 'rnn/~/attention_core/~/gru_1__w_h': [[684000, 1164000], [400, 1200]], 'rnn/~/attention_core/~/gru_1__w_i': [[1164000, 1845600], [568, 1200]], 'rnn/~/attention_core/~/gru_2__b': [[1845600, 1846800], [1200]], 'rnn/~/attention_core/~/gru_2__w_h': [[1846800, 2326800], [400, 1200]], 'rnn/~/attention_core/~/gru_2__w_i': [[2326800, 3008400], [568, 1200]], 'rnn/~/attention_core/~/linear__b': [[3008400, 3008430], [30]], 'rnn/~/attention_core/~/linear__w': [[3008430, 3025470], [568, 30]], 'rnn/~/conv1_d__b': [[3025470, 3025635], [165]], 'rnn/~/conv1_d__w': [[3025635, 3161760], [5, 165, 165]], 'rnn/~/embed__embeddings': [[3161760, 3246240], [512, 165]], 'rnn/~/embed_1__embeddings': [[3246240, 3273465], [165, 165]], 'rnn/~/linear__b': [[3273465, 3273586], [121]], 'rnn/~/linear__w': [[3273586, 3321986], [400, 121]]};
6
+
7
+ console.log(weights_meta);
8
+
9
+ var WEIGHTS = {};
10
+ var weight_buffer = null;
11
+ var W = null;
12
+ var w32 = null;
13
+ var w16 = null;
14
+
15
+ oReq.onload = function (oEvent) {
16
+ var arrayBuffer = oReq.response; // Note: not oReq.responseText
17
+ if (arrayBuffer) {
18
+ // convert bfloat16 to float32
19
+ // w16 = new Uint16Array(arrayBuffer)
20
+ // weight_buffer = new SharedArrayBuffer(2*arrayBuffer.byteLength);
21
+ // w32 = new Uint16Array(weight_buffer);
22
+ // for(var i=0; i < w16.length; i++) {
23
+ // w32[i * 2 + 1] = w16[i];
24
+ // }
25
+ W = new Float32Array(arrayBuffer);
26
+ document.getElementById("btn").innerText = "Buffer arrieved";
27
+
28
+ for(var k in weights_meta) {
29
+ info = weights_meta[k];
30
+ offset = info[0];
31
+ shape = info[1];
32
+ WEIGHTS[k] = tf.tensor(W.subarray(offset[0], offset[1]), shape);
33
+ }
34
+
35
+ document.getElementById("btn").disabled = false;
36
+ tf.engine().startScope();
37
+ setTimeout(function() {
38
+ cur_run = cur_run + 1;
39
+ dojob(cur_run);
40
+ }, 0);
41
+
42
+ document.getElementById("btn").innerText = "Generate";
43
+ }
44
+ };
45
+
46
+ tf.setBackend('wasm');
47
+
48
+ tf.ready().then( function() {
49
+ tf.enableProdMode();
50
+ oReq.send(null);
51
+ });
script.js ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ var log = console.log;
2
+ var ctx = null;
3
+ var canvas = null;
4
+ var RNN_SIZE = 400;
5
+ var VOCAB_SIZE = 165;
6
+ var NUM_ATT_HEADS=10;
7
+ var NUM_GMM_HEADS=20;
8
+ var cur_run = 0;
9
+ var scale_factor = 1.;
10
+
11
+ var randn = function() {
12
+ // Standard Normal random variable using Box-Muller transform.
13
+ var u = Math.random() * 0.999 + 1e-5;
14
+ var v = Math.random() * 0.999 + 1e-5;
15
+ return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
16
+ }
17
+
18
+ var rand_truncated_normal = function(low, high) {
19
+ while (true) {
20
+ r = randn();
21
+ if (r >= low && r <= high)
22
+ break;
23
+ // rejection sampling.
24
+ }
25
+ return r;
26
+ }
27
+
28
+ var softplus = function(x) {
29
+ const m = tf.maximum(x, 0.0);
30
+ return tf.add(m, tf.log(tf.add(tf.exp(tf.neg(m)), tf.exp(tf.sub(x, m)))));
31
+ }
32
+
33
+ var char2idx = {'\x00': 0, ' ': 1, '!': 2, '"': 3, '#': 4, '%': 5, '&': 6, "'": 7, '(': 8, ')': 9, '*': 10, ',': 11, '-': 12, '.': 13, '/': 14, '0': 15, '1': 16, '2': 17, '3': 18, '4': 19, '5': 20, '6': 21, '7': 22, '8': 23, '9': 24, ':': 25, ';': 26, '?': 27, 'A': 28, 'B': 29, 'C': 30, 'D': 31, 'E': 32, 'F': 33, 'G': 34, 'H': 35, 'I': 36, 'J': 37, 'K': 38, 'L': 39, 'M': 40, 'N': 41, 'O': 42, 'P': 43, 'Q': 44, 'R': 45, 'S': 46, 'T': 47, 'U': 48, 'V': 49, 'W': 50, 'X': 51, 'Y': 52, 'a': 53, 'b': 54, 'c': 55, 'd': 56, 'e': 57, 'f': 58, 'g': 59, 'h': 60, 'i': 61, 'j': 62, 'k': 63, 'l': 64, 'm': 65, 'n': 66, 'o': 67, 'p': 68, 'q': 69, 'r': 70, 's': 71, 't': 72, 'u': 73, 'v': 74, 'w': 75, 'x': 76, 'y': 77, 'z': 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};
34
+
35
+ var gru_core = function(input, weights, state, hidden_size) {
36
+ var [w_h,w_i,b] = weights;
37
+ var [w_h_z,w_h_a] = tf.split(w_h, [2 * hidden_size, hidden_size], 1);
38
+ var [b_z,b_a] = tf.split(b, [2 * hidden_size, hidden_size], 0);
39
+ gates_x = tf.matMul(input, w_i);
40
+ [zr_x,a_x] = tf.split(gates_x, [2 * hidden_size, hidden_size], 1);
41
+ zr_h = tf.matMul(state, w_h_z);
42
+ zr = tf.add(tf.add(zr_x, zr_h), b_z);
43
+ // fix this
44
+ [z,r] = tf.split(tf.sigmoid(zr), 2, 1);
45
+ a_h = tf.matMul(tf.mul(r, state), w_h_a);
46
+ a = tf.tanh(tf.add(tf.add(a_x, a_h), b_a));
47
+ next_state = tf.add(tf.mul(tf.sub(1., z), state), tf.mul(z, a));
48
+ return [next_state, next_state];
49
+ };
50
+
51
+
52
+ var generate = function() {
53
+ cur_run = cur_run + 1;
54
+ setTimeout(function() {
55
+ var counter = 2000;
56
+ tf.disposeVariables();
57
+
58
+ tf.engine().startScope();
59
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
60
+ ctx.beginPath();
61
+ dojob(cur_run);
62
+ }, 200);
63
+
64
+ return false;
65
+ }
66
+
67
+ var dojob = function(run_id) {
68
+ var text = document.getElementById("user-input").value;
69
+ if (text.length == 0) {
70
+ text = "Tất cả mọi người đều sinh ra có quyền bình đẳng.";
71
+ }
72
+
73
+
74
+ log(text);
75
+ original_text = text;
76
+ text = '' + text + ' ';
77
+
78
+ text = Array.from(text).map(function(e) {
79
+ return char2idx[e]
80
+ })
81
+ var text_embed = WEIGHTS['rnn/~/embed_1__embeddings'];
82
+ indices = tf.tensor1d(text, 'int32');
83
+ text = text_embed.gather(indices);
84
+
85
+ var embed = text;
86
+
87
+ var writer_embed = WEIGHTS['rnn/~/embed__embeddings'];
88
+ var e = document.getElementById("writers");
89
+ var wid = parseInt(e.value);
90
+ log(wid);
91
+
92
+ wid = tf.tensor1d([wid], 'int32');
93
+ wid = writer_embed.gather(wid);
94
+ embed = tf.add(wid, embed);
95
+
96
+
97
+
98
+ filter = WEIGHTS['rnn/~/conv1_d__w'];
99
+ embed = tf.conv1d(embed, filter, 1, 'same');
100
+ bias = tf.expandDims(WEIGHTS['rnn/~/conv1_d__b'], 0);
101
+ embed = tf.add(embed, bias);
102
+
103
+
104
+ // initial state
105
+ var gru0_hx = tf.zeros([1, RNN_SIZE]);
106
+ var gru1_hx = tf.zeros([1, RNN_SIZE]);
107
+ var gru2_hx = tf.zeros([1, RNN_SIZE]);
108
+
109
+ var att_location = tf.zeros([1, NUM_ATT_HEADS]);
110
+ var att_context = tf.zeros([1, VOCAB_SIZE]);
111
+
112
+ var input = tf.tensor([[0., 0., 1.]]);
113
+
114
+ gru0_w_h = WEIGHTS['rnn/~/attention_core/~/gru__w_h'];
115
+ gru0_w_i = WEIGHTS['rnn/~/attention_core/~/gru__w_i'];
116
+ gru0_bias = WEIGHTS['rnn/~/attention_core/~/gru__b'];
117
+
118
+ gru1_w_h = WEIGHTS['rnn/~/attention_core/~/gru_1__w_h'];
119
+ gru1_w_i = WEIGHTS['rnn/~/attention_core/~/gru_1__w_i'];
120
+ gru1_bias = WEIGHTS['rnn/~/attention_core/~/gru_1__b'];
121
+
122
+ gru2_w_h = WEIGHTS['rnn/~/attention_core/~/gru_2__w_h'];
123
+ gru2_w_i = WEIGHTS['rnn/~/attention_core/~/gru_2__w_i'];
124
+ gru2_bias = WEIGHTS['rnn/~/attention_core/~/gru_2__b'];
125
+
126
+ att_w = WEIGHTS['rnn/~/attention_core/~/linear__w'];
127
+ att_b = WEIGHTS['rnn/~/attention_core/~/linear__b'];
128
+ gmm_w = WEIGHTS['rnn/~/linear__w'];
129
+ gmm_b = WEIGHTS['rnn/~/linear__b'];
130
+
131
+ var ruler = tf.tensor([...Array(text.shape[0]).keys()]);
132
+ ruler = tf.expandDims(ruler, 1);
133
+ var bias = parseInt(document.getElementById("bias").value) / 100 * 3;
134
+
135
+
136
+ var cur_x = 20;
137
+ var cur_y = innerHeight / 2 + 30;
138
+ var path = [];
139
+ var dx = 0.;
140
+ var dy = 0;
141
+ var eos = 1.;
142
+ var counter = 0;
143
+
144
+
145
+ function loop(my_run_id) {
146
+ if (my_run_id < cur_run) {
147
+ tf.disposeVariables();
148
+ tf.engine().endScope();
149
+ return;
150
+ }
151
+
152
+ counter++;
153
+ if (counter < 2000) {
154
+ [att_location,att_context,gru0_hx,gru1_hx, gru2_hx, input] = tf.tidy(function() {
155
+ // Attention
156
+ const inp_0 = tf.concat([att_context, input], 1);
157
+ gru0_hx_ = gru0_hx;
158
+ [out_0,gru0_hx] = gru_core(inp_0, [gru0_w_h, gru0_w_i, gru0_bias], gru0_hx, RNN_SIZE);
159
+ tf.dispose(gru0_hx_);
160
+ const att_inp = tf.concat([att_context, input, out_0], 1);
161
+ const att_params = tf.add(tf.matMul(att_inp, att_w), att_b);
162
+ [alpha,beta,kappa] = tf.split(softplus(att_params), 3, 1);
163
+ att_location_ = att_location;
164
+ att_location = tf.add(att_location, tf.div(kappa, 25.));
165
+ tf.dispose(att_location_)
166
+
167
+ var phi = tf.sum(tf.mul(alpha, tf.exp(tf.div(tf.neg(tf.square(tf.sub(att_location, ruler))), beta))), 1);
168
+ phi = tf.expandDims(phi, 0);
169
+
170
+ att_context_ = att_context;
171
+ att_context = tf.sum(tf.mul(tf.expandDims(phi, 2), tf.expandDims(embed, 0)), 1)
172
+ tf.dispose(att_context_);
173
+
174
+ const inp_1 = tf.concat([input, out_0, att_context], 1);
175
+ // tf.dispose(input);
176
+ gru1_hx_ = gru1_hx;
177
+ [out_1,gru1_hx] = gru_core(inp_1, [gru1_w_h, gru1_w_i, gru1_bias], gru1_hx, RNN_SIZE);
178
+ tf.dispose(gru1_hx_);
179
+
180
+ const inp_2 = tf.concat([input, out_1, att_context], 1);
181
+ tf.dispose(input);
182
+ gru2_hx_ = gru2_hx;
183
+ [out_2, gru2_hx] = gru_core(inp_2, [gru2_w_h, gru2_w_i, gru2_bias], gru2_hx, RNN_SIZE);
184
+ tf.dispose(gru2_hx_);
185
+
186
+ // debugger;
187
+
188
+ // GMM
189
+ const gmm_params = tf.add(tf.matMul(out_2, gmm_w), gmm_b);
190
+ [x,y,logstdx,logstdy,angle,log_weight,eos_logit] = tf.split(gmm_params, [NUM_GMM_HEADS, NUM_GMM_HEADS, NUM_GMM_HEADS, NUM_GMM_HEADS, NUM_GMM_HEADS, NUM_GMM_HEADS, 1], 1);
191
+ // log_weight = tf.softmax(log_weight, 1);
192
+ // log_weight = tf.log(log_weight);
193
+ // log_weight = tf.mul(log_weight, 1. + bias);
194
+ const idx = tf.argMax(log_weight, 1).dataSync()[0];
195
+ // const idx = tf.multinomial(log_weight, 1).dataSync()[0];
196
+ x = x.dataSync()[idx];
197
+ y = y.dataSync()[idx];
198
+ const stdx = tf.exp(tf.sub(logstdx, bias)).dataSync()[idx];
199
+ const stdy = tf.exp(tf.sub(logstdy, bias)).dataSync()[idx];
200
+ angle = angle.dataSync()[idx];
201
+ e = tf.sigmoid(tf.mul(eos_logit, (1. + bias/5))).dataSync()[0];
202
+ const rx = rand_truncated_normal(-5, 5) * stdx;
203
+ const ry = rand_truncated_normal(-5, 5) * stdy;
204
+ x = x + Math.cos(-angle) * rx - Math.sin(-angle) * ry;
205
+ y = y + Math.sin(-angle) * rx + Math.cos(-angle) * ry;
206
+ if (Math.random() < e) {
207
+ e = 1.;
208
+ } else {
209
+ e = 0.;
210
+ }
211
+ input = tf.tensor([[x, y, e]]);
212
+ return [att_location, att_context, gru0_hx, gru1_hx, gru2_hx, input];
213
+ });
214
+
215
+ [dx,dy,eos_] = input.dataSync();
216
+ dy = -dy * 3. * scale_factor;
217
+ dx = dx * 3. * scale_factor;
218
+ if (eos == 0.) {
219
+ ctx.beginPath();
220
+ ctx.moveTo(cur_x, cur_y, 0, 0);
221
+ ctx.lineTo(cur_x + dx, cur_y + dy);
222
+ ctx.stroke();
223
+ }
224
+ eos = eos_;
225
+ cur_x = cur_x + dx;
226
+ cur_y = cur_y + dy;
227
+
228
+ if (att_location.dataSync()[0] < original_text.length + 1.5) {
229
+ setTimeout(function() {loop(my_run_id);}, 0);
230
+ }
231
+ }
232
+ }
233
+
234
+ loop(run_id);
235
+ }
236
+
237
+
238
+ window.onload = function(e) {
239
+ //Setting up canvas
240
+ canvas = document.getElementById("hw-canvas");
241
+ ctx = canvas.getContext("2d");
242
+ scale_factor = window.innerWidth / 1600;
243
+ ctx.canvas.width = window.innerWidth - 50;
244
+ ctx.canvas.height = window.innerHeight - 50;
245
+
246
+ }
style.css ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body {
2
+ padding: 2rem;
3
+ font-family: -apple-system, BlinkMacSystemFont, "Arial", sans-serif;
4
+ }
5
+
6
+ h1 {
7
+ font-size: 16px;
8
+ margin-top: 0;
9
+ }
10
+
11
+ p {
12
+ color: rgb(107, 114, 128);
13
+ font-size: 15px;
14
+ margin-bottom: 10px;
15
+ margin-top: 5px;
16
+ }
17
+
18
+ .card {
19
+ max-width: 620px;
20
+ margin: 0 auto;
21
+ padding: 16px;
22
+ border: 1px solid lightgray;
23
+ border-radius: 16px;
24
+ }
25
+
26
+ .card p:last-child {
27
+ margin-bottom: 0;
28
+ }
weights.bin ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5795596285bc683410be07ddc7eef5151070be863d91df5251f21996057273a3
3
+ size 13287944