forked from NonvolatileMemory/AAAI_2019_EXAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextEXAM_multi-label.py
195 lines (167 loc) · 5.4 KB
/
TextEXAM_multi-label.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
from mxnet import init
import mxnet as mx
from mxnet.gluon import nn
import numpy as np
from mxnet import nd
from mxnet.gluon import rnn
import pickle
from mxnet import gluon
from mxnet import autograd
"""
readme:
今日更新,更改为BIMPM w/o max attentive的形式进行测试
readme:
今日更新,使用预训练的embedding以及合适的词表大小
readme:
今日更新,更改为最高成绩的smn
readme:
今日更新,使用cnn进行分类
"""
val_size = 10000
inter = 2000
intra = 2000
batch_size = 1000
ctx = mx.gpu(0)
max_turn = 1
import pickle
vocab_raw = open('vocab.pkl','rb')
vocab = pickle.load(vocab_raw)
def batch_attention(encoder,decoder):
attention = nd.softmax(nd.batch_dot(encoder,nd.transpose(decoder,axes = (0,2,1))),axis=1)
new_decoder = nd.batch_dot(attention,nd.transpose(encoder,axes=(0,1,2)))
return new_decoder
train = open('fenlei_train','r')
val = open('fenlei_val','r')
def word2token(text,vocab):
count = 0
for qes in text:
text[count] = vocab.get(int(qes),1)
count = count+1
return text
def get_single_data(raw_data,batch_size,max_l):
X,y = np.zeros((4000000,30)),np.zeros((3500000,2000))
i = 0
t = 0
count = 0
err = 0
for line in raw_data:
count = count +1
line = line.strip()
try:
question = line.split('\t')[0]
topic = line.split('\t')[1]
except:
print(line)
try:
question = question.split(' ')
topic = topic.split(' ')
question = word2token(question,vocab)
except:
err = err + 1
print(err)
if(False):
break
else:
# print(t)
question = question[0:30]
try:
X[t] = [0]*(30-len(question)) + question
y[t,min(int(topic[0]),1999)] = 1
y[t,min(int(topic[1]),1999)] = 1
y[t,min(int(topic[2]),1999)] = 1
y[t,min(int(topic[3]),1999)] = 1
y[t,min(int(topic[4]),1999)] = 1
y[t,1999] = 0
# y[t,0] = topic[0]
# y[t,1] = topic[1]
# y[t,2] = topic[2]
# y[t,3] = topic[3]
# y[t,4] = topic[4]
except:
print(line)
print(t)
t = t + 1
if(t%10000==0):
print(t)
y = y[0:t]
y = y[0:((len(y)//batch_size)*batch_size)]
X = X[0:len(y)]
X = nd.array(X,ctx=ctx)
# y = nd.array(y,dtype='int32')
print("get data")
train_dataset = gluon.data.ArrayDataset(X, y)
train_data_iter = gluon.data.DataLoader(train_dataset, batch_size, shuffle=False)
return train_data_iter
class SMN_Last(nn.Block):
def __init__(self,**kwargs):
super(SMN_Last,self).__init__(**kwargs)
with self.name_scope():
self.Embed = nn.Embedding(411721,256)
# agg param
self.gru = rnn.GRU(1024,2,layout='NTC')
self.mlp_1 = nn.Dense(units=60,flatten=False,activation='relu')
self.mlp_2 = nn.Dense(units=1,flatten=False)
# lstm param
self.topic_embedding = self.params.get('param_test',shape=(1024,2000))
def forward(self,x):
"""
return shape:(batch_size,2000,2)
"""
# Encode layer
question = x[:,0:30]
question = self.Embed(question)
question = self.gru(question)
#interaction layer
interaction = nd.dot(question,self.topic_embedding.data())
interaction = nd.transpose(interaction,axes=(0,2,1))
interaction = interaction.reshape((batch_size*2000,-1))
# interaction = interaction.expand_dims(axis=1)
# print("interaction done")
#agg layer
# interaction = self.pooling(self.conv_2(self.conv_1(interaction)))
# print("agg done")
res = self.mlp_2(self.mlp_1(interaction))
res = res.reshape((batch_size,2000))
return res
#Train Model
SMN = SMN_Last()
SMN.initialize(ctx=ctx)
embed_raw = open('embed_clean','rb')
word2vec = (nd.array(np.loadtxt(embed_raw))).copyto(ctx)
SMN.Embed.weight.set_data(word2vec)
train_iter = get_single_data(train,batch_size,max_l=50)
val_iter = get_single_data(val,batch_size,max_l=50)
max_epoch = 300
Sloss = gluon.loss.SigmoidBCELoss()
trainer = gluon.Trainer(SMN.collect_params(), 'adam', {'learning_rate': 0.001})
from score import score
def get_label(label):
dense_label = nd.zeros((batch_size,2000),ctx=ctx)
for row in range(0,batch_size):
for col in range(0,5):
index = label[row,col]
if(index<2000):
dense_label[row,index] = 1
return dense_label
SMN.Embed.weight.lr_mut = 0
for epoch in range(max_epoch):
train_loss = 0.
train_acc = 0.
count = 0
kk = 0
for data, label in train_iter:
if(epoch>0):
SMN.Embed.weight.lr_mut = 0.001
# data = data.copyto(ctx)
# dense_label = get_label(label)
with autograd.record():
output = SMN(data)
loss = Sloss(output, nd.array(label,ctx=ctx).astype('float32'))
count = count + 1
loss.backward()
trainer.step(batch_size)
if(count%10==1):
print("loss:")
print(nd.mean(loss).asscalar()
print("Epoch %d. Loss: %f" % (
epoch, train_loss/len(train_iter))