-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
172 lines (141 loc) · 4.77 KB
/
test.py
File metadata and controls
172 lines (141 loc) · 4.77 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
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
from esmi import ESIM
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
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=resulta.cuda()
def out_put(net,resulta,resultb):
net.eval()
dataset = torch.utils.data.TensorDataset(resulta, resultb)
train_iter = torch.utils.data.DataLoader(dataset, 1, shuffle=False)
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()
output = net(XA, XB)
_, predicted = torch.max(output.data, -1)
predicted = predicted.reshape(-1)
result = predicted.cpu().numpy().tolist()
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
with torch.no_grad():
for XA, XB , y in train_iter:
XA = XA.long()
XB = XB.long()
if XA.size(0)!= batch_size:
break
output=net(XA,XB)
_, predicted = torch.max(output.data, -1)
y = y.reshape(-1)
total += XA.size(0)
predicted = predicted.reshape(-1)
y = y.reshape(-1)
correct += predicted.data.eq(y.data).cpu().sum()
s = correct.item()/total
print(correct.item(),"/" , total, "TestAcc: ", s)
if s > pre:
print("Flush and save model")
torch.save(net.state_dict(), model_path)
out_put(net,resulta,resultb)
return s
def train(net, texta,textb,labels,evala,evalb,evallabels,resulta,resultb,num_epochs, learning_rate, batch_size,model_path):
net.train()
loss_fct = nn.CrossEntropyLoss()
optimizer = optim.SGD(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()
y=y.long()
if XA.size(0)!= batch_size:
break
optimizer.zero_grad()
output=net(XA,XB)
_, predicted = torch.max(output.data, -1)
y=y.reshape(-1)
#predicted=predicted.float()
#print(output.size())
#print(y.size())
loss = loss_fct(output, y)
#exit()
#print(predicted.data)
#print(output)
loss.backward()
optimizer.step()
total += XA.size(0)
predicted=predicted.reshape(-1)
y=y.reshape(-1)
correct += predicted.data.eq(y.data).cpu().sum()
if iter % 200 ==0:
s = correct.item()/total
print(correct,"/" , total, "TrainAcc:", s)
#print(net.state_dict()["word_embeddings.weight"])
s = correct.item()/total
print("epoch: ",epoch, " ",correct,"/" , total, "TrainAcc:", s)
return
USE_Bi=True
model=ESIM(256,400,256)
if USE_CUDA:
model=model.cuda()
model_path="./Model/ESIM.pkt"
model=model.cuda()
print(model)
train(model,texta,textb,labels,evala,evalb,evallabels,resulta,resultb,Epoch,lr,Batchsize,model_path)