-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
executable file
·176 lines (163 loc) · 4.99 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
175
176
"""
Runs a model on a single node across N-gpus.
"""
import os
from bert_classifier import BERTClassifier
from pytorch_lightning import Trainer
from pytorch_lightning.callbacks import EarlyStopping, ModelCheckpoint
from test_tube import HyperOptArgumentParser
from utils import setup_testube_logger
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
# ------------------------
model = BERTClassifier(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 TRAINER
# ------------------------
trainer = Trainer(
logger=setup_testube_logger(),
checkpoint_callback=True,
early_stop_callback=early_stop_callback,
default_save_path="experiments/",
gpus=hparams.gpus,
distributed_backend=hparams.distributed_backend,
use_amp=hparams.use_16bit,
max_epochs=hparams.max_epochs,
min_epochs=hparams.min_epochs,
accumulate_grad_batches=hparams.accumulate_grad_batches,
log_gpu_memory=hparams.log_gpu_memory,
val_percent_check=hparams.val_percent_check,
)
# --------------------------------
# 4 INIT MODEL CHECKPOINT CALLBACK
# -------------------------------
ckpt_path = os.path.join(
trainer.default_save_path,
trainer.logger.name,
f"version_{trainer.logger.version}",
"checkpoints",
)
# initialize Model Checkpoint Saver
checkpoint_callback = ModelCheckpoint(
filepath=ckpt_path,
save_top_k=hparams.save_top_k,
verbose=True,
monitor=hparams.monitor,
period=1,
mode=hparams.metric_mode,
)
trainer.checkpoint_callback = checkpoint_callback
# ------------------------
# 5 START TRAINING
# ------------------------
trainer.fit(model)
if __name__ == "__main__":
# ------------------------
# TRAINING ARGUMENTS
# ------------------------
# these are project-wide arguments
parser = HyperOptArgumentParser(
strategy="random_search",
description="Minimalist BERT 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=3,
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=10,
type=int,
help="Limits training to a max number number of epochs",
)
# Batching
parser.add_argument(
"--batch_size", default=8, 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(
"--distributed_backend",
type=str,
default="dp",
help="Supports three options dp, ddp, ddp2",
)
parser.add_argument(
"--use_16bit",
dest="use_16bit",
action="store_true",
help="If true uses 16 bit precision",
)
parser.add_argument(
"--log_gpu_memory",
type=str,
default=None,
help="Uses the output of nvidia-smi to log GPU usage. \
Might slow performance.",
)
parser.add_argument(
"--val_percent_check",
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 = BERTClassifier.add_model_specific_args(parser)
hparams = parser.parse_args()
# ---------------------
# RUN TRAINING
# ---------------------
main(hparams)