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_test.py 3.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  1. import shutil
  2. import unittest
  3. import os
  4. from super_gradients.common.object_names import Models
  5. from super_gradients.training import models
  6. from super_gradients import Trainer
  7. from super_gradients.training.dataloaders.dataloaders import classification_test_dataloader
  8. from super_gradients.training.metrics import Accuracy, Top5
  9. class LRTest(unittest.TestCase):
  10. @classmethod
  11. def setUp(cls):
  12. # NAMES FOR THE EXPERIMENTS TO LATER DELETE
  13. cls.folder_name = "lr_test"
  14. cls.training_params = {
  15. "max_epochs": 1,
  16. "silent_mode": True,
  17. "initial_lr": 0.1,
  18. "loss": "CrossEntropyLoss",
  19. "train_metrics_list": [Accuracy(), Top5()],
  20. "valid_metrics_list": [Accuracy(), Top5()],
  21. "metric_to_watch": "Accuracy",
  22. "greater_metric_to_watch_is_better": True,
  23. }
  24. @classmethod
  25. def tearDownClass(cls) -> None:
  26. # ERASE THE FOLDER THAT WAS CREATED DURING THIS TEST
  27. if os.path.isdir(os.path.join("checkpoints", cls.folder_name)):
  28. shutil.rmtree(os.path.join("checkpoints", cls.folder_name))
  29. @staticmethod
  30. def get_trainer(name=""):
  31. trainer = Trainer(name)
  32. model = models.get(Models.RESNET18_CIFAR, num_classes=5)
  33. return trainer, model
  34. def test_function_lr(self):
  35. trainer, model = self.get_trainer(self.folder_name)
  36. def test_lr_function(initial_lr, epoch, iter, max_epoch, iters_per_epoch, **kwargs):
  37. return initial_lr * (1 - ((epoch * iters_per_epoch + iter) / (max_epoch * iters_per_epoch)))
  38. # test if we are able that lr_function supports functions with this structure
  39. training_params = {**self.training_params, "lr_mode": "FunctionLRScheduler", "lr_schedule_function": test_lr_function}
  40. trainer.train(
  41. model=model, training_params=training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  42. )
  43. # test that we assert lr_function is callable
  44. training_params = {**self.training_params, "lr_mode": "FunctionLRScheduler"}
  45. with self.assertRaises(AssertionError):
  46. trainer.train(
  47. model=model, training_params=training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  48. )
  49. def test_cosine_lr(self):
  50. trainer, model = self.get_trainer(self.folder_name)
  51. training_params = {**self.training_params, "lr_mode": "CosineLRScheduler", "cosine_final_lr_ratio": 0.01}
  52. trainer.train(
  53. model=model, training_params=training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  54. )
  55. def test_step_lr(self):
  56. trainer, model = self.get_trainer(self.folder_name)
  57. training_params = {**self.training_params, "lr_mode": "StepLRScheduler", "lr_decay_factor": 0.1, "lr_updates": [4]}
  58. trainer.train(
  59. model=model, training_params=training_params, train_loader=classification_test_dataloader(), valid_loader=classification_test_dataloader()
  60. )
  61. if __name__ == "__main__":
  62. unittest.main()
Tip!

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

Comments

Loading...