-
Notifications
You must be signed in to change notification settings - Fork 39
/
train.py
60 lines (49 loc) · 1.57 KB
/
train.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
import logging
import os
from model import HyperParameters, BertModelTrainer
logger = logging.getLogger("train model")
logger.setLevel(logging.INFO)
logger.propagate = False
logging.getLogger("transformers").setLevel(logging.ERROR)
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S"
)
MODEL_DIR = "model"
if not os.path.exists(MODEL_DIR):
os.mkdir(MODEL_DIR)
fh = logging.FileHandler(os.path.join(MODEL_DIR, "train.log"), encoding="utf-8")
fh.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
if __name__ == "__main__":
BERT_PRETRAINED_MODEL = "/bert/pytorch_chinese_L-12_H-768_A-12"
TRAINING_DATASET = "data/train/input.txt" # for quick dev
# TRAINING_DATASET = "data/raw/CAIL2019-SCM-big/SCM_5k.json"
test_input_path = "data/test/input.txt"
test_ground_truth_path = "data/test/ground_truth.txt"
config = {
"max_length": 512,
"epochs": 2,
"batch_size": 12,
"learning_rate": 2e-5,
"fp16": False,
"fp16_opt_level": "O1",
"max_grad_norm": 1.0,
"warmup_steps": 0.1,
}
hyper_parameter = HyperParameters()
hyper_parameter.__dict__ = config
algorithm = "BertForSimMatchModel"
trainer = BertModelTrainer(
TRAINING_DATASET,
BERT_PRETRAINED_MODEL,
hyper_parameter,
algorithm,
test_input_path,
test_ground_truth_path,
)
trainer.train(MODEL_DIR, 1)