catniff 0.1.7 → 0.1.8

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.
@@ -45,7 +45,8 @@ export declare enum OP {
45
45
  SIGMOID = 42,
46
46
  TANH = 43,
47
47
  T = 44,
48
- MM = 45
48
+ MM = 45,
49
+ DOT = 46
49
50
  }
50
51
  export declare class Node {
51
52
  value: Tensor;
@@ -100,6 +101,7 @@ export declare class Node {
100
101
  tanh(): Node;
101
102
  t(): Node;
102
103
  mm(other: Node | number): Node;
104
+ dot(other: Node | number): Node;
103
105
  backward(): void;
104
106
  static forceNode(value: Node | number): Node;
105
107
  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, gt, lt, ge, le, eq, logicalAnd, logicalOr, logicalXor, logicalNot, bitwiseAnd, bitwiseOr, bitwiseXor, bitwiseNot, bitwiseLeftShift, bitwiseRightShift, neg, abs, sign, sin, cos, tan, asin, acos, atan, sinh, cosh, asinh, acosh, atanh, sqrt, exp, log, log2, log10, log1p, relu, sigmoid, tanh, t, mm } = tensor_1.TensorMath;
5
+ const { add, sub, mul, pow, div, gt, lt, ge, le, eq, logicalAnd, logicalOr, logicalXor, logicalNot, bitwiseAnd, bitwiseOr, bitwiseXor, bitwiseNot, bitwiseLeftShift, bitwiseRightShift, neg, abs, sign, sin, cos, tan, asin, acos, atan, sinh, cosh, asinh, acosh, atanh, sqrt, exp, log, log2, log10, log1p, relu, sigmoid, tanh, t, mm, dot } = tensor_1.TensorMath;
6
6
  var OP;
7
7
  (function (OP) {
8
8
  OP[OP["NONE"] = 0] = "NONE";
@@ -51,6 +51,7 @@ var OP;
51
51
  OP[OP["TANH"] = 43] = "TANH";
52
52
  OP[OP["T"] = 44] = "T";
53
53
  OP[OP["MM"] = 45] = "MM";
54
+ OP[OP["DOT"] = 46] = "DOT";
54
55
  })(OP || (exports.OP = OP = {}));
55
56
  class Node {
56
57
  value;
@@ -447,6 +448,15 @@ class Node {
447
448
  };
448
449
  return out;
449
450
  }
451
+ dot(other) {
452
+ other = Node.forceNode(other);
453
+ const out = new Node(dot(this.value, other.value), [this, other], OP.DOT);
454
+ out.feedBackward = () => {
455
+ Node.addGrad(this, mul(out.grad, other.value));
456
+ Node.addGrad(other, mul(out.grad, this.value));
457
+ };
458
+ return out;
459
+ }
450
460
  backward() {
451
461
  // Build topological order
452
462
  const topo = [];
package/dist/tensor.d.ts CHANGED
@@ -54,4 +54,5 @@ export declare class TensorMath {
54
54
  static sum(tA: Tensor, dims?: number[] | number, keepDims?: boolean): Tensor;
55
55
  static t(tA: Tensor): Tensor;
56
56
  static mm(tA: Tensor, tB: Tensor): Tensor;
57
+ static dot(tA: Tensor, tB: Tensor): Tensor;
57
58
  }
package/dist/tensor.js CHANGED
@@ -289,5 +289,19 @@ class TensorMath {
289
289
  }
290
290
  return matC;
291
291
  }
292
+ static dot(tA, tB) {
293
+ const shapeA = TensorMath.getShape(tA);
294
+ const shapeB = TensorMath.getShape(tB);
295
+ if (shapeA.length !== 1 || shapeB.length !== 1 || shapeA[0] !== shapeB[0])
296
+ throw new Error("Inputs are not 1D tensors");
297
+ const vectLen = shapeA[0];
298
+ const vectA = tA;
299
+ const vectB = tB;
300
+ let sum = 0;
301
+ for (let index = 0; index < vectLen; index++) {
302
+ sum += vectA[index] * vectB[index];
303
+ }
304
+ return sum;
305
+ }
292
306
  }
293
307
  exports.TensorMath = TensorMath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.1.7",
3
+ "version": "0.1.8",
4
4
  "description": "A cute autograd engine for Javascript",
5
5
  "main": "index.js",
6
6
  "scripts": {