catniff 0.2.6 → 0.2.7

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/dist/core.d.ts CHANGED
@@ -91,6 +91,8 @@ 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
97
  val(): any;
96
98
  withGrad(requiresGrad: boolean): Tensor;
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.7",
4
4
  "description": "A cute autograd engine for Javascript",
5
5
  "main": "index.js",
6
6
  "scripts": {