Tianshou/tianshou/utils/lr_scheduler.py
Michael Panchenko 2cc34fb72b
Poetry install, remove gym, bump python (#925)
Closes #914 

Additional changes:

- Deprecate python below 11
- Remove 3rd party and throughput tests. This simplifies install and
test pipeline
- Remove gym compatibility and shimmy
- Format with 3.11 conventions. In particular, add `zip(...,
strict=True/False)` where possible

Since the additional tests and gym were complicating the CI pipeline
(flaky and dist-dependent), it didn't make sense to work on fixing the
current tests in this PR to then just delete them in the next one. So
this PR changes the build and removes these tests at the same time.
2023-09-05 14:34:23 -07:00

41 lines
1.4 KiB
Python

import torch
class MultipleLRSchedulers:
"""A wrapper for multiple learning rate schedulers.
Every time :meth:`~tianshou.utils.MultipleLRSchedulers.step` is called,
it calls the step() method of each of the schedulers that it contains.
Example usage:
::
scheduler1 = ConstantLR(opt1, factor=0.1, total_iters=2)
scheduler2 = ExponentialLR(opt2, gamma=0.9)
scheduler = MultipleLRSchedulers(scheduler1, scheduler2)
policy = PPOPolicy(..., lr_scheduler=scheduler)
"""
def __init__(self, *args: torch.optim.lr_scheduler.LambdaLR):
self.schedulers = args
def step(self) -> None:
"""Take a step in each of the learning rate schedulers."""
for scheduler in self.schedulers:
scheduler.step()
def state_dict(self) -> list[dict]:
"""Get state_dict for each of the learning rate schedulers.
:return: A list of state_dict of learning rate schedulers.
"""
return [s.state_dict() for s in self.schedulers]
def load_state_dict(self, state_dict: list[dict]) -> None:
"""Load states from state_dict.
:param List[Dict] state_dict: A list of learning rate scheduler
state_dict, in the same order as the schedulers.
"""
for s, sd in zip(self.schedulers, state_dict, strict=True):
s.__dict__.update(sd)