-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.js
257 lines (214 loc) · 6.63 KB
/
Matrix.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
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
function sigmoid(x) {
return 1 / (1 + Math.exp(-x));
}
function DSigmoid(y) {
return y * (1 - y);
}
function ReLU(x) {
if (x <= 0) return 0;
if (x >= 255) return 255;
return x;
}
function DReLU(y) {
if (y <= 0) return 0;
return 1;
}
function crossEntropyLoss(truth, pred) {
let reciPred = Matrix.reciprocal(pred);
let loss = Matrix.hadamardMult(reciPred, truth);
loss.scalarMult(-1);
return loss;
}
function normalizeArr(arr) {
let sum = arr.reduce((a, b) => a + b);
if (sum == 0) {
sum = 1;
}
let newArr = arr.map((a) => a / sum);
return newArr;
}
class Matrix {
constructor(width, height, init = true) {
this.matrix = [];
this.width = width;
this.height = height;
if (init) {
for (let i = 0; i < width * height; i++) {
this.matrix[i] = 0;
}
}
}
static matrixSum(a, b) {
if (a.width != b.width || a.height != b.height) {
console.error("Matrices are not compatible for sum.");
}
let summed = new Matrix(a.width, a.height);
for (let i = 0; i < a.matrix.length; i++) {
summed.matrix[i] = a.matrix[i] + b.matrix[i];
}
return summed;
}
static matrixSub(a, b) {
if (a.width != b.width || a.height != b.height) {
console.error("Matrices are not compatible for subtraction.");
}
let subbed = new Matrix(a.width, a.height, false);
for (let i = 0; i < a.matrix.length; i++) {
subbed.matrix[i] = a.matrix[i] - b.matrix[i];
}
return subbed;
}
static hadamardMult(a, b) {
if (a.width != b.width || a.height != b.height) {
console.error("Matrices are not compatible for subtraction.");
}
let hadaMult = new Matrix(a.width, a.height, false);
for (let i = 0; i < a.matrix.length; i++) {
hadaMult.matrix[i] = a.matrix[i] * b.matrix[i];
}
return hadaMult;
}
static matrixMult(a, b) {
if (a.width != b.height) {
console.error("Matrices are not compatible for cross product.");
}
let crossed = new Matrix(b.width, a.height, false);
for (let i = 0; i < a.height; i++) {
let aRow = a.matrix.slice(i * a.width, (i + 1) * a.width);
for (let j = 0; j < b.width; j++) {
let bCol = [];
for (let k = j; k < b.matrix.length; k += b.width) {
bCol.push(b.matrix[k]);
}
let prod = aRow.map((x, i) => x * bCol[i]);
let sum = prod.reduce((a, b) => a + b);
crossed.matrix[j + i * b.width] = sum;
}
}
return crossed;
}
static transpose(a) {
let transposed = new Matrix(a.height, a.width, false);
for (let i = 0; i < a.width; i++) {
for (let j = 0; j < a.height; j++) {
let aInd = i + j * a.width;
transposed.matrix.push(a.matrix[aInd]);
}
}
return transposed;
}
static toArray(m) {
return m.matrix.slice(0, m.matrix.length);
}
static fromArray(arr, w, h) {
if (w * h != arr.length) {
console.error("Cannot convert array to matrix of the provided dimensions.");
return;
}
let m = new Matrix(w, h, false);
m.matrix = arr.slice(0, arr.length);
return m;
}
static copyMatrix(a) {
let copied = new Matrix(a.width, a.height, false);
for (let i = 0; i < a.matrix.length; i++) {
copied.matrix[i] = a.matrix[i];
}
return copied;
}
static matrixMap(a, f) {
let mapped = new Matrix(a.width, a.height, false);
for (let i = 0; i < a.matrix.length; i++) {
mapped.matrix[i] = f(a.matrix[i]);
}
return mapped;
}
static reciprocal(a) {
let reci = new Matrix(a.width, a.height, false);
for (let i = 0; i < a.matrix.length; i++) {
reci.matrix[i] = 1 / a.matrix[i];
if (a.matrix[i] == 0) {
reci.matrix[i] = 0;
}
}
return reci;
}
randomize() {
for (let i = 0; i < this.matrix.length; i++) {
this.matrix[i] = Math.random() * 2 - 1;
}
}
scalarAdd(n) {
for (let i = 0; i < this.matrix.length; i++) {
this.matrix[i] += n;
}
}
scalarMult(n) {
for (let i = 0; i < this.matrix.length; i++) {
this.matrix[i] *= n;
}
}
matrixSum(b) {
if (b.width != this.width || b.height != this.height) {
console.error("Matrices not compatible for addition.");
return;
}
for (let i = 0; i < this.matrix.length; i++) {
this.matrix[i] += b.matrix[i];
}
}
matrixSub(b) {
if (b.width != this.width || b.height != this.height) {
console.error("Matrices not compatible for addition.");
return;
}
for (let i = 0; i < this.matrix.length; i++) {
this.matrix[i] -= b.matrix[i];
}
}
mutate(mr) {
for (let i = 0; i < this.matrix.length; i++) {
this.matrix[i] += randomGaussian(0, mr);
}
}
rotate() {
let rotated = new Matrix(this.height, this.width, false);
for (let i = 0; i < this.width; i++) {
for (let j = this.height - 1; j >= 0; j--) {
rotated.matrix.push(this.matrix[i + j * this.width]);
}
}
return rotated;
}
rotate180() {
let rotated180 = new Matrix(this.width, this.height, false);
rotated180.matrix = this.matrix.slice(0, this.matrix.length).reverse();
return rotated180;
}
clip(min, max) {
for (let i = 0; i < this.matrix.length; i++) {
this.matrix[i] = Math.min(max, Math.max(min, this.matrix[i]));
}
}
avg() {
return this.matrix.reduce((a, b) => a + b) / this.matrix.length;
}
softmax() {
if (this.matrix.length == 1) {
return;
}
let max = Math.max(...this.matrix);
this.scalarAdd(-max);
let exponential = this.matrix.map(Math.exp);
let sumOfExp = this.matrix.reduce((a, b) => a + b);
if (sumOfExp == 0) {
sumOfExp = 1; //do normal normalization if sum is 0?
}
this.scalarMult(1 / sumOfExp);
}
static deserialize(matrixObj) {
let m = new Matrix(matrixObj.width, matrixObj.height, false);
m.matrix = matrixObj.matrix;
return m;
}
}