-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain1.py
303 lines (270 loc) · 11.8 KB
/
train1.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#from config.config1 import *
from utils.utils1 import *
from data_processor.data_processor1 import *
from model.model1 import *
LOGGER = get_logger()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# from main1 import CFG
# import config.config1 as Config
# CFG = Config.CFG()
# import wandb
import gc
import time
import math
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import pandas as pd
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
from tqdm.auto import tqdm
import torch
import torch.nn as nn
from torch.optim import Adam, SGD, AdamW
from torch.utils.data import DataLoader, Dataset
# os.system('pip uninstall -y transformers')
# os.system('python -m pip install --no-index --find-links=../input/nbme-pip-wheels transformers')
from transformers import get_linear_schedule_with_warmup, get_cosine_schedule_with_warmup
#%env TOKENIZERS_PARALLELISM= true
# ====================================================
# Helper functions
# ====================================================
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def asMinutes(s):
m = math.floor(s / 60)
s -= m * 60
return '%dm %ds' % (m, s)
def timeSince(since, percent):
now = time.time()
s = now - since
es = s / (percent)
rs = es - s
return '%s (remain %s)' % (asMinutes(s), asMinutes(rs))
#开始训练,返回losses.avg
def train_fn(fold, train_loader, model, criterion, optimizer, epoch, scheduler, device):
model.train()
scaler = torch.cuda.amp.GradScaler(enabled=CFG.apex)
losses = AverageMeter()
start = end = time.time()
global_step = 0
#训练主函数:inputs包括inputs(vocab id),tokentype_ids,attention_mask;分批一批4个输入;step表示批次
for step, (inputs, labels) in enumerate(train_loader):
for k, v in inputs.items():
inputs[k] = v.to(device)
labels = labels.to(device)
batch_size = labels.size(0)
with torch.cuda.amp.autocast(enabled=CFG.apex):
#用"microsoft/deberta-v3-large"直接输出 得到[4,354,1]维张量
y_preds = model(inputs)
#criterion:nn.BCEWithLogitsLoss(reduction="none")
#[1416,1]
loss = criterion(y_preds.view(-1, 1), labels.view(-1, 1))
#平均
loss = torch.masked_select(loss, labels.view(-1, 1) != -1).mean()
if CFG.gradient_accumulation_steps > 1:
loss = loss / CFG.gradient_accumulation_steps
losses.update(loss.item(), batch_size)
#loss反向传播
scaler.scale(loss).backward()
grad_norm = torch.nn.utils.clip_grad_norm_(model.parameters(), CFG.max_grad_norm)
if (step + 1) % CFG.gradient_accumulation_steps == 0:
scaler.step(optimizer)
scaler.update()
optimizer.zero_grad()
global_step += 1
if CFG.batch_scheduler:
scheduler.step()
end = time.time()
if step % CFG.print_freq == 0 or step == (len(train_loader)-1):
print('Epoch: [{0}][{1}/{2}] '
'Elapsed {remain:s} '
'Loss: {loss.val:.4f}({loss.avg:.4f}) '
'Grad(标准化梯度): {grad_norm:.4f} '
'LR(学习率): {lr:.8f} '
.format(epoch+1, step, len(train_loader),
remain=timeSince(start, float(step+1)/len(train_loader)),
loss=losses,
grad_norm=grad_norm,
lr=scheduler.get_lr()[0]))
# if CFG.wandb:
# wandb.log({f"[fold{fold}] loss": losses.val,
# f"[fold{fold}] lr": scheduler.get_lr()[0]})
return losses.avg
def valid_fn(valid_loader, model, criterion, device):
losses = AverageMeter()
model.eval()
preds = []
start = end = time.time()
for step, (inputs, labels) in enumerate(valid_loader):
for k, v in inputs.items():
inputs[k] = v.to(device)
labels = labels.to(device)
batch_size = labels.size(0)
with torch.no_grad():
y_preds = model(inputs)
loss = criterion(y_preds.view(-1, 1), labels.view(-1, 1))
loss = torch.masked_select(loss, labels.view(-1, 1) != -1).mean()
if CFG.gradient_accumulation_steps > 1:
loss = loss / CFG.gradient_accumulation_steps
losses.update(loss.item(), batch_size)
preds.append(y_preds.sigmoid().to('cpu').numpy())
end = time.time()
if step % CFG.print_freq == 0 or step == (len(valid_loader)-1):
print('EVAL: [{0}/{1}] '
'Elapsed {remain:s} '
'Loss: {loss.val:.4f}({loss.avg:.4f}) '
.format(step, len(valid_loader),
loss=losses,
remain=timeSince(start, float(step+1)/len(valid_loader))))
predictions = np.concatenate(preds)
return losses.avg, predictions
def inference_fn(test_loader, model, device):
preds = []
model.eval()
model.to(device)
tk0 = tqdm(test_loader, total=len(test_loader))
for inputs in tk0:
for k, v in inputs.items():
inputs[k] = v.to(device)
with torch.no_grad():
y_preds = model(inputs)
preds.append(y_preds.sigmoid().to('cpu').numpy())
predictions = np.concatenate(preds)
return predictions
# ====================================================
# train loop
# ====================================================
def train_loop(folds, fold):
LOGGER.info(f"========== fold: {fold} training ==========")
# ====================================================
# loader
# ====================================================
#每一折的训练集
train_folds = folds[folds['fold'] != fold].reset_index(drop=True)
#每一折的测试集(20%)
valid_folds = folds[folds['fold'] == fold].reset_index(drop=True)
#测试集病历
valid_texts = valid_folds['pn_history'].values
#测试集的location
valid_labels = create_labels_for_scoring(valid_folds)
#初始化数据
"""
成员变量:
self.cfg = cfg
self.feature_texts = df['feature_text'].values
self.pn_historys = df['pn_history'].values
self.annotation_lengths = df['annotation_length'].values
self.locations = df['location'].values
"""
train_dataset = TrainDataset(CFG, train_folds)
valid_dataset = TrainDataset(CFG, valid_folds)
train_loader = DataLoader(train_dataset,
batch_size=CFG.batch_size,
shuffle=True,
num_workers=CFG.num_workers, pin_memory=True, drop_last=True)
valid_loader = DataLoader(valid_dataset,
batch_size=CFG.batch_size,
shuffle=False,
num_workers=CFG.num_workers, pin_memory=True, drop_last=False)
# ====================================================
# model & optimizer
# ====================================================
model = CustomModel(CFG, config_path=None, pretrained=True)
torch.save(model.config, OUTPUT_DIR + 'config.pth')
model.to(device)
#优化器
def get_optimizer_params(model, encoder_lr, decoder_lr, weight_decay=0.0):
param_optimizer = list(model.named_parameters())
#param_optimizer = ','.join(param_optimizer)
print(f'模型优化器有如下几种:{param_optimizer}')
#衰减?
no_decay = ["bias", "LayerNorm.bias", "LayerNorm.weight"]
optimizer_parameters = [
{'params': [p for n, p in model.model.named_parameters() if not any(nd in n for nd in no_decay)],
'lr': encoder_lr, 'weight_decay': weight_decay},
{'params': [p for n, p in model.model.named_parameters() if any(nd in n for nd in no_decay)],
'lr': encoder_lr, 'weight_decay': 0.0},
{'params': [p for n, p in model.named_parameters() if "model" not in n],
'lr': decoder_lr, 'weight_decay': 0.0}
]
return optimizer_parameters
#weight_decay是什么?
optimizer_parameters = get_optimizer_params(model,
encoder_lr=CFG.encoder_lr,
decoder_lr=CFG.decoder_lr,
weight_decay=CFG.weight_decay)
optimizer = AdamW(optimizer_parameters, lr=CFG.encoder_lr, eps=CFG.eps, betas=CFG.betas)
# ====================================================
# scheduler:调度器
# ====================================================
def get_scheduler(cfg, optimizer, num_train_steps):
if cfg.scheduler == 'linear':
scheduler = get_linear_schedule_with_warmup(
optimizer, num_warmup_steps=cfg.num_warmup_steps, num_training_steps=num_train_steps
)
elif cfg.scheduler == 'cosine':
scheduler = get_cosine_schedule_with_warmup(
optimizer, num_warmup_steps=cfg.num_warmup_steps, num_training_steps=num_train_steps,
num_cycles=cfg.num_cycles
)
return scheduler
#训练完所有epoch需要多少批
num_train_steps = int(len(train_folds) / CFG.batch_size * CFG.epochs)
scheduler = get_scheduler(CFG, optimizer, num_train_steps)
# ====================================================
# loop
# ====================================================
#
criterion = nn.BCEWithLogitsLoss(reduction="none")
best_score = 0.
for epoch in range(CFG.epochs):
#一个实数
start_time = time.time()
# train
avg_loss = train_fn(fold, train_loader, model, criterion, optimizer, epoch, scheduler, device)
# eval
avg_val_loss, predictions = valid_fn(valid_loader, model, criterion, device)
predictions = predictions.reshape((len(valid_folds), CFG.max_len))
# scoring
#得到验证集预测结果,形式是概率
char_probs = get_char_probs(valid_texts, predictions, CFG.tokenizer)
#将预测转化为[0 3; 5 9]的形式
results = get_results(char_probs, th=0.5)
#将结果用列表储存,方便计算score
preds = get_predictions(results)
score = get_score(valid_labels, preds)
elapsed = time.time() - start_time
LOGGER.info(
f'Epoch {epoch + 1} - avg_train_loss: {avg_loss:.4f} avg_val_loss: {avg_val_loss:.4f} time: {elapsed:.0f}s')
LOGGER.info(f'Epoch {epoch + 1} - Score: {score:.4f}')
# if CFG.wandb:
# wandb.log({f"[fold{fold}] epoch": epoch + 1,
# f"[fold{fold}] avg_train_loss": avg_loss,
# f"[fold{fold}] avg_val_loss": avg_val_loss,
# f"[fold{fold}] score": score})
if best_score < score:
best_score = score
LOGGER.info(f'Epoch {epoch + 1} - Save Best Score: {best_score:.4f} Model')
torch.save({'model': model.state_dict(),
'predictions': predictions},
OUTPUT_DIR + f"{CFG.model.replace('/', '-')}_fold{fold}_best.pth")
predictions = torch.load(OUTPUT_DIR + f"{CFG.model.replace('/', '-')}_fold{fold}_best.pth",
map_location=torch.device('cpu'))['predictions']
valid_folds[[i for i in range(CFG.max_len)]] = predictions
torch.cuda.empty_cache()
gc.collect()
return valid_folds