-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
152 lines (116 loc) · 4.38 KB
/
main.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
import os
import hydra
from omegaconf import OmegaConf
import pytorch_lightning as pl
from pytorch_lightning import loggers
from pytorch_lightning.callbacks import ModelCheckpoint, EarlyStopping
from transformers import AutoTokenizer
from source.callback.PredictionWriter import PredictionWriter
from source.callback.WordRPRWriter import WordRPRWriter
from source.datamodule.ClueWordsDataModule import ClueWordsDataModule
from source.model.TeCModel import TecModel
def get_logger(params, fold):
return loggers.TensorBoardLogger(
save_dir=params.log.dir,
name=f"{params.model.name}_{params.data.name}_{fold}_exp"
)
def get_model_checkpoint_callback(params, fold):
return ModelCheckpoint(
monitor="val_Mic-F1",
dirpath=params.model_checkpoint.dir,
filename=f"{params.model.name}_{params.data.name}_{fold}",
save_top_k=1,
save_weights_only=True,
mode="max"
)
def get_early_stopping_callback(params):
return EarlyStopping(
monitor='val_Mic-F1',
patience=params.trainer.patience,
min_delta=params.trainer.min_delta,
mode='max'
)
def get_tokenizer(hparams):
tokenizer = AutoTokenizer.from_pretrained(
hparams.tokenizer.architecture
)
if hparams.tokenizer.architecture == "gpt2":
tokenizer.add_special_tokens({'pad_token': '[PAD]'})
return tokenizer
def train(params):
for fold in params.data.folds:
print(f"Fitting {params.model.name} over {params.data.name} (fold {fold}) with fowling params\n"
f"{OmegaConf.to_yaml(params)}\n")
# Initialize a trainer
trainer = pl.Trainer(
fast_dev_run=params.trainer.fast_dev_run,
max_epochs=params.trainer.max_epochs,
precision=params.trainer.precision,
gpus=params.trainer.gpus,
progress_bar_refresh_rate=params.trainer.progress_bar_refresh_rate,
logger=get_logger(params, fold),
callbacks=[
get_model_checkpoint_callback(params, fold), # checkpoint_callback
get_early_stopping_callback(params), # early_stopping_callback
]
)
# Train the ⚡ model
trainer.fit(
model=TecModel(params.model),
datamodule=ClueWordsDataModule(params.data, get_tokenizer(params.model), fold=fold)
)
def test(params):
for fold in params.data.folds:
print(f"Predicting {params.model.name} over {params.data.name} (fold {fold}) with fowling params\n"
f"{OmegaConf.to_yaml(params)}\n")
# data
dm = ClueWordsDataModule(params.data, get_tokenizer(params.model), fold=fold)
# model
model = TecModel.load_from_checkpoint(
checkpoint_path=f"{params.model_checkpoint.dir}{params.model.name}_{params.data.name}_{fold}.ckpt"
)
model.hparams.stat.name = f"{params.model.name}_{params.data.name}_{fold}.stat"
# trainer
trainer = pl.Trainer(
gpus=params.trainer.gpus
)
# testing
trainer.test(
model=model,
datamodule=dm
)
def predict(params):
for fold in params.data.folds:
print(f"Predicting {params.model.name} over {params.data.name} (fold {fold}) with fowling params\n"
f"{OmegaConf.to_yaml(params)}\n")
# data
dm = ClueWordsDataModule(params.data, get_tokenizer(params.model), fold=fold)
# model
model = TecModel.load_from_checkpoint(
checkpoint_path=f"{params.model_checkpoint.dir}{params.model.name}_{params.data.name}_{fold}.ckpt"
)
params.representation.name = f"{params.model.name}_{params.data.name}_{fold}.rpr"
# trainer
trainer = pl.Trainer(
gpus=params.trainer.gpus,
callbacks=[WordRPRWriter(params.representation)]
)
# predicting
dm.prepare_data()
dm.setup("predict")
trainer.predict(
model=model,
datamodule=dm,
)
@hydra.main(config_path="settings/", config_name="settings.yaml")
def perform_tasks(params):
os.chdir(hydra.utils.get_original_cwd())
OmegaConf.resolve(params)
if "fit" in params.tasks:
train(params)
if "test" in params.tasks:
test(params)
if "predict" in params.tasks:
predict(params)
if __name__ == '__main__':
perform_tasks()