catniff 0.2.6 → 0.2.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.
package/README.md CHANGED
@@ -39,8 +39,6 @@ const B = new Tensor(3);
39
39
  console.log(A.add(B).val());
40
40
  ```
41
41
 
42
- All available APIs are in `./src/core.ts`.
43
-
44
42
  ## Autograd
45
43
 
46
44
  To compute the gradient wrt multiple variables of our mathematical expression, we can simply set `requiresGrad` to `true`:
@@ -74,11 +72,11 @@ G.backward();
74
72
  console.log(X.grad.val(), Y.grad.val());
75
73
  ```
76
74
 
77
- All available APIs are in `./src/core.ts`.
78
-
79
75
  ## Documentation
80
76
 
81
- Todo :/
77
+ Full documentation is available in [`./docs/documentation.md`](./docs/documentation.md).
78
+
79
+ All available APIs are in [`./src/core.ts`](./src/core.ts) if you want to dig deeper.
82
80
 
83
81
  ## Todos
84
82
 
package/dist/core.d.ts CHANGED
@@ -91,7 +91,9 @@ export declare class Tensor {
91
91
  mv(other: TensorValue | Tensor): Tensor;
92
92
  matmul(other: TensorValue | Tensor): Tensor;
93
93
  static fullLike(tensor: Tensor, num: number, options?: TensorOptions): Tensor;
94
+ static onesLike(tensor: Tensor, options?: TensorOptions): Tensor;
95
+ static zerosLike(tensor: Tensor, options?: TensorOptions): Tensor;
94
96
  backward(): void;
95
- val(): any;
97
+ val(): TensorValue;
96
98
  withGrad(requiresGrad: boolean): Tensor;
97
99
  }
package/dist/core.js CHANGED
@@ -872,6 +872,18 @@ class Tensor {
872
872
  return new Tensor(num, options);
873
873
  return new Tensor(new Array(tensor.value.length).fill(num), { shape: tensor.shape, strides: tensor.strides, ...options });
874
874
  }
875
+ // Utility to create a new tensor with shape of another tensor, filled with 1
876
+ static onesLike(tensor, options = {}) {
877
+ if (typeof tensor.value === "number")
878
+ return new Tensor(1, options);
879
+ return new Tensor(new Array(tensor.value.length).fill(1), { shape: tensor.shape, strides: tensor.strides, ...options });
880
+ }
881
+ // Utility to create a new tensor with shape of another tensor, filled with 0
882
+ static zerosLike(tensor, options = {}) {
883
+ if (typeof tensor.value === "number")
884
+ return new Tensor(0, options);
885
+ return new Tensor(new Array(tensor.value.length).fill(0), { shape: tensor.shape, strides: tensor.strides, ...options });
886
+ }
875
887
  // Reverse-mode autodiff call
876
888
  backward() {
877
889
  // Build topological order
@@ -880,7 +892,7 @@ class Tensor {
880
892
  function build(node) {
881
893
  if (!visited.has(node) && node.requiresGrad) {
882
894
  visited.add(node);
883
- node.grad = Tensor.fullLike(node, 0); // Reset grad with 0
895
+ node.grad = Tensor.zerosLike(node); // Reset grad with 0
884
896
  for (let child of node.children)
885
897
  build(child);
886
898
  topo.push(node);
@@ -888,7 +900,7 @@ class Tensor {
888
900
  }
889
901
  build(this);
890
902
  // Feed backward to calculate gradient
891
- this.grad = Tensor.fullLike(this, 1);
903
+ this.grad = Tensor.onesLike(this);
892
904
  for (let index = topo.length - 1; index > -1; index--) {
893
905
  topo[index].gradFn();
894
906
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.2.6",
3
+ "version": "0.2.8",
4
4
  "description": "A cute autograd engine for Javascript",
5
5
  "main": "index.js",
6
6
  "scripts": {