-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test to check if lr_scheduler is working as expected
- Loading branch information
Jacob Mathias Schreiner
committed
Jan 27, 2025
1 parent
14ab353
commit 9824d82
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Third-party | ||
import pytorch_lightning as pl | ||
import torch | ||
|
||
# First-party | ||
from neural_lam import config as nlconfig | ||
from neural_lam.models.ar_model import ARModel | ||
|
||
|
||
class ARModelWithParams(ARModel): | ||
def __init__(self, args, datastore, config): | ||
super().__init__(args=args, datastore=datastore, config=config) | ||
self.layer = torch.nn.Linear(1, 1) | ||
|
||
|
||
def test_lr_scheduler_reduces_lr(model_args, datastore): | ||
yaml_str = """ | ||
datastore: | ||
kind: mdp | ||
config_path: "" | ||
training: | ||
optimization: | ||
lr: 1 | ||
lr_scheduler: ExponentialLR | ||
lr_scheduler_kwargs: | ||
gamma: 0.5 | ||
""" | ||
config = nlconfig.NeuralLAMConfig.from_yaml(yaml_str) | ||
|
||
model = ARModelWithParams( | ||
args=model_args, datastore=datastore, config=config | ||
) | ||
[optimizer], [lr_scheduler] = model.configure_optimizers() | ||
|
||
assert optimizer.param_groups[0]["lr"] == 1 | ||
lr_scheduler.step() | ||
assert optimizer.param_groups[0]["lr"] == 0.5 | ||
lr_scheduler.step() | ||
assert optimizer.param_groups[0]["lr"] == 0.25 |