-
Notifications
You must be signed in to change notification settings - Fork 39
/
training.py
174 lines (160 loc) · 4.75 KB
/
training.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
167
168
169
170
171
172
173
174
"""
Runs a model on a single node across N-gpus.
"""
import argparse
import os
from datetime import datetime
from classifier import Classifier
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from pytorch_lightning.loggers import LightningLoggerBase, TensorBoardLogger
from torchnlp.random import set_seed
def main(hparams) -> None:
"""
Main training routine specific for this project
:param hparams:
"""
set_seed(hparams.seed)
# ------------------------
# 1 INIT LIGHTNING MODEL AND DATA
# ------------------------
model = Classifier(hparams)
# ------------------------
# 2 INIT EARLY STOPPING
# ------------------------
early_stop_callback = EarlyStopping(
monitor=hparams.monitor,
min_delta=0.0,
patience=hparams.patience,
verbose=True,
mode=hparams.metric_mode,
)
# ------------------------
# 3 INIT LOGGERS
# ------------------------
# Tensorboard Callback
tb_logger = TensorBoardLogger(
save_dir="experiments/",
version="version_" + datetime.now().strftime("%d-%m-%Y--%H-%M-%S"),
name="",
)
# Model Checkpoint Callback
ckpt_path = os.path.join(
"experiments/",
tb_logger.version,
"checkpoints",
)
# --------------------------------
# 4 INIT MODEL CHECKPOINT CALLBACK
# -------------------------------
checkpoint_callback = ModelCheckpoint(
filepath=ckpt_path,
save_top_k=hparams.save_top_k,
verbose=True,
monitor=hparams.monitor,
period=1,
mode=hparams.metric_mode,
save_weights_only=True,
)
# ------------------------
# 5 INIT TRAINER
# ------------------------
trainer = Trainer(
logger=tb_logger,
checkpoint_callback=True,
early_stop_callback=early_stop_callback,
gradient_clip_val=1.0,
gpus=hparams.gpus,
log_gpu_memory="all",
deterministic=True,
check_val_every_n_epoch=1,
fast_dev_run=False,
accumulate_grad_batches=hparams.accumulate_grad_batches,
max_epochs=hparams.max_epochs,
min_epochs=hparams.min_epochs,
val_check_interval=hparams.val_check_interval,
distributed_backend="dp",
)
# ------------------------
# 6 START TRAINING
# ------------------------
trainer.fit(model, model.data)
if __name__ == "__main__":
# ------------------------
# TRAINING ARGUMENTS
# ------------------------
# these are project-wide arguments
parser = argparse.ArgumentParser(
description="Minimalist Transformer Classifier",
add_help=True,
)
parser.add_argument("--seed", type=int, default=3, help="Training seed.")
parser.add_argument(
"--save_top_k",
default=1,
type=int,
help="The best k models according to the quantity monitored will be saved.",
)
# Early Stopping
parser.add_argument(
"--monitor", default="val_acc", type=str, help="Quantity to monitor."
)
parser.add_argument(
"--metric_mode",
default="max",
type=str,
help="If we want to min/max the monitored quantity.",
choices=["auto", "min", "max"],
)
parser.add_argument(
"--patience",
default=2,
type=int,
help=(
"Number of epochs with no improvement "
"after which training will be stopped."
),
)
parser.add_argument(
"--min_epochs",
default=1,
type=int,
help="Limits training to a minimum number of epochs",
)
parser.add_argument(
"--max_epochs",
default=5,
type=int,
help="Limits training to a max number number of epochs",
)
# Batching
parser.add_argument(
"--batch_size", default=6, type=int, help="Batch size to be used."
)
parser.add_argument(
"--accumulate_grad_batches",
default=2,
type=int,
help=(
"Accumulated gradients runs K small batches of size N before "
"doing a backwards pass."
),
)
# gpu args
parser.add_argument("--gpus", type=int, default=1, help="How many gpus")
parser.add_argument(
"--val_check_interval",
default=1.0,
type=float,
help=(
"If you don't want to use the entire dev set (for debugging or "
"if it's huge), set how much of the dev set you want to use with this flag."
),
)
# each LightningModule defines arguments relevant to it
parser = Classifier.add_model_specific_args(parser)
hparams = parser.parse_args()
# ---------------------
# RUN TRAINING
# ---------------------
main(hparams)