-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathlayers.ts
704 lines (622 loc) · 21.1 KB
/
layers.ts
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
import {
Parameter,
Tensor,
randn,
zeros,
tril,
broadcast,
tensor,
exp,
rand,
ones,
sqrt,
mul,
log,
_reshape
} from "./tensor";
import fs from 'fs';
// Interface that contains all the types of Module's attributes:
interface ModuleInterface {
// Array of [key: values] of the properties of the Module:
[key: string]: Module | Parameter | Tensor | any;
parameters(): (Parameter | Tensor)[];
train(): void;
eval(): void;
entries(): [string, Module | Parameter | Tensor | any][];
mode: "train" | "eval";
}
// Module class:
export class Module implements ModuleInterface {
// Instantiate Module's learnable parameters:
[key: string]: Module | Parameter | Tensor | any;
// Instantiate Module's mode initially as "train":
mode: "train" | "eval" = "train";
/**
* Returns all model parameters in a list.
* @returns {object} List with parameters in the model.
*/
parameters(): (Parameter | Tensor)[] {
// Iterate over each item in this Module.
let params: (Parameter | Tensor)[] = [];
for (const [_, value] of this.entries()) {
// Add every Module, Parameter or Tensor with requires_grad set to True:
if (value instanceof Module) {
params = params.concat(value.parameters());
} else if (value instanceof Parameter) {
params.push(value);
} else if (value instanceof Tensor) {
if (value.requires_grad) {
params.push(value);
}
}
}
return params;
}
/**
* Sets module's mode to train, which influences layers like Dropout
*/
train() {
this.mode = "train";
for (const [_, param] of this.entries()) {
if (param instanceof Module) {
param.train();
}
}
}
/**
* Sets module's mode to eval, which influences layers like Dropout
*/
eval() {
this.mode = "eval";
for (const [_, param] of this.entries()) {
if (param instanceof Module) {
param.eval();
}
}
}
/**
* Returns an array of key/values of the enumerable properties of the Module
* @returns {object} List with parameters in the model.
*/
entries(): [string, Module | Parameter | Tensor | any][] {
return Object.entries(this);
}
}
// Standard Layers:
export class Linear extends Module {
public W: Tensor;
public b: Tensor;
public has_bias: boolean;
/**
* Simple linear layer, with weight matrix and optional bias. Does not contain nonlinearity.
*
* @param {number} in_size - size of the last dimention of the input array.
* @param {number} out_size - size of the last dimention of the output array.
* @param {string} device - Device to perform Tensor operations. Either "gpu" or "cpu".
* @param {boolean} bias - wether to include a bias term.
* @param {boolean} xavier - Wether to use xavier initialization (divide by square root of first input dimension).
*/
constructor(in_size: number, out_size: number, device = 'cpu', bias = true, xavier = true) {
super();
this.W = randn([in_size, out_size], true, device, xavier);
this.b = zeros([out_size], true);
this.has_bias = bias;
}
/**
* Performs forward pass through the Linear layer.
* @param {Tensor} x - input Tensor.
* @returns {Tensor} new Tensor. Out = (In @ W) + b.
*/
forward(x: Tensor): Tensor {
let z = x.matmul(this.W);
if (this.has_bias) {
z = z.add(this.b);
}
return z;
}
}
export class MultiHeadSelfAttention extends Module {
public Wk: Linear;
public Wq: Linear;
public Wv: Linear;
public residual_proj: Linear;
public mask: Tensor;
public att_dropout: Dropout;
public residual_dropout: Dropout;
public softmax: Softmax;
public H: number;
/**
* Full transformer Layer implementation.
*
* @param {number} in_size - size of the last dimention of the input array.
* @param {number} out_size - size of the last dimention of the output array.
* @param {number} n_heads - number of parallel heads to be computed (must equally divide in_size).
* @param {number} n_timesteps - length of text sequence to be processed bt Transformer.
* @param {number} dropout_prob - probability of zeroing each activation in dropout Layer.
* @param {string} device - Device to perform Tensor operations. Either "gpu" or "cpu".
*/
constructor(
in_size: number,
out_size: number,
n_heads: number,
n_timesteps: number,
dropout_prob = 0,
device = 'cpu'
) {
super();
this.Wk = new Linear(in_size, in_size, device, true, false);
this.Wq = new Linear(in_size, in_size, device, true, false);
this.Wv = new Linear(in_size, in_size, device, true, false);
this.residual_proj = new Linear(in_size, out_size, device, true, false);
this.mask = tril([n_timesteps, n_timesteps], false);
this.att_dropout = new Dropout(dropout_prob);
this.residual_dropout = new Dropout(dropout_prob);
this.softmax = new Softmax();
// Store head_size and verify that it's an integer:
this.H = in_size / n_heads;
if (in_size % n_heads != 0) {
throw new Error("Embedding dimension not divisible in equal heads.");
}
}
/**
* Performs Multi Head Self-Attention on "x" tensor.
* @param {Tensor} x - input Tensor.
* @returns {Tensor} new Tensor.
*/
forward(x: Tensor): Tensor {
const [B, T, D] = x.shape;
const H = this.H;
const nh = D / H; // Num heads
// Get key, queries and values from the input:
let k = this.Wk.forward(x); // (B, T, D) @ (D, D) -> (B, T, D)
let q = this.Wq.forward(x); // (B, T, D) @ (D, D) -> (B, T, D)
let v = this.Wv.forward(x); // (B, T, D) @ (D, D) -> (B, T, D)
// Reshape into different heads:
k = k.reshape([B, T, nh, H]).transpose(1, 2); // (B, T, D) -> (B, T, nh, H) -> (B, nh, T, H)
q = q.reshape([B, T, nh, H]).transpose(1, 2); // (B, T, D) -> (B, T, nh, H) -> (B, nh, T, H)
v = v.reshape([B, T, nh, H]).transpose(1, 2); // (B, T, D) -> (B, T, nh, H) -> (B, nh, T, H)
// Compute attention activation:
const kT = k.transpose(-2, -1);
let att = q.matmul(kT); // (B, nh, T, H) @ (B, nh, H, T) -> (B, nh, T, T)
// Reduce module before going into softmax:
att = att.div(H ** 2);
// Apply mask (to block out future characters), softmax, and dropout:
const mask = broadcast(this.mask, att);
att = att.masked_fill(mask, (el: number): boolean => el === 0, -Infinity);
att = this.softmax.forward(att, -1);
att = this.att_dropout.forward(att);
// Compute weighted sum between values:
let out = att.matmul(v); // (B, nh, T, T) @ (B, nh, T, H) -> (B, nh, T, H)
// Restack heads in D dimension:
out = out.transpose(1, 2).reshape([B, T, D]); // (B, nh, T, H) -> (B, T, D)
// Apply final projection (Dense layer) and dropout:
out = this.residual_proj.forward(out); // (B, T, D) @ (D, D) -> (B, T, D)
out = this.residual_dropout.forward(out);
return out;
}
}
export class FullyConnected extends Module {
public l1: Linear;
public relu: ReLU;
public l2: Linear;
public dropout: Dropout;
/**
* Small block composed of two Linear layers, a ReLU non-linearity and a Dropout layer.
*
* @param {number} in_size - size of the last dimention of the input array.
* @param {number} out_size - size of the last dimention of the output array.
* @param {number} dropout_prob - probability of zeroing each activation in dropout Layer.
* @param {string} device - Device to perform Tensor operations. Either "gpu" or "cpu".
* @param {boolean} bias - wether to include a bias term.
*/
constructor(in_size: number, out_size: number, dropout_prob = 0, device: string = 'cpu', bias: boolean = true) {
super();
this.l1 = new Linear(in_size, in_size * 2, device, true, bias);
this.relu = new ReLU();
this.l2 = new Linear(in_size * 2, out_size);
this.dropout = new Dropout(dropout_prob);
}
/**
* Passes "x" tensor through the Fully Connected layers.
* @param {Tensor} x - input Tensor.
* @returns {Tensor} new Tensor.
*/
forward(x: Tensor): Tensor {
let z = this.l1.forward(x);
z = this.relu.forward(z);
z = this.l2.forward(z);
z = this.dropout.forward(z);
return z;
}
}
export class Block extends Module {
public att: MultiHeadSelfAttention;
public ln1: LayerNorm;
public fcc: FullyConnected;
public ln2: LayerNorm;
/**
* Full transformer decoder block. Composed of Multi Head Self Attention, Fully connected layers and Layer Norms.
*
* @param {number} in_size - size of the last dimention of the input array.
* @param {number} out_size - size of the last dimention of the output array.
* @param {number} n_heads - number of parallel heads to be computed (must equally divide in_size).
* @param {number} n_timesteps - length of text sequence to be processed bt Transformer.
* @param {number} dropout_prob - probability of zeroing each activation in dropout Layer.
* @param {string} device - Device to perform Tensor operations. Either "gpu" or "cpu".
*/
constructor(
in_size: number,
out_size: number,
n_heads: number,
n_timesteps: number,
dropout_prob = 0,
device = 'cpu'
) {
super();
this.att = new MultiHeadSelfAttention(
in_size,
in_size,
n_heads,
n_timesteps,
dropout_prob,
device
);
this.ln1 = new LayerNorm(in_size);
this.fcc = new FullyConnected(in_size, out_size, dropout_prob, device, true);
this.ln2 = new LayerNorm(out_size);
}
/**
* Passes "x" tensor through a full transformer Block.
* @param {Tensor} x - input Tensor.
* @returns {Tensor} new Tensor.
*/
forward(x: Tensor): Tensor {
let z = x.add(this.att.forward(this.ln1.forward(x)));
//z = this.ln1.forward(z)
z = z.add(this.fcc.forward(this.ln2.forward(z)));
//z = this.ln2.forward(z);
return z;
}
}
// Embedding Layers
export class Embedding extends Module {
public E: Tensor;
/**
* Embedding class, turns indexes into vectors.
*
* @param {number} vocab_size - number of different indexes (vocabulary size).
* @param {number} embed_size - size of the embedding vector generated.
*/
constructor(vocab_size: number, embed_size: number) {
super();
this.E = randn([vocab_size, embed_size], true, 'cpu', false);
}
/**
* Extracts embedding from rows in "idx":
* @param {Tensor} idx - rows to get embedding from.
* @returns {Tensor} new Tensor. Out = (In @ W) + b.
*/
forward(idx: Tensor): Tensor {
// Get idx dimensions:
const [B, T] = idx.shape;
let x = this.E.at(idx);
// Assure output tensor has desired shape:
x = x.reshape([B, T, this.E.shape[1]]);
return x;
}
}
export class PositionalEmbedding extends Module {
public E: Tensor;
/**
* Embedding class, turns indexes into vectors based on it's position through an optimized lookup table.
*
* @param {number} input_size - number of different embeddings (size of the input).
* @param {number} embed_size - size of the embedding vector generated.
*/
constructor(input_size: number, embed_size: number) {
super();
this.E = randn([input_size, embed_size], true, 'cpu', false);
}
/**
* Gets embedding for timesteps in "idx" array.
* @param {object} idx - Array [Batch x Timesteps]. Timesteps will be filled with positional embeddings.
* @returns {Tensor} new Tensor.
*/
forward(idx: Tensor): Tensor {
// Get num_timesteps dimension:
const [_, T] = idx.shape;
// Creates positional embeddings: (Batch, Timesteps) => (Batch, Timesteps, Embed)
const x = this.E.at([...Array(T).keys()]);
return x;
}
}
// Non-linearity Layers:
export class ReLU extends Module {
/**
* Rectified Linear Unit nonlinearity. Returns z if z>0 else 0.
*/
constructor() {
super();
}
/**
* Performs forward pass through Rectified Linear Unit nonlinearity. Returns z if z>0 else 0.
* @param {Tensor} z - input Tensor.
* @returns {Tensor} new Tensor.
*/
forward(z: Tensor): Tensor {
// Define recursive function:
function _relu(z: Array<any>): Array<any> {
// Base case, perform ReLU:
if (typeof z[0] === "number") {
return z.map((el: number): number => {
if (el > 0) {
return 1.0;
} else {
return 0.001;
}
});
// Recursive case, go deeper in array:
} else if (typeof z[0] === "object") {
return z.map((el: Array<any>): Array<any> => _relu(el));
} else throw Error("In ReLU, provided Tensor is not homogenous.");
}
const mask = tensor(_relu(z._data));
z = z.mul(mask);
return z;
}
}
export class Softmax extends Module {
/**
* Softmax nonlinearity class. Returns distribution of values (sum=1).
*/
constructor() {
super();
}
/**
* Performs forward pass through Softmax nonlinearity.
* @param {Tensor} z - input Tensor.
* @param {number} dim - dimension across which to apply Softmax.
* @returns {Tensor} new Tensor.
*/
forward(z: Tensor, dim = -1): Tensor {
z = exp(z);
const out = z.div(z.sum(dim, true));
return out;
}
}
// Regularization Layers:
export class Dropout extends Module {
public p: number;
/**
* Dropout class, added usually after other layers, to drop values to zero with given probability
*
* @param {number} drop_prob - probability to drop each value in input.
*/
constructor(drop_prob: number) {
super();
this.p = drop_prob;
this.mode = "train";
}
/**
* Performs forward pass through Dropout layer. Sets random values to zero (this.p % of the total).
* @param {Tensor} z - input Tensor.
* @returns {Tensor} new Tensor.
*/
forward(z: Tensor): Tensor {
if (this.mode == "eval") {
return z;
}
const mask = rand(z.shape);
// Set to zero all values of uniform distribution lower than probability of dropout:
let a = z.masked_fill(
mask,
(el: number): boolean => {
return el < this.p;
},
0
);
// Scale modulus by probability during training time:
a = a.div(1 - this.p);
return a;
}
}
export class LayerNorm extends Module {
public gamma: Tensor;
public beta: Tensor;
/**
* Layer Norm class, added usually after other layers to normalize across all of the output.
*
* @param {number} n_embed - size of the last dimention of the input.
*/
constructor(n_embed: number) {
super();
this.gamma = ones([n_embed], true);
this.beta = zeros([n_embed], true);
}
forward(x: Tensor): Tensor {
const var_x = x.variance(-1, true); // (B, T)
const norm_x = x.sub(x.mean(-1, true)).div(sqrt(var_x)); // (B, T, D)
const z = mul(norm_x, this.gamma).add(this.beta); // (B, T, D)
return z;
}
}
// Loss layers:
export class CrossEntropyLoss extends Module {
/**
* Cross Entropy Loss class, returns the loss given the output and the expected indexes.
*/
constructor() {
super();
}
/**
* Performs forward pass through CrossEntropyLoss, returns loss.
* @param {Tensor} z - Output from the last layer of the network. Must have shape like (*Batch dimentions, Number of possible classes).
* @param {object} y - Correct indexes expected from the model.
* @returns {object} Negative-log-likelihood loss of the model output.
*/
forward(z: Tensor, y: Tensor): Tensor {
// Get data's shape:
let zDims = z.shape;
// Get last dimension:
const D = zDims.slice(zDims.length - 1, zDims.length)[0];
// Get product of all batch dimensions:
zDims = zDims.slice(0, zDims.length - 1);
const B = zDims.reduce((a, b) => a * b, 1);
// Flatten out the batch dimensions:
z = z.reshape([B, D]);
// Perform softmax on output:
const logitsExp = exp(z);
const logitsSum = logitsExp.sum(1, true);
const logits = logitsExp.div(logitsSum);
const y_array = _reshape(y.data, [B]);
// Get cross-entropy loss:
const at_logits = logits.at([...Array(B).keys()], y_array);
const log_losses = log(at_logits);
let loss = log_losses.sum(-1).neg();
loss = loss.div(B);
return loss;
}
}
/**
* Mean Squared Error Loss class, returns the loss given the network output and the expected output.
*/
export class MSELoss extends Module {
/**
* Constructor.
*/
constructor() {
super();
}
/**
* Performs forward pass through MSELoss, returns loss.
* @param {Tensor} z - Output from the last layer of the network.
* @param {object} y - Correct outputs expected from the model.
* @returns {object} Mean Squared Error loss of the model output.
*/
forward(z: Tensor, y: Tensor): Tensor {
// Get data's shape:
let zDims = z.shape;
// Get last dimension:
const D = zDims.slice(zDims.length - 1, zDims.length)[0];
// Get product of all batch dimensions:
zDims = zDims.slice(0, zDims.length - 1);
const B = zDims.reduce((a, b) => a * b, 1);
// Flatten out the batch dimensions:
z = z.reshape([B, D]);
y = y.reshape([B, D]);
const S = z.sub(y);
const P = S.pow(2);
const Su = P.sum();
let loss = Su.mean();
loss = loss.div(B);
return loss;
}
}
export class Conv2D extends Module {
constructor(
in_channels, out_channels, kernel_size, stride = 1, padding = "same",
dilation = 1, groups = 1, bias = true, device = "cpu"
) {
super();
const [kh, kw] = Array.isArray(kernel_size) ? kernel_size : [kernel_size, kernel_size];
const [sh, sw] = Array.isArray(stride) ? stride : [stride, stride];
const [dh, dw] = Array.isArray(dilation) ? dilation : [dilation, dilation];
let ph, pw;
if (padding === "same") {
ph = Math.floor(((kh - 1) * dh + 1 - sh) / 2);
pw = Math.floor(((kw - 1) * dw + 1 - sw) / 2);
} else if (Array.isArray(padding)) {
[ph, pw] = padding;
} else {
ph = pw = padding;
}
const weight_shape = [out_channels, Math.floor(in_channels / groups), kh, kw];
this.W = randn(weight_shape, true, device, false);
this.b = bias ? zeros([out_channels], true) : null;
this.has_bias = bias;
this.stride = [sh, sw];
this.padding = [ph, pw];
this.dilation = [dh, dw];
this.groups = groups;
}
forward(x) {
const [kernel_height, kernel_width] = [this.W.shape[2], this.W.shape[3]];
const [batch, out_channels] = [x.shape[0], this.W.shape[0]];
const out_height = Math.floor((x.shape[2] + 2 * this.padding[0] - kernel_height) / this.stride[0]) + 1;
const out_width = Math.floor((x.shape[3] + 2 * this.padding[1] - kernel_width) / this.stride[1]) + 1;
x = x.img2col(kernel_height, kernel_width, this.stride, this.padding);
let reshaped_weights = this.W.reshape([this.W.shape[0], this.W.shape[1] * kernel_height * kernel_width]).transpose(0, 1);
x = x.matmul(reshaped_weights);
x = x.reshape([batch, out_channels, out_height, out_width]);
if (this.has_bias && this.b) {
x = x.add(this.b);//not sure bias is working correctly
}
return x;
}
}
export class MaxPool2D extends Module {
public kernel_size: [number, number];
public stride: [number, number];
constructor(kernel_size: number | [number, number], stride?: number | [number, number]) {
super();
this.kernel_size = Array.isArray(kernel_size) ? kernel_size : [kernel_size, kernel_size];
this.stride = stride ? (Array.isArray(stride) ? stride : [stride, stride]) : this.kernel_size;
}
forward(x: Tensor): Tensor {
x=x.maxpool(this.kernel_size,this.stride);
return x;
}
}
/**
* Saves the model to a JSON file.
* @param {Module} model - Model to be saved in JSON file.
* @param {string} file - JSON file.
*/
export function save(model: Module, file: string) {
/**
* Filters object, returning 'null' instead of 'value' for certain keys.
* @param {object} obj - Objects with keys and values that we have to filter.
* @returns {object} Filtered object.
*/
function recursiveReplacer(obj: { [key: string]: any; }): { [key: string]: any; }{
let result: { [key: string]: any; } = {};
for (var x in obj) {
if (x !== "forwardKernel" && x !== "backwardKernelA" && x !== "backwardKernelB" && x !== "gpu") {
if (typeof obj[x] === 'object' && !Array.isArray(obj[x])) {
result[x] = recursiveReplacer(obj[x]);
} else {
result[x] = obj[x];
}
} else {
result[x] = null;
}
}
return result
}
const data = JSON.stringify(recursiveReplacer(model));
fs.writeFileSync(file, data);
}
/**
* Loads a model from a JSON file.
* @param {Module} model - Blank model to load weights into (placeholder). Needs to be identical to model.
* @param {string} file - JSON file.
* @returns {Module} loadedModel - Model to be loaded from JSON file.
*/
export function load(model: Module, file: string): Module {
const loadedData = fs.readFileSync(file);
let loadedModel = JSON.parse(loadedData.toString());
loadParameters(loadedModel, model)
return model;
}
function loadParameters(source: Module, target: Module) {
for (const [key, value] of target.entries()) {
// Add every Module, Parameter or Tensor with requires_grad set to True:
if (value instanceof Module) {
loadParameters(source[key], target[key]);
} else if (value instanceof Parameter || value instanceof Tensor) {
target[key]._data = source[key]._data;
target[key].m = source[key].m;
target[key].v = source[key].v;
}
}
}