catniff 0.8.15 → 0.8.16

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.
@@ -7,8 +7,31 @@ export declare class StepLR {
7
7
  baseLR: number;
8
8
  baseGroupLRs: number[];
9
9
  constructor(optimizer: OptimizerWithLR, stepSize: number, gamma?: number, lastEpoch?: number);
10
- step(epoch?: number): void;
10
+ step(): void;
11
+ }
12
+ export declare class LinearLR {
13
+ optimizer: OptimizerWithLR;
14
+ startFactor: number;
15
+ endFactor: number;
16
+ totalIters: number;
17
+ lastEpoch: number;
18
+ baseLR: number;
19
+ baseGroupLRs: number[];
20
+ constructor(optimizer: OptimizerWithLR, startFactor?: number, endFactor?: number, totalIters?: number, lastEpoch?: number);
21
+ step(): void;
22
+ }
23
+ export declare class CosineAnnealingLR {
24
+ optimizer: OptimizerWithLR;
25
+ TMax: number;
26
+ etaMin: number;
27
+ lastEpoch: number;
28
+ baseLR: number;
29
+ baseGroupLRs: number[];
30
+ constructor(optimizer: OptimizerWithLR, TMax: number, etaMin?: number, lastEpoch?: number);
31
+ step(): void;
11
32
  }
12
33
  export declare const LRScheduler: {
13
34
  StepLR: typeof StepLR;
35
+ LinearLR: typeof LinearLR;
36
+ CosineAnnealingLR: typeof CosineAnnealingLR;
14
37
  };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LRScheduler = exports.StepLR = void 0;
3
+ exports.LRScheduler = exports.CosineAnnealingLR = exports.LinearLR = exports.StepLR = void 0;
4
4
  class StepLR {
5
5
  optimizer;
6
6
  stepSize;
@@ -16,23 +16,76 @@ class StepLR {
16
16
  this.baseLR = optimizer.lr;
17
17
  this.baseGroupLRs = this.optimizer.paramGroups.map(paramGroup => paramGroup.lr ?? this.optimizer.lr);
18
18
  }
19
- step(epoch) {
20
- if (typeof epoch === "undefined") {
21
- this.lastEpoch++;
22
- epoch = this.lastEpoch;
23
- }
24
- else {
25
- this.lastEpoch = epoch;
26
- }
19
+ step() {
20
+ this.lastEpoch++;
27
21
  // Update LR of each group
28
22
  for (let index = 0; index < this.baseGroupLRs.length; index++) {
29
- this.optimizer.paramGroups[index].lr = this.baseGroupLRs[index] * this.gamma ** Math.floor(epoch / this.stepSize);
23
+ this.optimizer.paramGroups[index].lr = this.baseGroupLRs[index] * this.gamma ** Math.floor(this.lastEpoch / this.stepSize);
30
24
  }
31
25
  // Update default LR
32
- this.optimizer.lr = this.baseLR * this.gamma ** Math.floor(epoch / this.stepSize);
26
+ this.optimizer.lr = this.baseLR * this.gamma ** Math.floor(this.lastEpoch / this.stepSize);
33
27
  }
34
28
  }
35
29
  exports.StepLR = StepLR;
30
+ class LinearLR {
31
+ optimizer;
32
+ startFactor;
33
+ endFactor;
34
+ totalIters;
35
+ lastEpoch;
36
+ baseLR;
37
+ baseGroupLRs;
38
+ constructor(optimizer, startFactor = 0.3333333333333333, endFactor = 1, totalIters = 5, lastEpoch = -1) {
39
+ this.optimizer = optimizer;
40
+ this.startFactor = startFactor;
41
+ this.endFactor = endFactor;
42
+ this.totalIters = totalIters;
43
+ this.lastEpoch = lastEpoch;
44
+ this.baseLR = optimizer.lr;
45
+ this.baseGroupLRs = this.optimizer.paramGroups.map(paramGroup => paramGroup.lr ?? this.optimizer.lr);
46
+ }
47
+ step() {
48
+ this.lastEpoch++;
49
+ // Clamp under total allowed iterations
50
+ const t = Math.min(this.lastEpoch, this.totalIters);
51
+ // Precalculate factor
52
+ const factor = this.startFactor + (t / this.totalIters) * (this.endFactor - this.startFactor);
53
+ // Update LR of each group
54
+ for (let index = 0; index < this.baseGroupLRs.length; index++) {
55
+ this.optimizer.paramGroups[index].lr = this.baseGroupLRs[index] * factor;
56
+ }
57
+ // Update default LR
58
+ this.optimizer.lr = this.baseLR * factor;
59
+ }
60
+ }
61
+ exports.LinearLR = LinearLR;
62
+ class CosineAnnealingLR {
63
+ optimizer;
64
+ TMax;
65
+ etaMin;
66
+ lastEpoch;
67
+ baseLR;
68
+ baseGroupLRs;
69
+ constructor(optimizer, TMax, etaMin = 0, lastEpoch = -1) {
70
+ this.optimizer = optimizer;
71
+ this.TMax = TMax;
72
+ this.etaMin = etaMin;
73
+ this.lastEpoch = lastEpoch;
74
+ this.baseLR = optimizer.lr;
75
+ this.baseGroupLRs = this.optimizer.paramGroups.map(paramGroup => paramGroup.lr ?? this.optimizer.lr);
76
+ }
77
+ step() {
78
+ this.lastEpoch++;
79
+ const cosine = (1 + Math.cos((this.lastEpoch * Math.PI) / this.TMax)) / 2;
80
+ for (let index = 0; index < this.baseGroupLRs.length; index++) {
81
+ this.optimizer.paramGroups[index].lr = this.etaMin + (this.baseGroupLRs[index] - this.etaMin) * cosine;
82
+ }
83
+ this.optimizer.lr = this.etaMin + (this.baseLR - this.etaMin) * cosine;
84
+ }
85
+ }
86
+ exports.CosineAnnealingLR = CosineAnnealingLR;
36
87
  exports.LRScheduler = {
37
- StepLR
88
+ StepLR,
89
+ LinearLR,
90
+ CosineAnnealingLR
38
91
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.8.15",
3
+ "version": "0.8.16",
4
4
  "description": "Torch-like deep learning framework for Javascript",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {