forked from nayeon7lee/bert-summarization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·59 lines (51 loc) · 1.9 KB
/
main.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
from model.transformer import Summarizer
from model.common_layer import evaluate
from utils import config
import torch
import torch.nn as nn
import torch.nn.functional as F
from tqdm import tqdm
import os
import time
import numpy as np
from utils.data import get_dataloaders, InputExample, InputFeatures
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def train_draft():
train_dl, val_dl, test_dl, tokenizer = get_dataloaders(is_small=config.small)
if(config.test):
print("Test model",config.model)
model = Transformer(model_file_path=config.save_path,is_eval=True)
evaluate(model,data_loader_test,model_name=config.model,ty='test')
exit(0)
model = Summarizer(is_draft=True, toeknizer=tokenizer)
print("TRAINABLE PARAMETERS",count_parameters(model))
print("Use Cuda: ", config.USE_CUDA)
best_rouge = 0
cnt = 0
eval_iterval = 500
for e in range(config.epochs):
# model.train()
print("Epoch", e)
l = []
pbar = tqdm(enumerate(train_dl),total=len(train_dl))
for i, d in pbar:
loss = model.train_one_batch(d)
l.append(loss.item())
pbar.set_description("TRAIN loss:{:.4f}".format(np.mean(l)))
if i%eval_iterval==0:
# model.eval()
loss,r_avg = evaluate(model,val_dl,model_name=config.model,ty="train")
# each epoch is long,so just do early stopping here.
if(r_avg > best_rouge):
best_rouge = r_avg
cnt = 0
model.save_model(loss,e,r_avg)
else:
cnt += 1
if(cnt > 20): break
# model.train()
# model.eval()
loss,r_avg = evaluate(model,val_dl,model_name=config.model,ty="valid")
if __name__ == "__main__":
train_draft()