catniff 0.5.10 → 0.5.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 +2 -0
- package/dist/core.js +30 -22
- package/package.json +1 -1
package/dist/core.d.ts
CHANGED
|
@@ -50,6 +50,8 @@ export declare class Tensor {
|
|
|
50
50
|
mean(dims?: number[] | number, keepDims?: boolean): Tensor;
|
|
51
51
|
max(dims?: number[] | number, keepDims?: boolean): Tensor;
|
|
52
52
|
min(dims?: number[] | number, keepDims?: boolean): Tensor;
|
|
53
|
+
all(dims?: number[] | number, keepDims?: boolean): Tensor;
|
|
54
|
+
any(dims?: number[] | number, keepDims?: boolean): Tensor;
|
|
53
55
|
var(dims?: number[] | number, keepDims?: boolean): Tensor;
|
|
54
56
|
std(dims?: number[] | number, keepDims?: boolean): Tensor;
|
|
55
57
|
softmax(dims?: number[] | number): Tensor;
|
package/dist/core.js
CHANGED
|
@@ -432,9 +432,9 @@ class Tensor {
|
|
|
432
432
|
}
|
|
433
433
|
// Calculate new value after sum
|
|
434
434
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
435
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
436
435
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
437
|
-
const outCoords =
|
|
436
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
437
|
+
outCoords[dims] = 0;
|
|
438
438
|
// Convert output coordinates to flat index
|
|
439
439
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
440
440
|
// Add into sum
|
|
@@ -483,9 +483,9 @@ class Tensor {
|
|
|
483
483
|
const originalSize = Tensor.shapeToSize(this.shape);
|
|
484
484
|
// Calculate new value after multiplying
|
|
485
485
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
486
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
487
486
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
488
|
-
const outCoords =
|
|
487
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
488
|
+
outCoords[dims] = 0;
|
|
489
489
|
// Convert output coordinates to flat index
|
|
490
490
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
491
491
|
// Multiply into product
|
|
@@ -502,9 +502,9 @@ class Tensor {
|
|
|
502
502
|
out.gradFn = () => {
|
|
503
503
|
const gradShape = this.shape, gradStrides = this.strides, gradValue = new Array(originalSize).fill(0);
|
|
504
504
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
505
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
506
505
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
507
|
-
const outCoords =
|
|
506
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
507
|
+
outCoords[dims] = 0;
|
|
508
508
|
// Convert output coordinates to flat index
|
|
509
509
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
510
510
|
// Grad is the product of other elements of the same axis, which is product of all els divided by the current value
|
|
@@ -541,9 +541,9 @@ class Tensor {
|
|
|
541
541
|
const originalSize = Tensor.shapeToSize(this.shape);
|
|
542
542
|
// Calculate sums and how many elements contribute to specific positions
|
|
543
543
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
544
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
545
544
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
546
|
-
const outCoords =
|
|
545
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
546
|
+
outCoords[dims] = 0;
|
|
547
547
|
// Convert output coordinates to flat index
|
|
548
548
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
549
549
|
// Calculate sum and contributors to the sum
|
|
@@ -566,9 +566,9 @@ class Tensor {
|
|
|
566
566
|
const gradShape = this.shape, gradStrides = this.strides, gradValue = new Array(originalSize).fill(0);
|
|
567
567
|
// Calculate grad by assigning 1 divided by the number of contributors to the position
|
|
568
568
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
569
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
570
569
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
571
|
-
const outCoords =
|
|
570
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
571
|
+
outCoords[dims] = 0;
|
|
572
572
|
// Convert output coordinates to flat index
|
|
573
573
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
574
574
|
// Mean = 1/n * (el1 + el2 + ... + eln) so grad = 1/n
|
|
@@ -604,9 +604,9 @@ class Tensor {
|
|
|
604
604
|
const originalSize = Tensor.shapeToSize(this.shape);
|
|
605
605
|
// Calculate maximum values of axes
|
|
606
606
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
607
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
608
607
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
609
|
-
const outCoords =
|
|
608
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
609
|
+
outCoords[dims] = 0;
|
|
610
610
|
// Convert output coordinates to flat index
|
|
611
611
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
612
612
|
// Get max over time
|
|
@@ -627,18 +627,18 @@ class Tensor {
|
|
|
627
627
|
const shareCounts = new Array(outputSize).fill(0);
|
|
628
628
|
const originalValue = this.value;
|
|
629
629
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
630
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
631
630
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
632
|
-
const outCoords =
|
|
631
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
632
|
+
outCoords[dims] = 0;
|
|
633
633
|
// Convert output coordinates to flat index
|
|
634
634
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
635
635
|
// We collect how many elements share the same max value first
|
|
636
636
|
shareCounts[outFlatIndex] += outputValue[outFlatIndex] === originalValue[realFlatIndex] ? 1 : 0;
|
|
637
637
|
}
|
|
638
638
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
639
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
640
639
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
641
|
-
const outCoords =
|
|
640
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
641
|
+
outCoords[dims] = 0;
|
|
642
642
|
// Convert output coordinates to flat index
|
|
643
643
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
644
644
|
// Here we share the grad between the elements that share the same max value
|
|
@@ -674,9 +674,9 @@ class Tensor {
|
|
|
674
674
|
const originalSize = Tensor.shapeToSize(this.shape);
|
|
675
675
|
// Calculate minimum values of axes
|
|
676
676
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
677
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
678
677
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
679
|
-
const outCoords =
|
|
678
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
679
|
+
outCoords[dims] = 0;
|
|
680
680
|
// Convert output coordinates to flat index
|
|
681
681
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
682
682
|
// Get min over time
|
|
@@ -697,18 +697,18 @@ class Tensor {
|
|
|
697
697
|
const shareCounts = new Array(outputSize).fill(0);
|
|
698
698
|
const originalValue = this.value;
|
|
699
699
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
700
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
701
700
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
702
|
-
const outCoords =
|
|
701
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
702
|
+
outCoords[dims] = 0;
|
|
703
703
|
// Convert output coordinates to flat index
|
|
704
704
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
705
705
|
// We collect how many elements share the same min value first
|
|
706
706
|
shareCounts[outFlatIndex] += outputValue[outFlatIndex] === originalValue[realFlatIndex] ? 1 : 0;
|
|
707
707
|
}
|
|
708
708
|
for (let realFlatIndex = 0; realFlatIndex < originalSize; realFlatIndex++) {
|
|
709
|
-
const coords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
710
709
|
// Force 0 on reduced axes to collapse into size-1 dims
|
|
711
|
-
const outCoords =
|
|
710
|
+
const outCoords = Tensor.indexToCoords(realFlatIndex, this.strides);
|
|
711
|
+
outCoords[dims] = 0;
|
|
712
712
|
// Convert output coordinates to flat index
|
|
713
713
|
const outFlatIndex = Tensor.coordsToIndex(outCoords, outputStrides);
|
|
714
714
|
// Here we share the grad between the elements that share the same min value
|
|
@@ -720,6 +720,14 @@ class Tensor {
|
|
|
720
720
|
}
|
|
721
721
|
return keepDims ? out : out.squeeze(dims);
|
|
722
722
|
}
|
|
723
|
+
// Tensor all condition reduction
|
|
724
|
+
all(dims, keepDims = false) {
|
|
725
|
+
return this.min(dims, keepDims).ne(0);
|
|
726
|
+
}
|
|
727
|
+
// Tensor any condition reduction
|
|
728
|
+
any(dims, keepDims = false) {
|
|
729
|
+
return this.max(dims, keepDims).ne(0);
|
|
730
|
+
}
|
|
723
731
|
// Tensor variance reduction
|
|
724
732
|
var(dims, keepDims = false) {
|
|
725
733
|
const meanXSquared = this.square().mean(dims, keepDims);
|