-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
69 lines (52 loc) · 1.92 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
61
62
63
64
65
66
67
68
69
from datetime import datetime
import torch
from pytz import timezone
from sklearn.model_selection import KFold, StratifiedKFold
from transformers import AutoTokenizer
import trainer
import wandb
from args import parse_args
from dataloader import Preprocess
from utils import set_seeds
def main(args):
if not args.run_name:
args.run_name = datetime.now(timezone("Asia/Seoul")).strftime("%Y-%m-%d-%H:%M:%S")
wandb.init(project='KLUE_TC', name=args.run_name, config=vars(args))
set_seeds(args.seed)
device = "cuda" if torch.cuda.is_available() else "cpu"
args.device = device
tokenizer = AutoTokenizer.from_pretrained(
args.tokenizer_name
if args.tokenizer_name
else args.model_name_or_path,
use_fast=True,
)
preprocess = Preprocess(args)
preprocess.load_train_data()
train_data_origin = preprocess.train_data
print(f"Size of train data : {len(train_data_origin)}")
if args.cv_strategy == 'random':
kf = KFold(n_splits=args.fold_num, shuffle=True)
splits = kf.split(X=train_data_origin)
else:
# default
# 여기 각 label로 바꿔야됨
train_labels = [sequence[-1] for sequence in train_data_origin]
skf = StratifiedKFold(n_splits=args.fold_num, shuffle=True)
splits = skf.split(X=train_data_origin, y=train_labels)
acc_avg = 0
for fold_num, (train_index, valid_index) in enumerate(splits):
train_data = train_data_origin[train_index]
valid_data = train_data_origin[valid_index]
best_acc = trainer.run(args, tokenizer, train_data, valid_data, fold_num + 1)
if not args.cv_strategy:
break
acc_avg += best_acc
if args.cv_strategy:
acc_avg /= args.fold_num
wandb.log({"acc_avg": acc_avg})
print("*" * 50, 'acc_avg', "*" * 50)
print(acc_avg)
if __name__ == '__main__':
args = parse_args('train')
main(args)