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

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

Comments

Loading...