catniff 0.2.10 → 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 +9 -0
- package/dist/core.js +55 -10
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +35 -0
- package/package.json +1 -1
package/dist/core.d.ts
CHANGED
|
@@ -112,6 +112,12 @@ export declare class Tensor {
|
|
|
112
112
|
relu(): Tensor;
|
|
113
113
|
sigmoid(): Tensor;
|
|
114
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;
|
|
115
121
|
round(): Tensor;
|
|
116
122
|
floor(): Tensor;
|
|
117
123
|
ceil(): Tensor;
|
|
@@ -120,6 +126,9 @@ export declare class Tensor {
|
|
|
120
126
|
frac(): Tensor;
|
|
121
127
|
clip(min: number, max: number): Tensor;
|
|
122
128
|
clamp: (min: number, max: number) => Tensor;
|
|
129
|
+
erf(): Tensor;
|
|
130
|
+
erfc(): Tensor;
|
|
131
|
+
erfinv(): Tensor;
|
|
123
132
|
transpose(dim1: number, dim2: number): Tensor;
|
|
124
133
|
swapaxes: (dim1: number, dim2: number) => Tensor;
|
|
125
134
|
swapdims: (dim1: number, dim2: number) => 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;
|
|
@@ -534,7 +535,7 @@ class Tensor {
|
|
|
534
535
|
}
|
|
535
536
|
// Tensor element-wise division
|
|
536
537
|
div(other) {
|
|
537
|
-
return this.elementWiseABDAG(other, (a, b) => a / b, (self, other, outGrad) => outGrad.div(other), (self, other, outGrad) => outGrad.mul(self.neg().div(other.
|
|
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())));
|
|
538
539
|
}
|
|
539
540
|
divide = this.div;
|
|
540
541
|
// Tensor element-wise modulo
|
|
@@ -618,7 +619,7 @@ class Tensor {
|
|
|
618
619
|
negative = this.neg;
|
|
619
620
|
// Tensor element-wise reciprocal
|
|
620
621
|
reciprocal() {
|
|
621
|
-
return this.elementWiseSelfDAG((a) => 1 / a, (self, outGrad) => outGrad.mul(self.
|
|
622
|
+
return this.elementWiseSelfDAG((a) => 1 / a, (self, outGrad) => outGrad.mul(self.pow(-2).neg()));
|
|
622
623
|
}
|
|
623
624
|
// Tensor element-wise square
|
|
624
625
|
square() {
|
|
@@ -643,21 +644,21 @@ class Tensor {
|
|
|
643
644
|
}
|
|
644
645
|
// Tensor element-wise tan
|
|
645
646
|
tan() {
|
|
646
|
-
return this.elementWiseSelfDAG((a) => Math.tan(a), (self, outGrad) => outGrad.mul(self.tan().
|
|
647
|
+
return this.elementWiseSelfDAG((a) => Math.tan(a), (self, outGrad) => outGrad.mul(self.tan().square().add(1)));
|
|
647
648
|
}
|
|
648
649
|
// Tensor element-wise asin
|
|
649
650
|
asin() {
|
|
650
|
-
return this.elementWiseSelfDAG((a) => Math.asin(a), (self, outGrad) => outGrad.div(self.
|
|
651
|
+
return this.elementWiseSelfDAG((a) => Math.asin(a), (self, outGrad) => outGrad.div(self.square().neg().add(1).sqrt()));
|
|
651
652
|
}
|
|
652
653
|
arcsin = this.asin;
|
|
653
654
|
// Tensor element-wise acos
|
|
654
655
|
acos() {
|
|
655
|
-
return this.elementWiseSelfDAG((a) => Math.acos(a), (self, outGrad) => outGrad.div(self.
|
|
656
|
+
return this.elementWiseSelfDAG((a) => Math.acos(a), (self, outGrad) => outGrad.div(self.square().neg().add(1).sqrt()).neg());
|
|
656
657
|
}
|
|
657
658
|
arccos = this.acos;
|
|
658
659
|
// Tensor element-wise atan
|
|
659
660
|
atan() {
|
|
660
|
-
return this.elementWiseSelfDAG((a) => Math.atan(a), (self, outGrad) => outGrad.div(self.
|
|
661
|
+
return this.elementWiseSelfDAG((a) => Math.atan(a), (self, outGrad) => outGrad.div(self.square().add(1)));
|
|
661
662
|
}
|
|
662
663
|
arctan = this.atan;
|
|
663
664
|
// Tensor element-wise atan2
|
|
@@ -675,7 +676,7 @@ class Tensor {
|
|
|
675
676
|
}
|
|
676
677
|
// Tensor element-wise asinh
|
|
677
678
|
asinh() {
|
|
678
|
-
return this.elementWiseSelfDAG((a) => Math.asinh(a), (self, outGrad) => outGrad.div(self.
|
|
679
|
+
return this.elementWiseSelfDAG((a) => Math.asinh(a), (self, outGrad) => outGrad.div(self.square().add(1).sqrt()));
|
|
679
680
|
}
|
|
680
681
|
arcsinh = this.asinh;
|
|
681
682
|
// Tensor element-wise acosh
|
|
@@ -685,7 +686,7 @@ class Tensor {
|
|
|
685
686
|
arccosh = this.acosh;
|
|
686
687
|
// Tensor element-wise atanh
|
|
687
688
|
atanh() {
|
|
688
|
-
return this.elementWiseSelfDAG((a) => Math.atanh(a), (self, outGrad) => outGrad.div(self.
|
|
689
|
+
return this.elementWiseSelfDAG((a) => Math.atanh(a), (self, outGrad) => outGrad.div(self.square().neg().add(1)));
|
|
689
690
|
}
|
|
690
691
|
arctanh = this.atanh;
|
|
691
692
|
// Tensor element-wise degree to radian
|
|
@@ -734,7 +735,7 @@ class Tensor {
|
|
|
734
735
|
}
|
|
735
736
|
// Tensor element-wise relu
|
|
736
737
|
relu() {
|
|
737
|
-
return this.elementWiseSelfDAG((a) => Math.max(a, 0), (self, outGrad) => outGrad.mul(self.
|
|
738
|
+
return this.elementWiseSelfDAG((a) => Math.max(a, 0), (self, outGrad) => outGrad.mul(self.gt(0)));
|
|
738
739
|
}
|
|
739
740
|
// Tensor element-wise sigmoid
|
|
740
741
|
sigmoid() {
|
|
@@ -745,7 +746,39 @@ class Tensor {
|
|
|
745
746
|
}
|
|
746
747
|
// Tensor element-wise tanh
|
|
747
748
|
tanh() {
|
|
748
|
-
return this.elementWiseSelfDAG((a) => Math.tanh(a), (self, outGrad) => outGrad.mul(self.tanh().
|
|
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))));
|
|
749
782
|
}
|
|
750
783
|
// Tensor element-wise round
|
|
751
784
|
round() {
|
|
@@ -773,6 +806,18 @@ class Tensor {
|
|
|
773
806
|
return this.elementWiseSelfDAG((a) => Math.max(min, Math.min(max, a)), (self, outGrad) => outGrad.mul(self.ge(min).mul(self.le(max))));
|
|
774
807
|
}
|
|
775
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)));
|
|
820
|
+
}
|
|
776
821
|
// Transpose
|
|
777
822
|
transpose(dim1, dim2) {
|
|
778
823
|
// If dimension out of bound, throw error
|
package/dist/utils.d.ts
ADDED
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
|
+
}
|