-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformers_model_trainer.py
166 lines (134 loc) · 3.9 KB
/
transformers_model_trainer.py
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import typing as tp
import pandas as pd
from torch.utils.data import Dataset
class CustomDataset(Dataset):
"""
This class designs logic to retrieve data from a custom dataset.
According to pytorch Dataset conception any map style dataset
should implement at least __len__ and __getitem__ methods.
"""
def __init__(
self,
) -> None:
"""
YOUR CODE HERE
"""
pass
def __len__(self) -> int:
"""
returns number of rows in data
"""
"""
YOUR CODE HERE
"""
pass
def __getitem__(self, idx: int) -> tp.Dict[str, tp.Any]:
"""
retrieves data for single index.
may include data processing and transformations.
E.g. augmenting data or tokenizing it.
returns dict with keys "input_ids", "label" and probably some more metadata (you decide whethere you need something more here)
"""
"""
YOUR CODE HERE
"""
pass
class ModelTrainer:
"""
This class implements logic run an experiemnt with a provided transformers classification model.
It incudes following components:
- load data
- load and configure a model and its artifacts
- train model
- validate model
- save model
- compue metrics
- run_experiment (as the man entrypoint to execute all flow)
Attention: current module intentionally doesnt support model inference or model serving.
It is a good practice to separate train/inference classes otherwise it is hard to maintain it all.
"""
def __init__(self):
"""
YOUR CODE HERE
"""
pass
def configure_optimizer(self) -> None:
"""
adds a self.optimizer attribute with a chosen optimizer and its params.
"""
"""
YOUR CODE HERE
"""
pass
def configure_scheduler(self) -> None:
"""
adds a self.scheduler attribute with a chosen scheduler (e.g. ReduceLROnPlateau).
"""
"""
YOUR CODE HERE
"""
pass
def apply_data_parallel(self) -> None:
"""
checks number of available cuda devices,
if number of GPUs is > 1, moves self.model to a DataParallel state for faster training.
"""
"""
YOUR CODE HERE
"""
pass
def load_data(self, filename: str, split: str) -> pd.DataFrame:
"""
uses Datasets library to load a dataset, takes as input dataset name (e.g. "imdb")
and a split. Loads data into pandas.
"""
"""
YOUR CODE HERE
"""
pass
def train(self, dataset: CustomDataset) -> None:
"""
YOUR CODE HERE
"""
pass
def validate(self, dataset: CustomDataset) -> tp.Dict[str, tp.Iterable]:
"""
takes a trained model and runs it on validation data.
Returns a dict with the keys "valid_labels" and "valid_preds" and corresponding values.
"""
"""
YOUR CODE HERE
"""
pass
def compute_metrics_report(
self, labels: tp.Iterable, predictions: tp.Iterable
) -> tp.Any:
"""
Computes classification metric (or several metrcis) for given task.
"""
"""
YOUR CODE HERE
"""
pass
def save_model(self, dst_path: str) -> None:
"""
Saves model to dst_path. Be careful to check if a model is on DataParallel state.
If it is, one needs to process it accordingly.
"""
"""
YOUR CODE HERE
"""
pass
def run_experiment(self):
"""
Main entrypoint.
Runs the flow from loading data to computing metrics.
"""
"""
YOUR CODE HERE
"""
pass
if __name__ == "__main__":
"""run experiment"""
model_trainer = ModelTrainer(...)
model_trainer.run_experiment()