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

mnist_trainer.py 1.5 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
  1. """
  2. This file runs the main training/val loop, etc... using Lightning Trainer
  3. """
  4. from argparse import ArgumentParser
  5. from pytorch_lightning import Trainer
  6. from dagshub.pytorch_lightning import DAGsHubLogger
  7. from dagshub.pytorch_lightning.utils import read_hparams
  8. def import_model():
  9. """
  10. Ugly hack to be able to import the model from ../mnist_model.py
  11. """
  12. import os
  13. examples_dir = os.path.abspath(os.path.join(os.path.dirname(os.path.abspath(__file__)), '..'))
  14. import sys
  15. sys.path.append(examples_dir)
  16. from mnist_model import MnistModel
  17. return MnistModel
  18. if __name__ == '__main__':
  19. # Read parameters from a versioned file, which should also be a DVC dependency.
  20. # This is the purest use case
  21. hparams_from_file = read_hparams('params.yml')
  22. # OPTIONAL:
  23. # Allow some hyperparameters to be defined in the command line
  24. parser = ArgumentParser(add_help=False)
  25. parser.add_argument('--gpus', type=str, default=None, required=False)
  26. # Parse args from command line, overriding params from file
  27. hparams = parser.parse_args(namespace=hparams_from_file)
  28. MnistModel = import_model()
  29. # init module
  30. model = MnistModel(hparams)
  31. # most basic trainer, uses good defaults
  32. trainer = Trainer(
  33. max_nb_epochs=hparams.max_nb_epochs,
  34. gpus=hparams.gpus,
  35. val_check_interval=0.2,
  36. logger=DAGsHubLogger(should_log_hparams=False), # This is the main point - use the DAGsHub logger!
  37. default_save_path='lightning_logs',
  38. )
  39. trainer.fit(model)
Tip!

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

Comments

Loading...