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

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

Comments

Loading...