catniff 0.8.13 → 0.8.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 CHANGED
@@ -20,21 +20,21 @@ class Tensor {
20
20
  static createGraph = false;
21
21
  constructor(value, options = {}) {
22
22
  // Memory buffer
23
- this.dtype = options.dtype || "float32";
23
+ this.dtype = options.dtype ?? "float32";
24
24
  const flatValue = Tensor.flattenValue(value);
25
25
  const TypedArrayConstructor = dtype_1.TypedArray[this.dtype];
26
26
  this.value = flatValue instanceof TypedArrayConstructor ? flatValue : TypedArrayConstructor.from(flatValue);
27
27
  // Tensor metadata
28
- this.shape = options.shape || Tensor.getShape(value);
29
- this.strides = options.strides || Tensor.getStrides(this.shape);
30
- this.offset = options.offset || 0;
31
- this.numel = options.numel || Tensor.shapeToSize(this.shape);
32
- this.device = options.device || "cpu";
28
+ this.shape = options.shape ?? Tensor.getShape(value);
29
+ this.strides = options.strides ?? Tensor.getStrides(this.shape);
30
+ this.offset = options.offset ?? 0;
31
+ this.numel = options.numel ?? Tensor.shapeToSize(this.shape);
32
+ this.device = options.device ?? "cpu";
33
33
  // Autograd data
34
34
  this.grad = options.grad;
35
35
  this.requiresGrad = options.requiresGrad ?? false;
36
- this.gradFn = options.gradFn || (() => { });
37
- this.children = options.children || [];
36
+ this.gradFn = options.gradFn ?? (() => { });
37
+ this.children = options.children ?? [];
38
38
  // Move to device in-place
39
39
  this.to_(this.device);
40
40
  }
@@ -622,14 +622,14 @@ class Tensor {
622
622
  return this;
623
623
  const newShape = [];
624
624
  const newStrides = [];
625
- let newOffset = this.offset || 0;
625
+ let newOffset = this.offset;
626
626
  // Pad ranges to match tensor dimensions
627
627
  const paddedRanges = [...ranges];
628
628
  while (paddedRanges.length < this.shape.length) {
629
629
  paddedRanges.push([]);
630
630
  }
631
631
  for (let i = 0; i < this.shape.length; i++) {
632
- const range = paddedRanges[i] || [];
632
+ const range = paddedRanges[i] ?? [];
633
633
  const dimSize = this.shape[i];
634
634
  const stride = this.strides[i];
635
635
  // Default values
@@ -675,7 +675,7 @@ class Tensor {
675
675
  const originalCoords = new Array(slicedCoords.length);
676
676
  for (let dim = 0; dim < slicedCoords.length; dim++) {
677
677
  const coord = slicedCoords[dim];
678
- const range = paddedRanges[dim] || [];
678
+ const range = paddedRanges[dim] ?? [];
679
679
  const start = range[0] ?? 0;
680
680
  const step = range[2] ?? 1;
681
681
  const normalizedStart = start < 0 ? start + this.shape[dim] : start;
@@ -1,11 +1,11 @@
1
- import { BaseOptimizer } from "./optim";
1
+ import { OptimizerWithLR } from "./optim";
2
2
  export declare class StepLR {
3
- optimizer: BaseOptimizer;
3
+ optimizer: OptimizerWithLR;
4
4
  stepSize: number;
5
5
  gamma: number;
6
6
  lastEpoch: number;
7
7
  baseLR: number;
8
- constructor(optimizer: BaseOptimizer, stepSize: number, gamma?: number, lastEpoch?: number);
8
+ constructor(optimizer: OptimizerWithLR, stepSize: number, gamma?: number, lastEpoch?: number);
9
9
  step(epoch?: number): void;
10
10
  }
11
11
  export declare const LRScheduler: {
package/dist/optim.d.ts CHANGED
@@ -4,7 +4,6 @@ export interface BaseOptimizerOptions {
4
4
  }
5
5
  export declare abstract class BaseOptimizer {
6
6
  params: Tensor[];
7
- lr: number;
8
7
  constructor(params: Tensor[], options?: BaseOptimizerOptions);
9
8
  zeroGrad(): void;
10
9
  }
@@ -15,7 +14,11 @@ export interface SGDOptions {
15
14
  weightDecay?: number;
16
15
  nesterov?: boolean;
17
16
  }
17
+ export interface OptimizerWithLR extends BaseOptimizer {
18
+ lr: number;
19
+ }
18
20
  export declare class SGD extends BaseOptimizer {
21
+ lr: number;
19
22
  momentumBuffers: Map<Tensor, Tensor>;
20
23
  momentum: number;
21
24
  dampening: number;
@@ -31,6 +34,7 @@ export interface AdamOptions {
31
34
  weightDecay?: number;
32
35
  }
33
36
  export declare class Adam extends BaseOptimizer {
37
+ lr: number;
34
38
  momentumBuffers: Map<Tensor, Tensor>;
35
39
  velocityBuffers: Map<Tensor, Tensor>;
36
40
  stepCount: number;
@@ -47,6 +51,7 @@ export interface AdamWOptions {
47
51
  weightDecay?: number;
48
52
  }
49
53
  export declare class AdamW extends BaseOptimizer {
54
+ lr: number;
50
55
  momentumBuffers: Map<Tensor, Tensor>;
51
56
  velocityBuffers: Map<Tensor, Tensor>;
52
57
  stepCount: number;
package/dist/optim.js CHANGED
@@ -4,10 +4,8 @@ exports.Optim = exports.AdamW = exports.Adam = exports.SGD = exports.BaseOptimiz
4
4
  const core_1 = require("./core");
5
5
  class BaseOptimizer {
6
6
  params;
7
- lr;
8
7
  constructor(params, options) {
9
8
  this.params = params;
10
- this.lr = options?.lr || 0.001;
11
9
  }
12
10
  zeroGrad() {
13
11
  for (let index = 0; index < this.params.length; index++) {
@@ -18,6 +16,7 @@ class BaseOptimizer {
18
16
  }
19
17
  exports.BaseOptimizer = BaseOptimizer;
20
18
  class SGD extends BaseOptimizer {
19
+ lr;
21
20
  momentumBuffers = new Map();
22
21
  momentum;
23
22
  dampening;
@@ -25,10 +24,11 @@ class SGD extends BaseOptimizer {
25
24
  nesterov;
26
25
  constructor(params, options) {
27
26
  super(params, options);
28
- this.momentum = options?.momentum || 0;
29
- this.dampening = options?.dampening || 0;
30
- this.weightDecay = options?.weightDecay || 0;
31
- this.nesterov = options?.nesterov || false;
27
+ this.lr = options?.lr ?? 0.001;
28
+ this.momentum = options?.momentum ?? 0;
29
+ this.dampening = options?.dampening ?? 0;
30
+ this.weightDecay = options?.weightDecay ?? 0;
31
+ this.nesterov = options?.nesterov ?? false;
32
32
  }
33
33
  step() {
34
34
  for (const param of this.params) {
@@ -69,6 +69,7 @@ class SGD extends BaseOptimizer {
69
69
  }
70
70
  exports.SGD = SGD;
71
71
  class Adam extends BaseOptimizer {
72
+ lr;
72
73
  momentumBuffers = new Map(); // First moment (m_t)
73
74
  velocityBuffers = new Map(); // Second moment (v_t)
74
75
  stepCount = 0;
@@ -77,9 +78,10 @@ class Adam extends BaseOptimizer {
77
78
  weightDecay;
78
79
  constructor(params, options) {
79
80
  super(params, options);
80
- this.betas = options?.betas || [0.9, 0.999];
81
- this.eps = options?.eps || 1e-8;
82
- this.weightDecay = options?.weightDecay || 0;
81
+ this.lr = options?.lr ?? 0.001;
82
+ this.betas = options?.betas ?? [0.9, 0.999];
83
+ this.eps = options?.eps ?? 1e-8;
84
+ this.weightDecay = options?.weightDecay ?? 0;
83
85
  }
84
86
  step() {
85
87
  this.stepCount++;
@@ -128,6 +130,7 @@ class Adam extends BaseOptimizer {
128
130
  }
129
131
  exports.Adam = Adam;
130
132
  class AdamW extends BaseOptimizer {
133
+ lr;
131
134
  momentumBuffers = new Map(); // First moment (m_t)
132
135
  velocityBuffers = new Map(); // Second moment (v_t)
133
136
  stepCount = 0;
@@ -136,9 +139,10 @@ class AdamW extends BaseOptimizer {
136
139
  weightDecay;
137
140
  constructor(params, options) {
138
141
  super(params, options);
139
- this.betas = options?.betas || [0.9, 0.999];
140
- this.eps = options?.eps || 1e-8;
141
- this.weightDecay = options?.weightDecay || 0.01;
142
+ this.lr = options?.lr ?? 0.001;
143
+ this.betas = options?.betas ?? [0.9, 0.999];
144
+ this.eps = options?.eps ?? 1e-8;
145
+ this.weightDecay = options?.weightDecay ?? 0.01;
142
146
  }
143
147
  step() {
144
148
  this.stepCount++;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.8.13",
3
+ "version": "0.8.14",
4
4
  "description": "Torch-like deep learning framework for Javascript",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {