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

lr_cooldown_test.py 1.9 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
  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.callbacks import TestLRCallback
  7. import numpy as np
  8. class LRCooldownTest(unittest.TestCase):
  9. def test_lr_cooldown_with_lr_scheduling(self):
  10. # Define Model
  11. net = LeNet()
  12. trainer = Trainer("lr_warmup_test")
  13. lrs = []
  14. phase_callbacks = [TestLRCallback(lr_placeholder=lrs)]
  15. train_params = {
  16. "max_epochs": 7,
  17. "cosine_final_lr_ratio": 0.2,
  18. "lr_mode": "CosineLRScheduler",
  19. "lr_cooldown_epochs": 2,
  20. "lr_warmup_epochs": 3,
  21. "initial_lr": 1,
  22. "loss": "CrossEntropyLoss",
  23. "optimizer": "SGD",
  24. "criterion_params": {},
  25. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  26. "train_metrics_list": [Accuracy()],
  27. "valid_metrics_list": [Accuracy()],
  28. "metric_to_watch": "Accuracy",
  29. "greater_metric_to_watch_is_better": True,
  30. "ema": False,
  31. "phase_callbacks": phase_callbacks,
  32. }
  33. expected_lrs = [0.25, 0.5, 0.75, 0.9236067977499791, 0.4763932022500211, 0.4763932022500211, 0.4763932022500211]
  34. trainer.train(
  35. model=net,
  36. training_params=train_params,
  37. train_loader=classification_test_dataloader(dataset_size=5, batch_size=4),
  38. valid_loader=classification_test_dataloader(dataset_size=5, batch_size=4),
  39. )
  40. # ALTHOUGH NOT SEEN IN HERE, THE 4TH EPOCH USES LR=1, SO THIS IS THE EXPECTED LIST AS WE COLLECT
  41. # THE LRS AFTER THE UPDATE
  42. np.testing.assert_allclose(np.array(lrs), np.array(expected_lrs), rtol=1e-6)
Tip!

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

Comments

Loading...