-
Notifications
You must be signed in to change notification settings - Fork 0
/
finetune.py
74 lines (63 loc) · 2.1 KB
/
finetune.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
from model import RIIDDTransformerModel
from dataset import get_dataloaders
import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from pytorch_lightning.callbacks import (
EarlyStopping,
LearningRateMonitor,
ModelCheckpoint,
)
from pytorch_lightning.loggers import TensorBoardLogger
from utils import get_wd
import hydra
SEED = 69
seed_everything(SEED)
@hydra.main(config_name="config_finetune")
def finetune(cfg) -> None:
val_step_frequency = cfg["val_step_frequency"]
learning_rate = cfg["learning_rate"]
model_path = cfg["model_path"]
batch_size = cfg["batch_size"]
validation_batch_size = cfg["validation_batch_size"]
max_window_size = cfg["max_window_size"]
num_workers = cfg["num_workers"]
use_lectures = cfg["use_lectures"]
model = RIIDDTransformerModel.load_from_checkpoint(f"{get_wd()}{model_path}")
model.learning_rate = learning_rate
model.lr_step_frequency = val_step_frequency
train_loader, val_loader = get_dataloaders(
batch_size=batch_size,
validation_batch_size=validation_batch_size,
max_window_size=max_window_size,
use_lectures=use_lectures,
num_workers=num_workers,
)
logger = TensorBoardLogger(
f"{get_wd()}lightning_logs",
name="fine_tune_lg",
)
# Initialize a trainer
trainer = pl.Trainer(
gpus=1,
max_epochs=2,
progress_bar_refresh_rate=1,
callbacks=[
EarlyStopping(monitor="avg_val_auc", patience=5, mode="max"),
ModelCheckpoint(
monitor="avg_val_auc",
filename="{epoch}-{val_loss_step:.2f}-{avg_val_auc:.2f}",
mode="max",
),
LearningRateMonitor(logging_interval="step"),
],
logger=logger,
val_check_interval=val_step_frequency, # check validation every 1000 step
limit_val_batches=0.05, # run through only 5% of val every time
)
trainer.fit(
model,
train_dataloader=train_loader,
val_dataloaders=[val_loader],
)
if __name__ == "__main__":
finetune()