catniff 0.8.15 → 0.8.17

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,43 @@ 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;
32
+ }
33
+ export interface Scheduler {
34
+ step: Function;
35
+ }
36
+ export declare class SequentialLR {
37
+ optimizer: OptimizerWithLR;
38
+ schedulers: Scheduler[];
39
+ milestones: number[];
40
+ lastEpoch: number;
41
+ constructor(optimizer: OptimizerWithLR, schedulers: Scheduler[], milestones: number[], lastEpoch?: number);
42
+ step(): void;
11
43
  }
12
44
  export declare const LRScheduler: {
13
45
  StepLR: typeof StepLR;
46
+ LinearLR: typeof LinearLR;
47
+ CosineAnnealingLR: typeof CosineAnnealingLR;
48
+ SequentialLR: typeof SequentialLR;
14
49
  };
@@ -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.SequentialLR = exports.CosineAnnealingLR = exports.LinearLR = exports.StepLR = void 0;
4
4
  class StepLR {
5
5
  optimizer;
6
6
  stepSize;
@@ -16,23 +16,101 @@ 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;
87
+ class SequentialLR {
88
+ optimizer;
89
+ schedulers;
90
+ milestones;
91
+ lastEpoch;
92
+ constructor(optimizer, schedulers, milestones, lastEpoch = -1) {
93
+ this.optimizer = optimizer;
94
+ this.schedulers = schedulers;
95
+ this.milestones = milestones;
96
+ this.lastEpoch = lastEpoch;
97
+ }
98
+ step() {
99
+ this.lastEpoch++;
100
+ let schedulerIndex = this.schedulers.length - 1; // default to last
101
+ for (let index = 0; index < this.milestones.length; index++) {
102
+ if (this.lastEpoch < this.milestones[index]) {
103
+ schedulerIndex = index;
104
+ break;
105
+ }
106
+ }
107
+ this.schedulers[schedulerIndex].step();
108
+ }
109
+ }
110
+ exports.SequentialLR = SequentialLR;
36
111
  exports.LRScheduler = {
37
- StepLR
112
+ StepLR,
113
+ LinearLR,
114
+ CosineAnnealingLR,
115
+ SequentialLR
38
116
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "catniff",
3
- "version": "0.8.15",
3
+ "version": "0.8.17",
4
4
  "description": "Torch-like deep learning framework for Javascript",
5
5
  "main": "./dist/index.js",
6
6
  "scripts": {