catniff 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/autograd.d.ts +5 -1
- package/dist/autograd.js +20 -2
- package/dist/tensor.d.ts +2 -0
- package/dist/tensor.js +38 -0
- package/package.json +8 -2
package/README.md
CHANGED
|
@@ -26,7 +26,7 @@ console.log(x.grad); // 5
|
|
|
26
26
|
|
|
27
27
|
Tensors in Catniff are either numbers (scalars/0-D tensors) or multidimensional number arrays (n-D tensors).
|
|
28
28
|
|
|
29
|
-
There is a built-in `TensorMath` class to help with
|
|
29
|
+
There is a built-in `TensorMath` class to help with tensor arithmetic, for example:
|
|
30
30
|
```js
|
|
31
31
|
const { TensorMath } = require("catniff");
|
|
32
32
|
|
|
@@ -70,6 +70,8 @@ All available APIs are in `./src/autograd.ts`.
|
|
|
70
70
|
I'm mostly just learning and playing with this currently, so there are no concrete plans yet, but here are what I currently have in mind:
|
|
71
71
|
|
|
72
72
|
* Fix whatever is the problem right now (there are a lot of problems right now lol).
|
|
73
|
+
* Add more tensor ops.
|
|
74
|
+
* Proper documentation.
|
|
73
75
|
* GPU acceleration.
|
|
74
76
|
* Some general neural net APIs.
|
|
75
77
|
|
package/dist/autograd.d.ts
CHANGED
|
@@ -11,7 +11,9 @@ export declare enum OP {
|
|
|
11
11
|
LOG = 8,
|
|
12
12
|
RELU = 9,
|
|
13
13
|
SIGMOID = 10,
|
|
14
|
-
TANH = 11
|
|
14
|
+
TANH = 11,
|
|
15
|
+
T = 12,
|
|
16
|
+
MM = 13
|
|
15
17
|
}
|
|
16
18
|
export declare class Node {
|
|
17
19
|
value: Tensor;
|
|
@@ -32,6 +34,8 @@ export declare class Node {
|
|
|
32
34
|
relu(): Node;
|
|
33
35
|
sigmoid(): Node;
|
|
34
36
|
tanh(): Node;
|
|
37
|
+
t(): Node;
|
|
38
|
+
mm(other: Node | number): Node;
|
|
35
39
|
backward(): void;
|
|
36
40
|
static forceNode(value: Node | number): Node;
|
|
37
41
|
static addGrad(node: Node, accumGrad: Tensor): void;
|
package/dist/autograd.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Node = exports.OP = void 0;
|
|
4
4
|
const tensor_1 = require("./tensor");
|
|
5
|
-
const { add, sub, mul, pow, div, neg, exp, log, relu, sigmoid, tanh, ge } = tensor_1.TensorMath;
|
|
5
|
+
const { add, sub, mul, pow, div, neg, exp, log, relu, sigmoid, tanh, ge, t, mm } = tensor_1.TensorMath;
|
|
6
6
|
var OP;
|
|
7
7
|
(function (OP) {
|
|
8
8
|
OP[OP["NONE"] = 0] = "NONE";
|
|
@@ -17,6 +17,8 @@ var OP;
|
|
|
17
17
|
OP[OP["RELU"] = 9] = "RELU";
|
|
18
18
|
OP[OP["SIGMOID"] = 10] = "SIGMOID";
|
|
19
19
|
OP[OP["TANH"] = 11] = "TANH";
|
|
20
|
+
OP[OP["T"] = 12] = "T";
|
|
21
|
+
OP[OP["MM"] = 13] = "MM";
|
|
20
22
|
})(OP || (exports.OP = OP = {}));
|
|
21
23
|
class Node {
|
|
22
24
|
value;
|
|
@@ -61,7 +63,7 @@ class Node {
|
|
|
61
63
|
out.feedBackward = () => {
|
|
62
64
|
// x * y d/dx = y
|
|
63
65
|
Node.addGrad(this, mul(out.grad, other.value));
|
|
64
|
-
// x
|
|
66
|
+
// x * y d/dy = x
|
|
65
67
|
Node.addGrad(other, mul(out.grad, this.value));
|
|
66
68
|
};
|
|
67
69
|
return out;
|
|
@@ -142,6 +144,22 @@ class Node {
|
|
|
142
144
|
};
|
|
143
145
|
return out;
|
|
144
146
|
}
|
|
147
|
+
t() {
|
|
148
|
+
const out = new Node(t(this.value), [this], OP.T);
|
|
149
|
+
out.feedBackward = () => {
|
|
150
|
+
Node.addGrad(this, t(out.grad));
|
|
151
|
+
};
|
|
152
|
+
return out;
|
|
153
|
+
}
|
|
154
|
+
mm(other) {
|
|
155
|
+
other = Node.forceNode(other);
|
|
156
|
+
const out = new Node(mm(this.value, other.value), [this, other], OP.MM);
|
|
157
|
+
out.feedBackward = () => {
|
|
158
|
+
Node.addGrad(this, mm(out.grad, t(other.value)));
|
|
159
|
+
Node.addGrad(other, mm(t(this.value), out.grad));
|
|
160
|
+
};
|
|
161
|
+
return out;
|
|
162
|
+
}
|
|
145
163
|
backward() {
|
|
146
164
|
// Build topological order
|
|
147
165
|
const topo = [];
|
package/dist/tensor.d.ts
CHANGED
|
@@ -23,4 +23,6 @@ export declare class TensorMath {
|
|
|
23
23
|
static squeeze(tA: Tensor, dims?: number[] | number): Tensor;
|
|
24
24
|
static sumAxis(tA: Tensor, axis: number): Tensor;
|
|
25
25
|
static sum(tA: Tensor, dims?: number[] | number, keepDims?: boolean): Tensor;
|
|
26
|
+
static t(tA: Tensor): Tensor;
|
|
27
|
+
static mm(tA: Tensor, tB: Tensor): Tensor;
|
|
26
28
|
}
|
package/dist/tensor.js
CHANGED
|
@@ -315,5 +315,43 @@ class TensorMath {
|
|
|
315
315
|
}
|
|
316
316
|
return keepDims ? out : TensorMath.squeeze(out, dims);
|
|
317
317
|
}
|
|
318
|
+
static t(tA) {
|
|
319
|
+
const shapeA = TensorMath.getShape(tA);
|
|
320
|
+
if (shapeA.length !== 2)
|
|
321
|
+
throw new Error("Input is not a matrix");
|
|
322
|
+
const matA = tA;
|
|
323
|
+
const matARows = matA.length;
|
|
324
|
+
const matACols = matA[0].length;
|
|
325
|
+
const matATranspose = Array.from({ length: matACols }, () => new Array(matARows).fill(0));
|
|
326
|
+
for (let i = 0; i < matARows; i++) {
|
|
327
|
+
for (let j = 0; j < matACols; j++) {
|
|
328
|
+
matATranspose[j][i] = matA[i][j];
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return matATranspose;
|
|
332
|
+
}
|
|
333
|
+
static mm(tA, tB) {
|
|
334
|
+
const shapeA = TensorMath.getShape(tA);
|
|
335
|
+
const shapeB = TensorMath.getShape(tB);
|
|
336
|
+
if (shapeA.length !== 2 || shapeB.length !== 2)
|
|
337
|
+
throw new Error("Inputs are not matrices");
|
|
338
|
+
const matA = tA;
|
|
339
|
+
const matB = tB;
|
|
340
|
+
const matARows = matA.length;
|
|
341
|
+
const matACols = matA[0].length;
|
|
342
|
+
const matBRows = matB.length;
|
|
343
|
+
const matBCols = matB[0].length;
|
|
344
|
+
if (matACols !== matBRows)
|
|
345
|
+
throw new Error("Invalid matrices shape for multiplication");
|
|
346
|
+
const matC = Array.from({ length: matARows }, () => new Array(matBCols).fill(0));
|
|
347
|
+
for (let i = 0; i < matARows; i++) {
|
|
348
|
+
for (let j = 0; j < matBCols; j++) {
|
|
349
|
+
for (let k = 0; k < matACols; k++) {
|
|
350
|
+
matC[i][j] += matA[i][k] * matB[k][j];
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return matC;
|
|
355
|
+
}
|
|
318
356
|
}
|
|
319
357
|
exports.TensorMath = TensorMath;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "catniff",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "A cute autograd engine for Javascript",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,12 +14,18 @@
|
|
|
14
14
|
"cats",
|
|
15
15
|
"catniff",
|
|
16
16
|
"autograd",
|
|
17
|
+
"autodiff",
|
|
17
18
|
"ml",
|
|
18
19
|
"dl",
|
|
19
20
|
"ai",
|
|
20
21
|
"maths",
|
|
21
22
|
"gradient",
|
|
22
|
-
"tensors"
|
|
23
|
+
"tensors",
|
|
24
|
+
"library",
|
|
25
|
+
"framework",
|
|
26
|
+
"neural-network",
|
|
27
|
+
"machine-learning",
|
|
28
|
+
"deep-learning"
|
|
23
29
|
],
|
|
24
30
|
"author": "nguyenphuminh",
|
|
25
31
|
"license": "GPL-3.0",
|