-
Notifications
You must be signed in to change notification settings - Fork 3
/
polstm.q
345 lines (291 loc) · 7.69 KB
/
polstm.q
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/ Check sampler
/ Xavier gdist
/ Check sampling of H
tanh:{((exp(x)-exp(-1*x)))%((exp(x)+exp(-1*x)))};
sigmoid:{1%(1+exp neg x)}
/ Util function fo 2d arrays of fixed size
k)shape:{(#x),(#x[0])}
/ Gaussian, mean 0 stdev 0.1 - 1000 numbers
GDIST:"F"$trim "," vs raze read0 `gdist.txt;
/ sampler[] samples C numbers from a MASTER list
MASTER:til count GDIST;DATA:GDIST;FLIST:(); /vars for SAMPLER
SAMPLER:{[MASTER;C]
while[C>count FLIST;
SMPL:distinct (floor (count MASTER)%2)?(MASTER);
$[0=count FLIST;
FLIST::DATA[SMPL];
FLIST::FLIST,DATA[SMPL]
];
m:MASTER[where not MASTER in SMPL]
];
:0.01*FLIST[til C]
};
/ Read text input - shakespearean text
k:read0 `:cmplshake.txt;
/ Remove multiple spaces, only one space.
cleanText:{x where(or)':[not " "=x]};
TXT:" " sv cleanText over 'k;
/TXT:" " sv k[where (count each k="\"")>0];
CHARS: distinct TXT;
VOCAB_SIZE:count CHARS;
P:0;
I:0;
/ Utility methods - characters to index, and vice versa
CHAR_TO_IX:CHARS, 'til count CHARS;
IX_TO_CHARS:(til count CHARS),'CHARS;
SEQLEN:25; / For now
ESZ:VOCAB_SIZE;
HSZ:25;
OSZ:VOCAB_SIZE;
/ Softmax variables
SMPREDS:();
SMX:();
SMTARGETS:();
SMT:0;
SMW:(OSZ,HSZ)#SAMPLER[MASTER;1000]; /osz,hsz
SMDW:(OSZ,HSZ)#SAMPLER[MASTER;1000]; /osz,hsz
/**************************L*S*T*M******************************************/
LWH:((HSZ,HSZ)#SAMPLER[MASTER;1000];
(HSZ,HSZ)#SAMPLER[MASTER;1000];
(HSZ,HSZ)#SAMPLER[MASTER;1000];
(HSZ,HSZ)#SAMPLER[MASTER;1000]); /i,f,o,j
LWX:((HSZ,ESZ)#SAMPLER[MASTER;1000];
(HSZ,ESZ)#SAMPLER[MASTER;1000];
(HSZ,ESZ)#SAMPLER[MASTER;1000];
(HSZ,ESZ)#SAMPLER[MASTER;1000]);
LB:((HSZ)#0f;(HSZ)#0f;(HSZ)#0f;(HSZ)#0f); / bi;bf;bo;bj
LDB:((HSZ)#0f;(HSZ)#0f;(HSZ)#0f;(HSZ)#0f); / bi;bf;bo;bj
LDWX:((HSZ,ESZ)#0f;
(HSZ,ESZ)#0f;
(HSZ,ESZ)#0f;
(HSZ,ESZ)#0f);
LDWH:((HSZ,HSZ)#0f;
(HSZ,HSZ)#0f;
(HSZ,HSZ)#0f;
(HSZ,HSZ)#0f);
LSTMX:(1 1)#"0";
LSTMT:0; / time pointer in LSTM
LSTMH:(1,HSZ)#0.0;
LSTMC:(1,HSZ)#0.0;
LSTMCT:(1 1)#"0";
/all gates here
LSTMIG:(1 1)#"0";
LSTMFG:(1 1)#"0";
LSTMOG:(1 1)#"0";
LSTMCUPD:(1 1)#"0";
LDHPREV:(1,HSZ)#0;
LDCPREV:(1,HSZ)#0;
GETLOSS:{[DUMMY]A::SMTARGETS,'reverse SMPREDS;:sum {neg log(A[x][A[x;0]+1])}each til count SMTARGETS};
LSTMFW:{[XT;SF] L:0;
LSTMT::LSTMT+1;
T:LSTMT; H:"f"$LSTMH[T-1];
K:raze H;K[where K=0f]:0.0;H:(shape H)#K;
ki:sigmoid[((LWH[0]$H)+(LWX[0]$XT)+LB[0])];
ki[where raze ki<-1e-25]:-1e-25;
kf:sigmoid[((LWH[1]$H)+(LWX[1]$XT)+LB[1])];
kf[where raze kf<-1e-25]:-1e-25;
ko:sigmoid[((LWH[2]$H)+(LWX[2]$XT)+LB[2])];
ko[where raze ko<-1e-25]:-1e-25;
TMP:(LWH[3]$H)+(LWX[3]$XT)+LB[3];
ku:tanh[TMP];
ku[where raze ku<-1e-25]:-1e-25;
if[0=SF;
LSTMIG::(LSTMIG, enlist ki);
LSTMFG::(LSTMFG, enlist kf);
LSTMOG::(LSTMOG, enlist ko);
LSTMCUPD::(LSTMCUPD, enlist ku);
];
TMPC:(raze ki*ku)+(raze kf)*LSTMC[T-1];
TMPC[where raze TMPC<1e-25]:-1e-25;
TMPCT:tanh[TMPC];
TMPCT[where raze TMPCT<1e-25]:-1e-25;
TMPH:ko*TMPCT;
t:raze TMPH;
t[where t<-1e-25]:-1e-25;
TMPH:(HSZ,1)#t;
/ TMPH[where raze TMPH<1e-25]:-1e-25;
if[0=SF;
LSTMC::(LSTMC, enlist TMPC);
LSTMCT::(LSTMCT, enlist TMPCT);
LSTMH::(LSTMH, enlist TMPH);
LSTMX::(LSTMX, enlist raze XT);
];
:LSTMH[T]
};
LSTMBW:{[DH] T:LSTMT;
DH:DH+ LDHPREV;
TMP:raze LSTMCT[T];
DC:raze ((1-TMP*TMP)*LSTMOG[T]* flip DH)+raze LDCPREV;
TMP:LSTMIG[T];
DINPUT:raze (TMP*(1-TMP))*LSTMCUPD[T]*DC;
TMP:LSTMFG[T];
DFORGET:raze (TMP*(1-TMP))*LSTMC[T-1]*DC;
TMP:LSTMOG[T];
DOUTPUT:raze (TMP*(1-TMP))*LSTMCT[T]*flip DH;
TMP:LSTMCUPD[T];
DUPDATE:raze (1-TMP*TMP)*LSTMIG[T]*DC;
LDCPREV::flip (HSZ,1)#LSTMFG[T]*DC;
/ show shape LDCPREV;
LDB[0]::LDB[0]+DINPUT;
LDB[1]::LDB[1]+DFORGET;
LDB[2]::LDB[2]+DOUTPUT;
LDB[3]::LDB[3]+DUPDATE;
HIN:LSTMH[T-1];
LDWX[0]::LDWX[0]+(DINPUT*/:\: LSTMX[T]);
LDWX[1]::LDWX[1]+(DFORGET*/:\: LSTMX[T]);
LDWX[2]::LDWX[2]+(DOUTPUT*/:\: LSTMX[T]);
LDWX[3]::LDWX[3]+(DUPDATE*/:\: LSTMX[T]);
LDWH[0]+::(DINPUT*/:\: raze HIN);
LDWH[1]+::(DFORGET*/:\: raze HIN);
LDWH[2]+::(DOUTPUT*/:\: raze HIN);
LDWH[3]+::(DUPDATE*/:\: raze HIN);
LDHPREV::(1,HSZ)#(LWH[0]$DINPUT);
LDHPREV::LDHPREV+(1,HSZ)#(LWH[1]$DFORGET);
LDHPREV::LDHPREV+(1,HSZ)#(LWH[2]$DOUTPUT);
LDHPREV::LDHPREV+(1,HSZ)#(LWH[3]$DUPDATE);
dX:(flip LWX[0])$DINPUT;
dX:dX+(flip LWX[1])$DFORGET;
dX:dX+(flip LWX[2])$DOUTPUT;
dX:dX+(flip LWX[3])$DUPDATE;
LSTMT::LSTMT-1;
:dX
};
INITLAYER:{[L]
LSTMT::0;
LSTMX::"0";
LSTMCT::"0";
LSTMIG::"0";
LSTMFG::"0";
LSTMOG::"0";
LSTMCUPD::"0";
LDHPREV::(1,HSZ)#0;
LDCPREV::(1,HSZ)#0;
LSTMH::(1,HSZ)#0.0;
LSTMC::(1,HSZ)#0.0;
/ LDB::((1,HSZ)#0f; (1,HSZ)#0f; (1,HSZ)#0f; (1,HSZ)#0f); / bi;bf;bo;bj
LDB::((HSZ)#0f;(HSZ)#0f;(HSZ)#0f;(HSZ)#0f); / bi;bf;bo;bj
LDWX[;;]::0.0;
LDWH[;;]::0.0;
SMPREDS::();
SMX::();
SMTARGETS::();
SMT::0;
SMDW::(OSZ,HSZ)#0f
};
/
softmax forward pass
\
SOFTMAXFW:{[IX;SF] SMT::SMT+1;
K:raze IX;K[where K=0f]:0.0;IX:(shape IX)#K;
YY:raze SMW$IX;
YY:exp(YY-max YY);
YY:YY%sum YY;
if[0=SF;
$[0=count SMPREDS;SMPREDS::(1,OSZ)#YY;SMPREDS::(SMPREDS, enlist YY)];
$[0=count SMX;SMX::(1,HSZ)#IX;SMX::(SMX, enlist IX)];
];
:neg log SMPREDS[(SMT-1);TARGETS[SMT-1;1]]};
/ :YY};
/
softmax backward pass
\
SOFTMAXBW:{[i] SMT::SMT-1;
$[0=count SMTARGETS;SMTARGETS::i;SMTARGETS::(SMTARGETS, enlist i)];
TMPX:raze over ((HSZ,1)#SMX[SMT]);
TMPD:raze over ((1,OSZ)#SMPREDS[SMT]);
TMPD[i]:TMPD[i]-1;
SMDW::SMDW+(TMPD */:\: TMPX);
DELTA:(1,HSZ)#(flip SMW)$TMPD;
:DELTA
};
NORMALIZEGRADS:{[N]LDWX::LDWX%N;LDWH::LDWH%N;SMDW::SMDW%N};
TAKESTEP:{[LR]LWX::LWX-LR*LDWX;LWH::LWH-LR*LDWH;{tx:raze LDWX[x];th:raze LDWH[x];th[where th<-1e-50]:-1e-50;tx[where tx<-1e-50]:-1e-50;LDWH[x]:(shape LDWH[x])#th;LDWX[x]:(shape LDWX[x])#tx;}each til count LDWX;LB::LB-LR*LDB;SMW::SMW-LR*SMDW};
LR:0.1;
CLIPGRAD:5.0;
XS:();
TRAIN:{[INPUT;TARGETS]
T:0;
COST:0.0;
SF:0;
INITLAYER[0];
/ Forward
while[T<count INPUT;
XS::raze (1,VOCAB_SIZE)#0;
XS[INPUT[T][1]]:1;
H:LSTMFW[("f"$(VOCAB_SIZE,1)#XS);SF];
K:raze H;K[where K=0f]:0.0;H:(shape H)#K;
/TMP:(SOFTMAXFW[H;SF])[[TARGETS[T];0]];
TMP:SOFTMAXFW[H;SF];
COST+:TMP;
T+:1;
];
/ Backward
T:T-1;
while [T>0;
H:SOFTMAXBW[TARGETS[T][1]];
H:LSTMBW[H];
T:T-1;
];
/ Normalize and SGD
GRADNORM:sqrt((sum over LDWX xexp 2)+(sum over LDWH xexp 2)+(sum over SMDW xexp 2));
if[GRADNORM>CLIPGRAD;NORMALIZEGRADS(GRADNORM%CLIPGRAD)];
TAKESTEP[LR];
/show GRADNORM;
/show "Cost = ";
/show COST;
:COST
};
SAMPLE:{[SEED;N]
XT:"f"$raze (1,VOCAB_SIZE)#0;
XT[SEED]:1.0;
I:0;
KIX:();
SF:1;
show "Sampling...";
/ INITLAYER[0];
while[I<N;
/ H:LSTMFW["f"$(VOCAB_SIZE,1)#XT;SF];
/ H:SOFTMAXFW[H;SF];
/ Pass through the entire LSTM to get new H
H:LSTMH[LSTMT];
t:sigmoid[(LWH[til 3]$\:H)+(LWX[til 3]$\:XT)];
s:tanh[((LWX[3]$XT)+(LWH[3]$H))];
H:t[2]*tanh((t[0]*s)+t[1]*LSTMC[LSTMT]);
/ Now softmax to get final preds
Y:raze SMW$H;
H:(exp Y)%(sum exp Y);
XT:"f"$raze (1,VOCAB_SIZE)#0;
INDEX:sum 1?(idesc H)[til 10];
XT[INDEX]:1f;
$[0=count KIX;KIX:INDEX;KIX:KIX,INDEX];
I+:1;
];
:KIX;
};
I:0;
INITLAYER[0];
/ SMOOTHLOSS:(neg log (1.0%VOCAB_SIZE)*SEQLEN);
while [I<1000000;
if[(I=0) or ((P+SEQLEN+1)>=count TXT);P:0;LSTMC:(1,HSZ)#0.0;LSTMH:(1,HSZ)#0.0];
/ Start sending batches of chars to the LSTM
INPUT:TXT[P+til SEQLEN];
INPUT:INPUT,'(CHARS?INPUT);
TARGETS:TXT[(P+1)+til SEQLEN];
TARGETS:TARGETS,'(CHARS?TARGETS);
COST:TRAIN[INPUT;TARGETS];
/ SMOOTHLOSS:SMOOTHLOSS*0.999+COST*0.001;
/ show "Smooth loss = ";
/ show SMOOTHLOSS;
/show "Counter = ";
if[0=I mod 10;show I];
I+:1;
P+:SEQLEN;
/Sample from NN periodically
if[0=I mod 100;
SAMPLEIX:SAMPLE[INPUT[sum 1?count INPUT][1];200];
/show GETLOSS[0];
show (COST;IX_TO_CHARS[SAMPLEIX;1]);
INITLAYER[0];
];
/show IX_TO_CHARS[-1+SAMPLEIX;1];
];