-
Notifications
You must be signed in to change notification settings - Fork 3
/
train_querydoc_model.py
112 lines (100 loc) · 3.13 KB
/
train_querydoc_model.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
import argparse
import logging
import torch
from torch.utils.data import DataLoader
from transformers import ElectraTokenizerFast
from pytorch_finetuning.electra_for_logistic_regression import \
ElectraForLogisticRegression
from pytorch_finetuning import pytorch_finetuning
logging.basicConfig(
format="%(asctime)s %(levelname)s:%(message)s",
level=logging.DEBUG,
datefmt="%I:%M:%S",
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"train_tsv", type=str, help="Path to TSV file with training data"
)
parser.add_argument(
"dev_tsv", type=str, help="Path to TSV file with development data"
)
parser.add_argument(
"save_path", type=str, help="Path to folder to save checkpoints"
)
parser.add_argument(
"--doc_max_len",
default=128,
help="Max number of tokens to use (do not use more than 512 tokens",
)
parser.add_argument("--batch_size", default=32, help="Batch size")
parser.add_argument(
"--grad_acc_steps", default=8, help="Gradient accumulation steps"
)
parser.add_argument(
"--gpu_num", default="0", help="GPU ID, Use -1 to run on CPU"
)
parser.add_argument(
"--random_seed", default=0, help="Random seed"
)
parser.add_argument(
"--num_epochs", default=20, help="Number of training epochs"
)
args = parser.parse_args()
tokenizer = ElectraTokenizerFast.from_pretrained("Seznam/small-e-czech")
if torch.cuda.is_available() and args.gpu_num != "-1":
device = torch.device(f"cuda:{args.gpu_num}")
else:
device = torch.device("cpu")
train_dataset = pytorch_finetuning.RelevanceDataset(
args.train_tsv,
max_len=args.doc_max_len,
tokenizer=tokenizer,
nrows=None,
)
dev_dataset = pytorch_finetuning.RelevanceDataset(
args.dev_tsv,
max_len=args.doc_max_len,
tokenizer=tokenizer,
nrows=None,
)
train_loader = DataLoader(
train_dataset,
batch_size=args.batch_size,
num_workers=5,
pin_memory=False,
shuffle=True,
)
dev_loader = DataLoader(
dev_dataset,
batch_size=args.batch_size,
num_workers=5,
pin_memory=False
)
metrics = {
"p_at_10": lambda model, predictions:
pytorch_finetuning.get_p_at_10_precision(
None,
None,
predictions.squeeze(-1),
dev_dataset.get_column("label"),
dev_dataset.get_column("query"),
)
}
model_name = f"querydoc_electra-{args.random_seed}"
# MAIN
# Load pre-trained model, fine-tune on TRAIN_TSV data and save
# P@10 progression can be inspected using Tensorboard
pytorch_finetuning.train(
"Seznam/small-e-czech",
train_loader,
dev_loader,
num_epochs=args.num_epochs,
device=device,
model_class=ElectraForLogisticRegression,
saving_path=args.save_path,
finetuning_model_name=model_name,
metrics=metrics,
random_seed=args.random_seed,
grad_acc_steps=args.grad_acc_steps,
)