-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain.py
More file actions
249 lines (217 loc) · 7.99 KB
/
train.py
File metadata and controls
249 lines (217 loc) · 7.99 KB
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
import numpy as np
import time
import copy
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torch.autograd as autograd
import torch.nn.functional
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms
import warnings
import torch
USE_CUDA = torch.cuda.is_available()
import get_data
from model import BiLSTM_Match
from model import LSTM_Match
import matplotlib.pyplot as plt
import pandas as pd
np.random.seed(53113)
torch.manual_seed(53113)
if USE_CUDA:
torch.cuda.manual_seed(53113)
embedding_dim=400
hidden_dim=256
vocab_size=51158
target=1
Batchsize=128
stringlen=25
Epoch=20
lr=0.001
"""
embedding_dim=400
hidden_dim=256
vocab_size=51158
target=1
Batchsize=8
stringlen=25
Epoch=20
lr=0.1
"""
texta,textb,labels,evala,evalb,evallabels=get_data.train_data(stringlen)
resulta,resultb=get_data.result_data(stringlen)
if USE_CUDA:
texta = texta.cuda()
textb= textb.cuda()
labels= labels.cuda()
evala= evala.cuda()
evalb= evalb.cuda()
evallabels= evallabels.cuda()
resulta=resulta.cuda()
resultb=resultb.cuda()
def out_put(net,resulta,resultb,batchsize):
net.eval()
dataset = torch.utils.data.TensorDataset(resulta, resultb)
train_iter = torch.utils.data.DataLoader(dataset, batchsize, shuffle=False)
statea = None
stateb = None
filename = 'SheShuaiJie_NJU_predict.txt'
with open(filename, 'w') as file_object:
print("open success")
with torch.no_grad():
for XA, XB in train_iter:
XA = XA.long()
XB = XB.long()
if XA.size(0)!= batchsize:
break
if statea is not None:
if isinstance(statea, tuple): # LSTM, state:(h, c)
statea = (statea[0].detach(), statea[1].detach())
else:
statea = statea.detach()
if stateb is not None:
if isinstance(stateb, tuple): # LSTM, state:(h, c)
stateb = (stateb[0].detach(), stateb[1].detach())
else:
stateb = stateb.detach()
(output, statea, stateb) = net(XA, XB, statea, stateb, False)
result = output.detach().cpu().numpy().tolist()
result = [1 if i[0] > 0.5 else 0 for i in result]
for i in result:
file_object.write(str(i)+"\n")
return
def eval(net,eval_dataa,eval_datab, eval_label,resulta,resultb,batch_size,pre,model_path):
net.eval()
dataset = torch.utils.data.TensorDataset(eval_dataa,eval_datab, eval_label)
train_iter = torch.utils.data.DataLoader(dataset, batch_size, shuffle=False)
total=0
correct=0
statea = None
stateb = None
with torch.no_grad():
for XA, XB , y in train_iter:
XA = XA.long()
XB = XB.long()
if XA.size(0)!= batch_size:
break
if statea is not None:
if isinstance(statea, tuple): # LSTM, state:(h, c)
statea = (statea[0].detach(), statea[1].detach())
else:
statea = statea.detach()
if stateb is not None:
if isinstance(stateb, tuple): # LSTM, state:(h, c)
stateb = (stateb[0].detach(), stateb[1].detach())
else:
stateb = stateb.detach()
(output, statea, stateb) = net(XA, XB, statea, stateb, False)
total += XA.size(0)
result = output.detach().cpu().numpy().tolist()
result = [1 if i[0] > 0.5 else 0 for i in result]
answer = y.cpu().numpy().tolist()
for i in range(len(answer)):
# print(answer[i][0]," ",result[i])
if answer[i][0] == result[i]:
correct += 1
s = (((1.0 * correct) / total))
print(correct,"/" , total, "TestAcc: ", s)
if s > pre:
print("Flush and save model")
torch.save(net.state_dict(), model_path)
out_put(net,resulta,resultb,batch_size)
return s
def train(net, texta,textb,labels,evala,evalb,evallabels,resulta,resultb,num_epochs, learning_rate, batch_size,model_path):
net.train()
statea = None
stateb = None
#loss_fct = nn.CrossEntropyLoss()
loss_fct = torch.nn.MSELoss()
optimizer = optim.SGD(net.parameters(), lr=learning_rate)
#optimizer = torch.optim.AdamW(net.parameters(), lr=learning_rate)
dataset = torch.utils.data.TensorDataset(texta,textb,labels)
train_iter = torch.utils.data.DataLoader(dataset, batch_size, shuffle=True)
pre=0.5
acc_list=[]
index_list=[]
pre_list=[]
for epoch in range(num_epochs):
correct = 0
total=0
iter = 0
net.train()
for XA, XB , y in train_iter:
iter += 1
XA = XA.long()
XB = XB.long()
y=y.float()
if XA.size(0)!= batch_size:
break
if statea is not None:
if isinstance (statea, tuple): # LSTM, state:(h, c)
statea = (statea[0].detach(), statea[1].detach())
else:
statea = statea.detach()
if stateb is not None:
if isinstance (stateb, tuple): # LSTM, state:(h, c)
stateb = (stateb[0].detach(), stateb[1].detach())
else:
stateb = stateb.detach()
optimizer.zero_grad()
(output, statea,stateb) = net(XA,XB, statea,stateb,True)
loss = loss_fct(output, y)
#print(output)
loss.backward()
optimizer.step()
total += XA.size(0)
result = output.detach().cpu().numpy().tolist()
result=[ 1 if i[0]>0.5 else 0 for i in result]
answer=y.cpu().numpy().tolist()
for i in range(len(answer)):
#print(answer[i][0]," ",result[i])
if answer[i][0]==result[i]:
correct += 1
if iter % 200 ==0:
s = (((1.0 * correct) / total))
print(correct,"/" , total, "TrainAcc:", s)
#print(net.state_dict()["word_embeddings.weight"])
s = ((1.0 * correct) / total)
print("epoch: ",epoch, " ",correct,"/" , total, "TrainAcc:", s)
temp=eval(net,evala,evalb,evallabels,resulta,resultb,batch_size,pre,model_path)
if temp>pre:
pre=temp
acc_list.append(s)
index_list.append(epoch)
pre_list.append(temp)
return acc_list,index_list,pre_list
def Draw(acc_list,index_list,pre_list):
tempa = np.array(acc_list)
tempb = np.array(index_list)
tempc = np.array(pre_list)
fig = plt.figure(1)
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 1)
df1 = pd.DataFrame(tempa, index=tempb, columns=['Train'])
df2 = pd.DataFrame(tempc, index=tempb, columns=['Test'])
df1.plot(ax=ax1, kind='line', rot=360, grid='on')
ax1.set_xticks(range(len(index_list)))
ax1.set_xticklabels(range(len(index_list)))
df2.plot(ax=ax2, kind='line', rot=360, grid='on')
ax2.set_xticks(range(Epoch))
ax2.set_xticklabels(range(Epoch))
plt.show()
USE_Bi=True
if USE_Bi:
print("Using BiLSTM")
model = BiLSTM_Match(embedding_dim, hidden_dim, vocab_size, target, Batchsize, stringlen)
model_path = "./Model/BiLSTMmodel.pth"
else:
print("Using LSTM")
model = LSTM_Match(embedding_dim, hidden_dim, vocab_size,target,Batchsize,stringlen)
model_path = "./Model/LSTMmodel.pth"
if USE_CUDA:
model=model.cuda()
model=model.cuda()
print(model)
exit()
a,b,c=train(model,texta,textb,labels,evala,evalb,evallabels,resulta,resultb,Epoch,lr,Batchsize,model_path)
Draw(a,b,c)