-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
215 lines (169 loc) · 7.22 KB
/
utils.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import random
import torch
import numpy as np
from torch import nn
from torch.optim import Adam, AdamW, SGD
from adamp import AdamP
from torch.optim.lr_scheduler import StepLR, ReduceLROnPlateau, CosineAnnealingLR, ExponentialLR, \
CosineAnnealingWarmRestarts
from transformers import get_linear_schedule_with_warmup
from transformers import AutoConfig, AutoTokenizer, AutoModelForSequenceClassification
from dataloader import YNAT_dataset
from classifier import TextClassifier
def set_seeds(seed=42):
# 랜덤 시드를 설정하여 매 코드를 실행할 때마다 동일한 결과를 얻게 합니다.
os.environ['PYTHONHASHSEED'] = str(seed)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.cuda.manual_seed_all(seed) # if use multi-GPU
torch.backends.cudnn.benchmark = False
def save_checkpoint(state, model_dir, model_filename):
print('saving model ...')
if not os.path.exists(model_dir):
os.makedirs(model_dir)
torch.save(state, os.path.join(model_dir, model_filename))
def get_optimizer(model, args):
if args.optimizer == 'adam':
optimizer = Adam(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
elif args.optimizer == 'adamW':
optimizer = AdamW(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
elif args.optimizer == 'adamP':
optimizer = AdamP(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
elif args.optimizer == 'SGD':
optimizer = SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
# 모든 parameter들의 grad값을 0으로 초기화
optimizer.zero_grad()
return optimizer
def get_scheduler(optimizer, args):
if args.scheduler == 'plateau':
scheduler = ReduceLROnPlateau(optimizer, patience=args.plateau_patience, factor=args.plateau_factor, mode='max',
verbose=True)
elif args.scheduler == 'linear_warmup':
scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps=args.warmup_steps,
num_training_steps=args.total_steps)
elif args.scheduler == 'step_lr':
scheduler = StepLR(optimizer, step_size=args.step_size, gamma=args.gamma)
elif args.scheduler == 'exp_lr':
scheduler = ExponentialLR(optimizer, gamma=args.gamma)
elif args.scheduler == 'cosine_annealing':
scheduler = CosineAnnealingLR(optimizer, T_max=args.t_max, eta_min=args.eta_min)
elif args.scheduler == 'cosine_annealing_warmstart':
scheduler = CosineAnnealingWarmRestarts(optimizer, T_0=args.T_0, T_mult=args.T_mult, eta_min=args.eta_min,
last_epoch=-1)
return scheduler
def update_params(loss, model, optimizer, batch_idx, max_len, args):
if args.gradient_accumulation:
# normalize loss to account for batch accumulation
loss = loss / args.accum_iter
# backward pass
loss.backward()
# weights update
if ((batch_idx + 1) % args.accum_iter == 0) or (batch_idx + 1 == max_len):
torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip_grad)
optimizer.step()
optimizer.zero_grad()
else:
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), args.clip_grad)
optimizer.step()
optimizer.zero_grad()
def load_tokenizer(args):
tokenizer = AutoTokenizer.from_pretrained(
args.tokenizer_name
if args.tokenizer_name
else args.model_name_or_path,
use_fast=True,
)
return tokenizer
def load_model(args, model_name=None):
if not model_name:
model_name = args.model_name
model_path = os.path.join(args.model_dir, model_name)
print("Loading Model from:", model_path)
# load_state = torch.load(model_path)
load_state = torch.load(model_name)
# Load pretrained model and tokenizer
config = AutoConfig.from_pretrained(
args.config_name
if args.config_name
else args.model_name_or_path,
)
config.num_labels = 7
model = AutoModelForSequenceClassification.from_pretrained(
args.model_name_or_path,
from_tf=bool(".ckpt" in args.model_name_or_path),
config=config,
)
if args.classifier == "CNN":
model.classifier = TextClassifier(args)
model.load_state_dict(load_state['state_dict'], strict=True)
model = model.to(args.device)
print("Loading Model from:", model_path, "...Finished.")
return model
def get_model(args):
# Load pretrained model and tokenizer
config = AutoConfig.from_pretrained(
args.config_name
if args.config_name
else args.model_name_or_path,
)
config.num_labels = 7
model = AutoModelForSequenceClassification.from_pretrained(
args.model_name_or_path,
from_tf=bool(".ckpt" in args.model_name_or_path),
config=config,
)
if args.classifier == "CNN":
model.classifier = TextClassifier(args)
model = model.to(args.device)
return model
def get_loaders(args, train, valid, is_inference=False):
pin_memory = True
train_loader, valid_loader = None, None
if is_inference:
test_dataset = YNAT_dataset(args, valid, is_inference)
test_loader = torch.utils.data.DataLoader(test_dataset, num_workers=args.num_workers, shuffle=False,
batch_size=args.batch_size, pin_memory=pin_memory)
return test_loader
if train is not None:
train_dataset = YNAT_dataset(args, train, is_inference)
train_loader = torch.utils.data.DataLoader(train_dataset, num_workers=args.num_workers, shuffle=True,
batch_size=args.batch_size, pin_memory=pin_memory)
if valid is not None:
valid_dataset = YNAT_dataset(args, valid, is_inference)
valid_loader = torch.utils.data.DataLoader(valid_dataset, num_workers=args.num_workers, shuffle=False,
batch_size=args.batch_size, pin_memory=pin_memory)
return train_loader, valid_loader
# loss계산하고 parameter update!
def compute_loss(preds, targets, args):
"""
Args :
preds : (batch_size, max_seq_len)
targets : (batch_size, max_seq_len)
"""
# print(preds, targets)
loss = get_criterion(preds, targets, args)
# 마지막 시퀀스에 대한 값만 loss 계산
# loss = loss[:, -1]
# loss = torch.mean(loss)
return loss
def get_criterion(pred, target, args):
if args.criterion == 'BCE':
loss = nn.BCELoss(reduction="none")
elif args.criterion == "BCELogit":
loss = nn.BCEWithLogitsLoss(reduction="none")
elif args.criterion == "MSE":
loss = nn.MSELoss(reduction="none")
elif args.criterion == "L1":
loss = nn.L1Loss(reduction="none")
elif args.criterion == "CE":
loss = nn.CrossEntropyLoss()
elif args.criterion == "WeightedCE":
weights = [1, 1, 2, 1, 1, 1, 1]
class_weights = torch.FloatTensor(weights).cuda()
loss = nn.CrossEntropyLoss(weight=class_weights)
return loss(pred, target)