catniff 0.2.9 → 0.2.11

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
@@ -43,14 +43,25 @@ export declare class Tensor {
43
43
  mean(dims?: number[] | number, keepDims?: boolean): Tensor;
44
44
  add(other: TensorValue | Tensor): Tensor;
45
45
  sub(other: TensorValue | Tensor): Tensor;
46
+ subtract: (other: TensorValue | Tensor) => Tensor;
46
47
  mul(other: TensorValue | Tensor): Tensor;
48
+ multiply: (other: TensorValue | Tensor) => Tensor;
47
49
  pow(other: TensorValue | Tensor): Tensor;
48
50
  div(other: TensorValue | Tensor): Tensor;
51
+ divide: (other: TensorValue | Tensor) => Tensor;
52
+ remainder(other: TensorValue | Tensor): Tensor;
49
53
  ge(other: TensorValue | Tensor): Tensor;
54
+ greaterEqual: (other: TensorValue | Tensor) => Tensor;
50
55
  le(other: TensorValue | Tensor): Tensor;
56
+ lessEqual: (other: TensorValue | Tensor) => Tensor;
51
57
  gt(other: TensorValue | Tensor): Tensor;
58
+ greater: (other: TensorValue | Tensor) => Tensor;
52
59
  lt(other: TensorValue | Tensor): Tensor;
60
+ less: (other: TensorValue | Tensor) => Tensor;
53
61
  eq(other: TensorValue | Tensor): Tensor;
62
+ equal: (other: TensorValue | Tensor) => Tensor;
63
+ ne(other: TensorValue | Tensor): Tensor;
64
+ notEqual: (other: TensorValue | Tensor) => Tensor;
54
65
  logicalAnd(other: TensorValue | Tensor): Tensor;
55
66
  logicalOr(other: TensorValue | Tensor): Tensor;
56
67
  logicalXor(other: TensorValue | Tensor): Tensor;
@@ -62,21 +73,38 @@ export declare class Tensor {
62
73
  bitwiseLeftShift(other: TensorValue | Tensor): Tensor;
63
74
  bitwiseRightShift(other: TensorValue | Tensor): Tensor;
64
75
  neg(): Tensor;
76
+ negative: () => Tensor;
77
+ reciprocal(): Tensor;
78
+ square(): Tensor;
65
79
  abs(): Tensor;
80
+ absolute: () => Tensor;
66
81
  sign(): Tensor;
67
82
  sin(): Tensor;
68
83
  cos(): Tensor;
69
84
  tan(): Tensor;
70
85
  asin(): Tensor;
86
+ arcsin: () => Tensor;
71
87
  acos(): Tensor;
88
+ arccos: () => Tensor;
72
89
  atan(): Tensor;
90
+ arctan: () => Tensor;
91
+ atan2(other: TensorValue | Tensor): Tensor;
92
+ arctan2: (other: TensorValue | Tensor) => Tensor;
73
93
  sinh(): Tensor;
74
94
  cosh(): Tensor;
75
95
  asinh(): Tensor;
96
+ arcsinh: () => Tensor;
76
97
  acosh(): Tensor;
98
+ arccosh: () => Tensor;
77
99
  atanh(): Tensor;
100
+ arctanh: () => Tensor;
101
+ deg2rad(): Tensor;
102
+ rad2deg(): Tensor;
78
103
  sqrt(): Tensor;
104
+ rsqrt(): Tensor;
79
105
  exp(): Tensor;
106
+ exp2(): Tensor;
107
+ expm1(): Tensor;
80
108
  log(): Tensor;
81
109
  log2(): Tensor;
82
110
  log10(): Tensor;
@@ -84,7 +112,26 @@ export declare class Tensor {
84
112
  relu(): Tensor;
85
113
  sigmoid(): Tensor;
86
114
  tanh(): Tensor;
115
+ softplus(): Tensor;
116
+ softsign(): Tensor;
117
+ silu(): Tensor;
118
+ mish(): Tensor;
119
+ maximum(other: TensorValue | Tensor): Tensor;
120
+ minimum(other: TensorValue | Tensor): Tensor;
121
+ round(): Tensor;
122
+ floor(): Tensor;
123
+ ceil(): Tensor;
124
+ trunc(): Tensor;
125
+ fix: () => Tensor;
126
+ frac(): Tensor;
127
+ clip(min: number, max: number): Tensor;
128
+ clamp: (min: number, max: number) => Tensor;
129
+ erf(): Tensor;
130
+ erfc(): Tensor;
131
+ erfinv(): Tensor;
87
132
  transpose(dim1: number, dim2: number): Tensor;
133
+ swapaxes: (dim1: number, dim2: number) => Tensor;
134
+ swapdims: (dim1: number, dim2: number) => Tensor;
88
135
  t(): Tensor;
89
136
  dot(other: TensorValue | Tensor): Tensor;
90
137
  mm(other: TensorValue | Tensor): Tensor;
package/dist/core.js CHANGED
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Tensor = void 0;
4
+ const utils_1 = require("./utils");
4
5
  class Tensor {
5
6
  value;
6
7
  shape;
@@ -522,38 +523,55 @@ class Tensor {
522
523
  sub(other) {
523
524
  return this.elementWiseABDAG(other, (a, b) => a - b, (self, other, outGrad) => outGrad, (self, other, outGrad) => outGrad.neg());
524
525
  }
526
+ subtract = this.sub;
525
527
  // Tensor element-wise multiplication
526
528
  mul(other) {
527
529
  return this.elementWiseABDAG(other, (a, b) => a * b, (self, other, outGrad) => outGrad.mul(other), (self, other, outGrad) => outGrad.mul(self));
528
530
  }
531
+ multiply = this.mul;
529
532
  // Tensor element-wise power
530
533
  pow(other) {
531
534
  return this.elementWiseABDAG(other, (a, b) => a ** b, (self, other, outGrad) => outGrad.mul(other.mul(self.pow(other.sub(1)))), (self, other, outGrad) => outGrad.mul(self.pow(other).mul(self.log())));
532
535
  }
533
536
  // Tensor element-wise division
534
537
  div(other) {
535
- return this.elementWiseABDAG(other, (a, b) => a / b, (self, other, outGrad) => outGrad.div(other), (self, other, outGrad) => outGrad.mul(self.neg().div(other.pow(2))));
538
+ return this.elementWiseABDAG(other, (a, b) => a / b, (self, other, outGrad) => outGrad.div(other), (self, other, outGrad) => outGrad.mul(self.neg().div(other.square())));
539
+ }
540
+ divide = this.div;
541
+ // Tensor element-wise modulo
542
+ remainder(other) {
543
+ return this.elementWiseABDAG(other, (a, b) => a % b);
536
544
  }
537
545
  // Tensor element-wise greater or equal comparison
538
546
  ge(other) {
539
547
  return this.elementWiseABDAG(other, (a, b) => a >= b ? 1 : 0);
540
548
  }
549
+ greaterEqual = this.ge;
541
550
  // Tensor element-wise less or equal comparison
542
551
  le(other) {
543
552
  return this.elementWiseABDAG(other, (a, b) => a <= b ? 1 : 0);
544
553
  }
554
+ lessEqual = this.le;
545
555
  // Tensor element-wise greater-than comparison
546
556
  gt(other) {
547
557
  return this.elementWiseABDAG(other, (a, b) => a > b ? 1 : 0);
548
558
  }
559
+ greater = this.gt;
549
560
  // Tensor element-wise less-than comparison
550
561
  lt(other) {
551
562
  return this.elementWiseABDAG(other, (a, b) => a < b ? 1 : 0);
552
563
  }
564
+ less = this.lt;
553
565
  // Tensor element-wise equality comparison
554
566
  eq(other) {
555
567
  return this.elementWiseABDAG(other, (a, b) => a === b ? 1 : 0);
556
568
  }
569
+ equal = this.eq;
570
+ // Tensor element-wise not equality comparison
571
+ ne(other) {
572
+ return this.elementWiseABDAG(other, (a, b) => a !== b ? 1 : 0);
573
+ }
574
+ notEqual = this.ne;
557
575
  // Tensor element-wise logical and
558
576
  logicalAnd(other) {
559
577
  return this.elementWiseABDAG(other, (a, b) => a === 1 && b === 1 ? 1 : 0);
@@ -598,10 +616,20 @@ class Tensor {
598
616
  neg() {
599
617
  return this.elementWiseSelfDAG((a) => -a, (self, outGrad) => outGrad.mul(-1));
600
618
  }
619
+ negative = this.neg;
620
+ // Tensor element-wise reciprocal
621
+ reciprocal() {
622
+ return this.elementWiseSelfDAG((a) => 1 / a, (self, outGrad) => outGrad.mul(self.pow(-2).neg()));
623
+ }
624
+ // Tensor element-wise square
625
+ square() {
626
+ return this.elementWiseSelfDAG((a) => a * a, (self, outGrad) => outGrad.mul(self.mul(2)));
627
+ }
601
628
  // Tensor element-wise absolute
602
629
  abs() {
603
630
  return this.elementWiseSelfDAG((a) => Math.abs(a), (self, outGrad) => outGrad.mul(self.sign()));
604
631
  }
632
+ absolute = this.abs;
605
633
  // Tensor element-wise sign function
606
634
  sign() {
607
635
  return this.elementWiseSelfDAG((a) => Math.sign(a));
@@ -616,20 +644,28 @@ class Tensor {
616
644
  }
617
645
  // Tensor element-wise tan
618
646
  tan() {
619
- return this.elementWiseSelfDAG((a) => Math.tan(a), (self, outGrad) => outGrad.mul(self.tan().pow(2).add(1)));
647
+ return this.elementWiseSelfDAG((a) => Math.tan(a), (self, outGrad) => outGrad.mul(self.tan().square().add(1)));
620
648
  }
621
649
  // Tensor element-wise asin
622
650
  asin() {
623
- return this.elementWiseSelfDAG((a) => Math.asin(a), (self, outGrad) => outGrad.div(self.pow(2).neg().add(1).sqrt()));
651
+ return this.elementWiseSelfDAG((a) => Math.asin(a), (self, outGrad) => outGrad.div(self.square().neg().add(1).sqrt()));
624
652
  }
653
+ arcsin = this.asin;
625
654
  // Tensor element-wise acos
626
655
  acos() {
627
- return this.elementWiseSelfDAG((a) => Math.acos(a), (self, outGrad) => outGrad.div(self.pow(2).neg().add(1).sqrt()).neg());
656
+ return this.elementWiseSelfDAG((a) => Math.acos(a), (self, outGrad) => outGrad.div(self.square().neg().add(1).sqrt()).neg());
628
657
  }
658
+ arccos = this.acos;
629
659
  // Tensor element-wise atan
630
660
  atan() {
631
- return this.elementWiseSelfDAG((a) => Math.atan(a), (self, outGrad) => outGrad.div(self.pow(2).add(1)));
661
+ return this.elementWiseSelfDAG((a) => Math.atan(a), (self, outGrad) => outGrad.div(self.square().add(1)));
632
662
  }
663
+ arctan = this.atan;
664
+ // Tensor element-wise atan2
665
+ atan2(other) {
666
+ return this.elementWiseABDAG(other, (a, b) => Math.atan2(a, b), (self, other, outGrad) => outGrad.mul(other.div(self.square().add(other.square()))), (self, other, outGrad) => outGrad.mul(self.neg().div(self.square().add(other.square()))));
667
+ }
668
+ arctan2 = this.atan2;
633
669
  // Tensor element-wise sinh
634
670
  sinh() {
635
671
  return this.elementWiseSelfDAG((a) => Math.sinh(a), (self, outGrad) => outGrad.mul(self.cosh()));
@@ -640,24 +676,47 @@ class Tensor {
640
676
  }
641
677
  // Tensor element-wise asinh
642
678
  asinh() {
643
- return this.elementWiseSelfDAG((a) => Math.asinh(a), (self, outGrad) => outGrad.div(self.pow(2).add(1).sqrt()));
679
+ return this.elementWiseSelfDAG((a) => Math.asinh(a), (self, outGrad) => outGrad.div(self.square().add(1).sqrt()));
644
680
  }
681
+ arcsinh = this.asinh;
645
682
  // Tensor element-wise acosh
646
683
  acosh() {
647
684
  return this.elementWiseSelfDAG((a) => Math.acosh(a), (self, outGrad) => outGrad.div(self.add(1).sqrt().mul(self.sub(1).sqrt())));
648
685
  }
686
+ arccosh = this.acosh;
649
687
  // Tensor element-wise atanh
650
688
  atanh() {
651
- return this.elementWiseSelfDAG((a) => Math.atanh(a), (self, outGrad) => outGrad.div(self.pow(2).neg().add(1)));
689
+ return this.elementWiseSelfDAG((a) => Math.atanh(a), (self, outGrad) => outGrad.div(self.square().neg().add(1)));
690
+ }
691
+ arctanh = this.atanh;
692
+ // Tensor element-wise degree to radian
693
+ deg2rad() {
694
+ return this.elementWiseSelfDAG((a) => a * (Math.PI / 180), (self, outGrad) => outGrad.mul(Math.PI / 180));
695
+ }
696
+ // Tensor element-wise radian to degree
697
+ rad2deg() {
698
+ return this.elementWiseSelfDAG((a) => a / (Math.PI / 180), (self, outGrad) => outGrad.div(Math.PI / 180));
652
699
  }
653
700
  // Tensor element-wise square root
654
701
  sqrt() {
655
702
  return this.elementWiseSelfDAG((a) => Math.sqrt(a), (self, outGrad) => outGrad.div(self.sqrt().mul(2)));
656
703
  }
704
+ // Tensor element-wise reciprocal of square root
705
+ rsqrt() {
706
+ return this.elementWiseSelfDAG((a) => 1 / Math.sqrt(a), (self, outGrad) => outGrad.mul(self.pow(-1.5).mul(-0.5)));
707
+ }
657
708
  // Tensor element-wise e^x
658
709
  exp() {
659
710
  return this.elementWiseSelfDAG((a) => Math.exp(a), (self, outGrad) => outGrad.mul(self.exp()));
660
711
  }
712
+ // Tensor element-wise 2^x
713
+ exp2() {
714
+ return this.elementWiseSelfDAG((a) => 2 ** a, (self, outGrad) => outGrad.mul(self.exp2().mul(Math.log(2))));
715
+ }
716
+ // Tensor element-wise e^x - 1
717
+ expm1() {
718
+ return this.elementWiseSelfDAG((a) => Math.expm1(a), (self, outGrad) => outGrad.mul(self.exp()));
719
+ }
661
720
  // Tensor element-wise natural log
662
721
  log() {
663
722
  return this.elementWiseSelfDAG((a) => Math.log(a), (self, outGrad) => outGrad.div(self));
@@ -676,7 +735,7 @@ class Tensor {
676
735
  }
677
736
  // Tensor element-wise relu
678
737
  relu() {
679
- return this.elementWiseSelfDAG((a) => Math.max(a, 0), (self, outGrad) => outGrad.mul(self.ge(0)));
738
+ return this.elementWiseSelfDAG((a) => Math.max(a, 0), (self, outGrad) => outGrad.mul(self.gt(0)));
680
739
  }
681
740
  // Tensor element-wise sigmoid
682
741
  sigmoid() {
@@ -687,7 +746,77 @@ class Tensor {
687
746
  }
688
747
  // Tensor element-wise tanh
689
748
  tanh() {
690
- return this.elementWiseSelfDAG((a) => Math.tanh(a), (self, outGrad) => outGrad.mul(self.tanh().pow(2).neg().add(1)));
749
+ return this.elementWiseSelfDAG((a) => Math.tanh(a), (self, outGrad) => outGrad.mul(self.tanh().square().neg().add(1)));
750
+ }
751
+ // Tensor element-wise softplus
752
+ softplus() {
753
+ return this.elementWiseSelfDAG((a) => Math.log1p(Math.exp(a)), (self, outGrad) => outGrad.mul(self.sigmoid()));
754
+ }
755
+ // Tensor element-wise softsign
756
+ softsign() {
757
+ return this.elementWiseSelfDAG((a) => a / (1 + Math.abs(a)), (self, outGrad) => outGrad.div(self.abs().add(1).square()));
758
+ }
759
+ // Tensor element-wise silu (swish)
760
+ silu() {
761
+ return this.elementWiseSelfDAG((a) => a / (1 + Math.exp(-a)), (self, outGrad) => {
762
+ const sig = self.sigmoid();
763
+ return outGrad.mul(sig.add(self.mul(sig).mul(sig.neg().add(1))));
764
+ });
765
+ }
766
+ // Tensor element-wise mish
767
+ mish() {
768
+ return this.elementWiseSelfDAG((a) => a * Math.tanh(Math.log1p(Math.exp(a))), (self, outGrad) => {
769
+ const tanhSoftPlus = self.exp().add(1).log().tanh();
770
+ // tanh(softplus(x)) + x * (1 - tanh²(softplus(x))) * sigmoid(x)
771
+ const derivative = tanhSoftPlus.add(self.mul(tanhSoftPlus.square().neg().add(1)).mul(self.sigmoid()));
772
+ return outGrad.mul(derivative);
773
+ });
774
+ }
775
+ // Tensor element-wise maximum
776
+ maximum(other) {
777
+ return this.elementWiseABDAG(other, (a, b) => Math.max(a, b), (self, other, outGrad) => outGrad.mul(self.gt(other).add(self.eq(other).mul(0.5))), (self, other, outGrad) => outGrad.mul(other.gt(self).add(other.eq(self).mul(0.5))));
778
+ }
779
+ // Tensor element-wise minimum
780
+ minimum(other) {
781
+ return this.elementWiseABDAG(other, (a, b) => Math.min(a, b), (self, other, outGrad) => outGrad.mul(self.lt(other).add(self.eq(other).mul(0.5))), (self, other, outGrad) => outGrad.mul(other.lt(self).add(other.eq(self).mul(0.5))));
782
+ }
783
+ // Tensor element-wise round
784
+ round() {
785
+ return this.elementWiseSelfDAG((a) => Math.round(a));
786
+ }
787
+ // Tensor element-wise floor
788
+ floor() {
789
+ return this.elementWiseSelfDAG((a) => Math.floor(a));
790
+ }
791
+ // Tensor element-wise ceil
792
+ ceil() {
793
+ return this.elementWiseSelfDAG((a) => Math.ceil(a));
794
+ }
795
+ // Tensor element-wise truncation
796
+ trunc() {
797
+ return this.elementWiseSelfDAG((a) => Math.trunc(a));
798
+ }
799
+ fix = this.trunc;
800
+ // Tensor element-wise fraction portion
801
+ frac() {
802
+ return this.elementWiseSelfDAG((a) => a - Math.floor(a));
803
+ }
804
+ // Tensor element-wise clip and clamp
805
+ clip(min, max) {
806
+ return this.elementWiseSelfDAG((a) => Math.max(min, Math.min(max, a)), (self, outGrad) => outGrad.mul(self.ge(min).mul(self.le(max))));
807
+ }
808
+ clamp = this.clip;
809
+ // Tensor element-wise error function
810
+ erf() {
811
+ return this.elementWiseSelfDAG((a) => (0, utils_1.erf)(a), (self, outGrad) => outGrad.mul(self.square().neg().exp().mul(2 / Math.sqrt(Math.PI))));
812
+ }
813
+ // Tensor element-wise complementary error function
814
+ erfc() {
815
+ return this.elementWiseSelfDAG((a) => (0, utils_1.erfc)(a), (self, outGrad) => outGrad.mul(self.square().neg().exp().mul(2 / Math.sqrt(Math.PI)).neg()));
816
+ }
817
+ // Tensor element-wise inverse error function
818
+ erfinv() {
819
+ return this.elementWiseSelfDAG((a) => (0, utils_1.erfinv)(a), (self, outGrad) => outGrad.mul(self.erfinv().square().exp().mul(Math.sqrt(Math.PI) / 2)));
691
820
  }
692
821
  // Transpose
693
822
  transpose(dim1, dim2) {
@@ -716,6 +845,8 @@ class Tensor {
716
845
  }
717
846
  return out;
718
847
  }
848
+ swapaxes = this.transpose;
849
+ swapdims = this.transpose;
719
850
  // Transpose 2D
720
851
  t() {
721
852
  // Verify matrix shape
@@ -0,0 +1,3 @@
1
+ export declare function erf(x: number): number;
2
+ export declare function erfc(x: number): number;
3
+ export declare function erfinv(x: number): number;
package/dist/utils.js ADDED
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.erf = erf;
4
+ exports.erfc = erfc;
5
+ exports.erfinv = erfinv;
6
+ // Error function using Abramowitz and Stegun approximation
7
+ function erf(x) {
8
+ const a1 = 0.254829592;
9
+ const a2 = -0.284496736;
10
+ const a3 = 1.421413741;
11
+ const a4 = -1.453152027;
12
+ const a5 = 1.061405429;
13
+ const p = 0.3275911;
14
+ const sign = x >= 0 ? 1 : -1;
15
+ x = Math.abs(x);
16
+ // Formula 7.1.26
17
+ const t = 1.0 / (1.0 + p * x);
18
+ const y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
19
+ return sign * y;
20
+ }
21
+ // Complementary error function
22
+ function erfc(x) {
23
+ return 1 - erf(x);
24
+ }
25
+ // Inverse error function using Winitzki approximation
26
+ function erfinv(x) {
27
+ if (Math.abs(x) >= 1)
28
+ throw new Error("Input must be in range (-1, 1)");
29
+ const a = 0.147;
30
+ const ln = Math.log(1 - x * x);
31
+ const part1 = 2 / (Math.PI * a) + ln / 2;
32
+ const part2 = ln / a;
33
+ const sign = x >= 0 ? 1 : -1;
34
+ return sign * Math.sqrt(-part1 + Math.sqrt(part1 * part1 - part2));
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "description": "A cute autograd engine for Javascript",
5
5
  "main": "index.js",
6
6
  "scripts": {