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

loss_loggings_test.py 4.3 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  1. import unittest
  2. from torch import Tensor
  3. from torchmetrics import Accuracy
  4. import torch
  5. from super_gradients import Trainer
  6. from super_gradients.common.object_names import Models
  7. from super_gradients.training import models
  8. from super_gradients.training.dataloaders.dataloaders import classification_test_dataloader
  9. class CriterionWithUnnamedComponents(torch.nn.CrossEntropyLoss):
  10. def __init__(self):
  11. super(CriterionWithUnnamedComponents, self).__init__()
  12. def forward(self, input: Tensor, target: Tensor) -> tuple:
  13. loss = super(CriterionWithUnnamedComponents, self).forward(input=input, target=target)
  14. items = torch.cat((loss.unsqueeze(0), loss.unsqueeze(0))).detach()
  15. return loss, items
  16. class CriterionWithNamedComponents(CriterionWithUnnamedComponents):
  17. def __init__(self):
  18. super(CriterionWithNamedComponents, self).__init__()
  19. self.component_names = ["loss_A", "loss_B"]
  20. class LossLoggingsTest(unittest.TestCase):
  21. def test_single_item_logging(self):
  22. trainer = Trainer("test_single_item_logging", model_checkpoints_location="local")
  23. dataloader = classification_test_dataloader(batch_size=10)
  24. model = models.get(Models.RESNET18, arch_params={"num_classes": 5})
  25. train_params = {
  26. "max_epochs": 1,
  27. "lr_updates": [1],
  28. "lr_decay_factor": 0.1,
  29. "lr_mode": "StepLRScheduler",
  30. "lr_warmup_epochs": 0,
  31. "initial_lr": 0.1,
  32. "loss": torch.nn.CrossEntropyLoss(),
  33. "optimizer": "SGD",
  34. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  35. "train_metrics_list": [Accuracy()],
  36. "valid_metrics_list": [Accuracy()],
  37. "metric_to_watch": "Accuracy",
  38. "greater_metric_to_watch_is_better": True,
  39. }
  40. trainer.train(model=model, training_params=train_params, train_loader=dataloader, valid_loader=dataloader)
  41. self.assertListEqual(trainer.loss_logging_items_names, ["CrossEntropyLoss"])
  42. def test_multiple_unnamed_components_loss_logging(self):
  43. trainer = Trainer("test_multiple_unnamed_components_loss_logging", model_checkpoints_location="local")
  44. dataloader = classification_test_dataloader(batch_size=10)
  45. model = models.get(Models.RESNET18, arch_params={"num_classes": 5})
  46. train_params = {
  47. "max_epochs": 1,
  48. "lr_updates": [1],
  49. "lr_decay_factor": 0.1,
  50. "lr_mode": "StepLRScheduler",
  51. "lr_warmup_epochs": 0,
  52. "initial_lr": 0.1,
  53. "loss": CriterionWithUnnamedComponents(),
  54. "optimizer": "SGD",
  55. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  56. "train_metrics_list": [Accuracy()],
  57. "valid_metrics_list": [Accuracy()],
  58. "metric_to_watch": "Accuracy",
  59. "greater_metric_to_watch_is_better": True,
  60. }
  61. trainer.train(model=model, training_params=train_params, train_loader=dataloader, valid_loader=dataloader)
  62. self.assertListEqual(trainer.loss_logging_items_names, ["CriterionWithUnnamedComponents/loss_0", "CriterionWithUnnamedComponents/loss_1"])
  63. def test_multiple_named_components_loss_logging(self):
  64. trainer = Trainer("test_multiple_named_components_loss_logging", model_checkpoints_location="local")
  65. dataloader = classification_test_dataloader(batch_size=10)
  66. model = models.get(Models.RESNET18, arch_params={"num_classes": 5})
  67. train_params = {
  68. "max_epochs": 1,
  69. "lr_updates": [1],
  70. "lr_decay_factor": 0.1,
  71. "lr_mode": "StepLRScheduler",
  72. "lr_warmup_epochs": 0,
  73. "initial_lr": 0.1,
  74. "loss": CriterionWithNamedComponents(),
  75. "optimizer": "SGD",
  76. "optimizer_params": {"weight_decay": 1e-4, "momentum": 0.9},
  77. "train_metrics_list": [Accuracy()],
  78. "valid_metrics_list": [Accuracy()],
  79. "metric_to_watch": "Accuracy",
  80. "greater_metric_to_watch_is_better": True,
  81. }
  82. trainer.train(model=model, training_params=train_params, train_loader=dataloader, valid_loader=dataloader)
  83. self.assertListEqual(trainer.loss_logging_items_names, ["CriterionWithNamedComponents/loss_A", "CriterionWithNamedComponents/loss_B"])
  84. if __name__ == "__main__":
  85. unittest.main()
Tip!

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

Comments

Loading...