-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
141 lines (125 loc) · 4.45 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
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
from const import Config
import os
import sys
import argparse
import numpy as np
import torch
import torch.nn as nn
from torch import optim
import json
import model
from torch.autograd import Variable
import utils
torch.manual_seed(1)
np.random.seed(1)
config=Config()
parse=argparse.ArgumentParser()
parse.add_argument('--mode',default='train',help='train/test')
parse.add_argument('--cuda',default=False)
parse.add_argument('--device',default="3")
args=parse.parse_args()
mode=args.mode
use_cuda=args.cuda
device_id=args.device
if use_cuda:
torch.cuda.manual_seed(1)
os.environ["CUDA_VISIBLE_DEVICES"] = device_id
model=model.EntModel(config,use_cuda)
if use_cuda:
model.cuda()
if mode=="test":
print("loading model")
state_dict=torch.load(open(config.model),'rb')
model.load_state_dict(state_dict)
def load_data(data_file):
all_data=[]
with open(data_file,'r',encoding='utf-8') as f:
for line in f:
data=[]
sentence=json.loads(line)
data.append(sentence["sentence"])
data.append(sentence["tags"])
all_data.append(data)
f.close()
return all_data
def get_batch(batch):
batch=sorted(batch,key=lambda x:len(x[0]),reverse=True)
X_len=[len(s[0]) for s in batch]
max_batch_sent_len=max(X_len)
X=[]
Y=[]
for s in batch:
X.append(s[0]+[config.vocab["<unk>"]]*(max_batch_sent_len-len(s[0])))
Y.append(s[1]+[0]*(max_batch_sent_len-len(s[1])))
sentence_tensor=utils.convert_long_variable(X,use_cuda)
tags_tensor=utils.convert_long_tensor(Y,use_cuda)
length_tensor=utils.convert_long_tensor(X_len,use_cuda)
return sentence_tensor,tags_tensor,length_tensor
def train_step(train_data,optimizer,dev_data):
model.train()
count=0
total_loss=0
for j in range(0, len(train_data), config.batch_size):
optimizer.zero_grad()
print("run bactch : % d" % j)
batch = train_data[j:j + config.batch_size]
sentence_tensor, tags_tensor,length_tensor=get_batch(batch)
loss=model.get_loss(sentence_tensor,tags_tensor,length_tensor)
loss.backward()
optimizer.step()
print("minibatch : %d , loss : %.5f " % (j,loss.item()))
total_loss+=loss.item()
count+=1
print("-------------------------------------------------------------")
print("avg loss : %.5f"%(total_loss/count))
print("-------------------------------------------------------------")
f1=dev_step(dev_data)
return f1
def dev_step(dev_data):
print("test the model")
model.eval()
predict_entity=[]
gold_entity=[]
origin=0.
found=0.
correct=0.
for j in range(0,len(dev_data),config.batch_size):
batch = dev_data[j:j + config.batch_size]
sentence_tensor, tags_tensor, length_tensor = get_batch(batch)
_,paths=model(sentence_tensor,length_tensor)
for sentence,tags ,path,length in zip(sentence_tensor,tags_tensor,paths,length_tensor):
sentence=sentence[:length]
tags=tags[:length]
sentence=list(sentence.cpu().numpy())
tags=list(tags.cpu().numpy())
predict_entity=utils.calculate(sentence,path,config.id2words,config.id2tags)
gold_entity=utils.calculate(sentence,tags,config.id2words,config.id2tags)
origin+=len(gold_entity)
found+=len(predict_entity)
for p_tag in predict_entity:
if p_tag in gold_entity:
correct+=1
p=0. if found==0 else (correct/found)
r=0. if origin==0 else (correct/origin)
f1=0. if p+r==0 else (2*p*r)/(p+r)
print("precision : %.3f , recall : %.3f , f1 : %.3f " % (p,r,f1))
return f1
def train(train_data,dev_data):
optimizer=optim.Adam(model.parameters())
print("start train")
best_f1=0
for i in range(config.epoch_size):
print("train epoch : %d" % i)
f1=train_step(train_data,optimizer,dev_data)
if f1>best_f1:
best_f1=f1
print("-----------------------------------------")
print("best f1 : %.3f " % best_f1)
print("-----------------------------------------")
print("save model....")
best_model_file=os.path.join(config.model_file,"epoch-%d_f1-%.3f.pt"%(i,best_f1))
torch.save(model.state_dict(),best_model_file)
if __name__=='__main__':
train_data=load_data(config.train_data_file)
dev_data=load_data(config.test_data_ffile)
train(train_data,dev_data)