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

train.py 2.2 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
  1. import os
  2. import sys
  3. import json
  4. import yaml
  5. import torch
  6. import importlib
  7. import numpy as np
  8. import pandas as pd
  9. from pathlib import Path
  10. from sklearn.model_selection import KFold
  11. from data_loader.data_loaders import DataFrameDataLoader
  12. from dotenv import load_dotenv
  13. import logging
  14. logging.basicConfig(
  15. level=logging.DEBUG,
  16. format="%(asctime)s [%(levelname)s] %(message)s",
  17. handlers=[
  18. logging.FileHandler("debug.log"),
  19. logging.StreamHandler()
  20. ]
  21. )
  22. load_dotenv('envs/.env')
  23. with open('params.yaml', 'r') as f:
  24. PARAMS = yaml.safe_load(f)
  25. config_path = Path(os.getenv('OUTPUT_PATH'), os.getenv('CONFIG_PATH'))
  26. with open(config_path, 'r') as f:
  27. CONFIG = json.load(f)
  28. def start_training(method='lstm'):
  29. try:
  30. model_module = importlib.import_module(f'model.{method}')
  31. model = model_module.Model(**CONFIG, **PARAMS[method])
  32. except Exception as e:
  33. raise e
  34. if torch.cuda.is_available():
  35. device = torch.device('cuda', PARAMS.get('gpu', 0))
  36. else:
  37. device = torch.device('cpu')
  38. model.to(device)
  39. try:
  40. trainer_module = importlib.import_module(f'training.{method}')
  41. trainer = trainer_module.Trainer(model, method, mode='train')
  42. except Exception as e:
  43. raise e
  44. df = pd.read_csv('data/all.csv')
  45. dataloader = DataFrameDataLoader(
  46. df, batch_size=PARAMS['train']['batch_size'],
  47. shuffle=PARAMS['train']['shuffle'], use_bag=PARAMS[method]['use_bag'],
  48. use_eos=PARAMS[method].get('use_eos'), max_len=PARAMS[method].get('max_len')
  49. )
  50. trainer.set_dataloader(dataloader)
  51. results, losses = trainer.train()
  52. columns = list(losses[0].keys())
  53. losses_df = pd.DataFrame(losses, columns=columns)
  54. return results, losses_df
  55. if __name__ == '__main__':
  56. method = sys.argv[1]
  57. try:
  58. results, losses_df = start_training(method)
  59. except Exception as e:
  60. logging.error(e)
  61. raise e
  62. results_path = Path(os.getenv('OUTPUT_PATH'), f'{method}_{os.getenv("RESULTS_PATH")}')
  63. with open(results_path, 'w') as f:
  64. json.dump(results, f)
  65. plots_path = Path(os.getenv('OUTPUT_PATH'), f'{method}_{os.getenv("PLOTS_PATH")}')
  66. losses_df.to_csv(plots_path, index=False)
Tip!

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

Comments

Loading...