catniff 0.6.12 → 0.6.14
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.js +39 -33
- package/dist/optim.js +1 -1
- package/package.json +1 -1
package/dist/core.js
CHANGED
|
@@ -185,14 +185,22 @@ class Tensor {
|
|
|
185
185
|
static elementWiseSelf(tA, op) {
|
|
186
186
|
if (typeof tA.value === "number")
|
|
187
187
|
return new Tensor(op(tA.value));
|
|
188
|
+
const contiguous = tA.isContiguous();
|
|
188
189
|
const outputShape = tA.shape;
|
|
189
|
-
const outputStrides = Tensor.getStrides(outputShape);
|
|
190
|
+
const outputStrides = contiguous ? tA.strides : Tensor.getStrides(outputShape);
|
|
190
191
|
const outputSize = tA.numel;
|
|
191
192
|
const outputValue = new Array(outputSize);
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
193
|
+
if (contiguous) {
|
|
194
|
+
for (let index = 0; index < outputSize; index++) {
|
|
195
|
+
outputValue[index] = op(tA.value[index + tA.offset]);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
for (let index = 0; index < outputSize; index++) {
|
|
200
|
+
const outputCoords = Tensor.indexToCoords(index, outputStrides);
|
|
201
|
+
const originalIndex = tA.offset + Tensor.coordsToIndex(outputCoords, tA.strides);
|
|
202
|
+
outputValue[index] = op(tA.value[originalIndex]);
|
|
203
|
+
}
|
|
196
204
|
}
|
|
197
205
|
return new Tensor(outputValue, { shape: outputShape, strides: outputStrides, numel: tA.numel });
|
|
198
206
|
}
|
|
@@ -291,9 +299,6 @@ class Tensor {
|
|
|
291
299
|
// Contiguity-related ops
|
|
292
300
|
isContiguous() {
|
|
293
301
|
const expectedStrides = Tensor.getStrides(this.shape);
|
|
294
|
-
if (expectedStrides.length !== this.strides.length) {
|
|
295
|
-
return false;
|
|
296
|
-
}
|
|
297
302
|
for (let i = 0; i < this.strides.length; i++) {
|
|
298
303
|
if (this.strides[i] !== expectedStrides[i]) {
|
|
299
304
|
return false;
|
|
@@ -342,6 +347,7 @@ class Tensor {
|
|
|
342
347
|
const out = new Tensor(this.value, {
|
|
343
348
|
shape: newShape,
|
|
344
349
|
strides: outputStrides,
|
|
350
|
+
offset: this.offset,
|
|
345
351
|
numel: outputSize,
|
|
346
352
|
device: this.device
|
|
347
353
|
});
|
|
@@ -356,28 +362,7 @@ class Tensor {
|
|
|
356
362
|
return out;
|
|
357
363
|
}
|
|
358
364
|
reshape(newShape) {
|
|
359
|
-
|
|
360
|
-
const originalSize = this.numel;
|
|
361
|
-
const outputSize = Tensor.shapeToSize(newShape);
|
|
362
|
-
if (originalSize !== outputSize || typeof this.value === "number") {
|
|
363
|
-
throw new Error("Can not reshape: incompatible sizes");
|
|
364
|
-
}
|
|
365
|
-
// Create new tensor with forced compatibility (only contiguity for now)
|
|
366
|
-
const outputStrides = Tensor.getStrides(newShape);
|
|
367
|
-
const out = new Tensor(this.contiguous().value, {
|
|
368
|
-
shape: newShape,
|
|
369
|
-
strides: outputStrides,
|
|
370
|
-
numel: outputSize
|
|
371
|
-
});
|
|
372
|
-
// Gradient reshaped and flow back to the original tensor
|
|
373
|
-
if (this.requiresGrad) {
|
|
374
|
-
out.requiresGrad = true;
|
|
375
|
-
out.children.push(this);
|
|
376
|
-
out.gradFn = () => {
|
|
377
|
-
Tensor.addGrad(this, out.grad.reshape(this.shape));
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
return out;
|
|
365
|
+
return this.contiguous().view(newShape);
|
|
381
366
|
}
|
|
382
367
|
flatten(startDim = 0, endDim = -1) {
|
|
383
368
|
// Handle negative indices
|
|
@@ -552,7 +537,7 @@ class Tensor {
|
|
|
552
537
|
}
|
|
553
538
|
// Tensor indexing
|
|
554
539
|
index(indices) {
|
|
555
|
-
const tensorIndices = this.handleOther(indices).
|
|
540
|
+
const tensorIndices = this.handleOther(indices).clone();
|
|
556
541
|
if (typeof tensorIndices.value === "number") {
|
|
557
542
|
return this.indexWithArray([tensorIndices.value]).squeeze(0);
|
|
558
543
|
}
|
|
@@ -1856,8 +1841,29 @@ class Tensor {
|
|
|
1856
1841
|
}
|
|
1857
1842
|
// Returns a copy of the tensor (with new data allocation) and keeps grad connection
|
|
1858
1843
|
clone() {
|
|
1859
|
-
|
|
1860
|
-
|
|
1844
|
+
let out;
|
|
1845
|
+
if (typeof this.value === "number") {
|
|
1846
|
+
out = new Tensor(this.value);
|
|
1847
|
+
}
|
|
1848
|
+
else {
|
|
1849
|
+
const contiguous = this.isContiguous();
|
|
1850
|
+
const outputStrides = contiguous ? this.strides : Tensor.getStrides(this.shape);
|
|
1851
|
+
const outputSize = this.numel;
|
|
1852
|
+
const outputValue = new Array(outputSize);
|
|
1853
|
+
if (contiguous) {
|
|
1854
|
+
for (let index = 0; index < outputSize; index++) {
|
|
1855
|
+
outputValue[index] = this.value[this.offset + index];
|
|
1856
|
+
}
|
|
1857
|
+
}
|
|
1858
|
+
else {
|
|
1859
|
+
for (let index = 0; index < outputSize; index++) {
|
|
1860
|
+
const outputCoords = Tensor.indexToCoords(index, outputStrides);
|
|
1861
|
+
const originalIndex = Tensor.coordsToIndex(outputCoords, this.strides);
|
|
1862
|
+
outputValue[index] = this.value[this.offset + originalIndex];
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
out = new Tensor(outputValue, { shape: this.shape, strides: outputStrides, numel: outputSize });
|
|
1866
|
+
}
|
|
1861
1867
|
if (this.requiresGrad) {
|
|
1862
1868
|
out.requiresGrad = true;
|
|
1863
1869
|
out.children.push(this);
|
package/dist/optim.js
CHANGED
|
@@ -139,7 +139,7 @@ class AdamW extends BaseOptimizer {
|
|
|
139
139
|
this.lr = options?.lr || 0.001;
|
|
140
140
|
this.betas = options?.betas || [0.9, 0.999];
|
|
141
141
|
this.eps = options?.eps || 1e-8;
|
|
142
|
-
this.weightDecay = options?.weightDecay || 0;
|
|
142
|
+
this.weightDecay = options?.weightDecay || 0.01;
|
|
143
143
|
}
|
|
144
144
|
step() {
|
|
145
145
|
this.stepCount++;
|