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

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

Comments

Loading...