Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added warmup each cycle feature in CyclicalScheduler #3064

Merged
merged 18 commits into from
Sep 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions ignite/handlers/param_scheduler.py
sihyeong671 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@
cycle_mult: float = 1.0,
start_value_mult: float = 1.0,
end_value_mult: float = 1.0,
warmup_each_cycle: bool = False,
warmup_duration: Optional[int] = None,
save_history: bool = False,
param_group_index: Optional[int] = None,
):
Expand All @@ -313,6 +315,18 @@
self.cycle = 0
self.start_value_mult = start_value_mult
self.end_value_mult = end_value_mult
self.warmup_each_cycle = warmup_each_cycle
if not self.warmup_each_cycle:
if warmup_duration is not None:
warnings.warn(

Check warning on line 321 in ignite/handlers/param_scheduler.py

View check run for this annotation

Codecov / codecov/patch

ignite/handlers/param_scheduler.py#L318-L321

Added lines #L318 - L321 were not covered by tests
sihyeong671 marked this conversation as resolved.
Show resolved Hide resolved
sihyeong671 marked this conversation as resolved.
Show resolved Hide resolved
f"warmup_each_cycle=False but your warmup_duration is {warmup_duration}. "
f"so warmup_duration will be set to 0. "
f"If you want to use warmup each cycle, pleas set warmup_each_cycle=True"
sihyeong671 marked this conversation as resolved.
Show resolved Hide resolved
)
self.warmup_duration = 0

Check warning on line 326 in ignite/handlers/param_scheduler.py

View check run for this annotation

Codecov / codecov/patch

ignite/handlers/param_scheduler.py#L326

Added line #L326 was not covered by tests
else:
self.warmup_duration = warmup_duration
sihyeong671 marked this conversation as resolved.
Show resolved Hide resolved
self.total_cycle_size = self.warmup_duration + self.cycle_size

Check warning on line 329 in ignite/handlers/param_scheduler.py

View check run for this annotation

Codecov / codecov/patch

ignite/handlers/param_scheduler.py#L328-L329

Added lines #L328 - L329 were not covered by tests

if self.cycle_size < 2:
raise ValueError(f"Argument cycle_size should be positive and larger than 1, but given {cycle_size}")
Expand All @@ -325,18 +339,26 @@
"cycle",
"start_value_mult",
"end_value_mult",
"warmup_duration",
]

def __call__(self, engine: Optional[Engine], name: Optional[str] = None) -> None:
if self.event_index != 0 and self.event_index % self.cycle_size == 0:
if self.event_index != 0 and self.event_index % self.total_cycle_size == 0:

Check warning on line 346 in ignite/handlers/param_scheduler.py

View check run for this annotation

Codecov / codecov/patch

ignite/handlers/param_scheduler.py#L346

Added line #L346 was not covered by tests
self.event_index = 0
self.cycle_size = int(self.cycle_size * self.cycle_mult)
self.warmup_duration = int(self.warmup_duration * self.cycle_mult)
self.total_cycle_size = int(self.warmup_duration + self.cycle_size)

Check warning on line 350 in ignite/handlers/param_scheduler.py

View check run for this annotation

Codecov / codecov/patch

ignite/handlers/param_scheduler.py#L349-L350

Added lines #L349 - L350 were not covered by tests
self.cycle += 1
self.start_value *= self.start_value_mult
if self.event_index != 0 and self.event_index == self.warmup_duration:

Check warning on line 353 in ignite/handlers/param_scheduler.py

View check run for this annotation

Codecov / codecov/patch

ignite/handlers/param_scheduler.py#L353

Added line #L353 was not covered by tests
self.end_value *= self.end_value_mult

return super(CyclicalScheduler, self).__call__(engine, name)

def _get_cycle_param(self):
sihyeong671 marked this conversation as resolved.
Show resolved Hide resolved
cycle_progress = (self.event_index - self.warmup_duration) / self.cycle_size
return self.start_value + ((self.end_value - self.start_value) / 2) * (1 - math.cos(math.pi * cycle_progress))

Check warning on line 360 in ignite/handlers/param_scheduler.py

View check run for this annotation

Codecov / codecov/patch

ignite/handlers/param_scheduler.py#L359-L360

Added lines #L359 - L360 were not covered by tests


class LinearCyclicalScheduler(CyclicalScheduler):
"""Linearly adjusts param value to 'end_value' for a half-cycle, then linearly
Expand Down Expand Up @@ -538,8 +560,9 @@

def get_param(self) -> float:
"""Method to get current optimizer's parameter value"""
sihyeong671 marked this conversation as resolved.
Show resolved Hide resolved
cycle_progress = self.event_index / self.cycle_size
return self.start_value + ((self.end_value - self.start_value) / 2) * (1 - math.cos(math.pi * cycle_progress))
if self.warmup_each_cycle and self.event_index < self.warmup_duration:
return self.end_value + (self.start_value - self.end_value) * self.event_index / self.warmup_duration
return self._get_cycle_param()

Check warning on line 565 in ignite/handlers/param_scheduler.py

View check run for this annotation

Codecov / codecov/patch

ignite/handlers/param_scheduler.py#L563-L565

Added lines #L563 - L565 were not covered by tests


class ConcatScheduler(ParamScheduler):
Expand Down
Loading