Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

update_param_groups_unit_test.py 2.1 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  1. import unittest
  2. from super_gradients.training import Trainer
  3. from super_gradients.training.dataloaders.dataloaders import classification_test_dataloader
  4. from super_gradients.training.metrics import Accuracy
  5. from super_gradients.training.models import LeNet
  6. from super_gradients.training.utils import HpmStruct, get_param
  7. from super_gradients.training.utils.callbacks import TestLRCallback
  8. import numpy as np
  9. class TestNet(LeNet):
  10. """
  11. Toy test net with update_param_groups method that hard codes some lr.
  12. """
  13. def __init__(self):
  14. super(TestNet, self).__init__()
  15. def update_param_groups(self, param_groups: list, lr: float, epoch: int, iter: int, training_params: HpmStruct, total_batch: int) -> list:
  16. initial_lr = get_param(training_params, "initial_lr")
  17. for param_group in param_groups:
  18. param_group["lr"] = initial_lr * (epoch + 1)
  19. return param_groups
  20. class UpdateParamGroupsTest(unittest.TestCase):
  21. def test_lr_scheduling_with_update_param_groups(self):
  22. # Define Model
  23. net = TestNet()
  24. trainer = Trainer("lr_warmup_test")
  25. lrs = []
  26. phase_callbacks = [TestLRCallback(lr_placeholder=lrs)]
  27. train_params = {
  28. "max_epochs": 3,
  29. "lr_mode": "step",
  30. "lr_updates": [0, 1, 2],
  31. "initial_lr": 0.1,
  32. "lr_decay_factor": 1,
  33. "loss": "cross_entropy",
  34. "optimizer": "SGD",
  35. "criterion_params": {},
  36. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  37. "train_metrics_list": [Accuracy()],
  38. "valid_metrics_list": [Accuracy()],
  39. "metric_to_watch": "Accuracy",
  40. "greater_metric_to_watch_is_better": True,
  41. "ema": False,
  42. "phase_callbacks": phase_callbacks,
  43. }
  44. expected_lrs = np.array([0.1, 0.2, 0.3])
  45. trainer.train(model=net, training_params=train_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader())
  46. self.assertTrue(np.allclose(np.array(lrs), expected_lrs, rtol=0.0000001))
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...