-
Notifications
You must be signed in to change notification settings - Fork 4
/
basic_BiLSTM.py
221 lines (185 loc) · 7.21 KB
/
basic_BiLSTM.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
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
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 9 09:10:48 2018
@author: Deep
"""
import dataProcess
import testReader
from gensim.models import KeyedVectors
import os
import numpy as np
from keras.models import Sequential
from keras.layers.embeddings import Embedding
from keras.layers.wrappers import Bidirectional
from keras.layers import Dense, Merge, Dropout, Flatten
from keras.layers.recurrent import LSTM
TRAIN_QA_DIR = "C:/Users/Deep/Desktop/SemEval t11/data/train_qa.txt"
TRAIN_INS_DIR = "C:/Users/Deep/Desktop/SemEval t11/data/train_ins.txt"
DEV_QA_DIR = "C:/Users/Deep/Desktop/SemEval t11/data/dev_qa.txt"
DEV_INS_DIR = "C:/Users/Deep/Desktop/SemEval t11/data/dev_ins.txt"
TEST_QA_DIR = "C:/Users/Deep/Desktop/gold_data/test_data_qa.txt"
TEST_INS_DIR = "C:/Users/Deep/Desktop/gold_data/test_data_ins.txt"
WORDVEC_DIR = "C:/Users/Deep/Desktop/SemEval t11/w2v"
#WORD2VEC_BIN = "GoogleNews-vectors-negative300.bin"
WORD2VEC_BIN = "glove.840B.300d.txt"
WORD2VEC_EMBED_SIZE = 300
QA_EMBED_SIZE = 64
BATCH_SIZE = 512
EPOCHS = 6
seed = 7
np.random.seed(seed)
# 加载数据
print("Loading and formatting data...")
sqac_pairs_tokenizes = dataProcess.get_sqac_pairs_tokenizes(TRAIN_QA_DIR, TRAIN_INS_DIR)
dev_sqac_pairs_tokenizes = dataProcess.get_sqac_pairs_tokenizes(DEV_QA_DIR, DEV_INS_DIR)
test_sqac_pairs_tokenizes = testReader.get_sqac_pairs_tokenizes(TEST_QA_DIR, TEST_INS_DIR)
all_pairs_tokenizes = sqac_pairs_tokenizes + dev_sqac_pairs_tokenizes + test_sqac_pairs_tokenizes
story_maxlen = max([len(sqatriple[0]) for sqatriple in all_pairs_tokenizes])
question_maxlen = max([len(sqatriple[1]) for sqatriple in all_pairs_tokenizes])
answer_maxlen = max([len(sqatriple[2]) for sqatriple in all_pairs_tokenizes])
word2idx = dataProcess.build_vocab_from_sqac_pairs_tokenizes(all_pairs_tokenizes)
vocab_size = len(word2idx) + 1 # include mask character 0
# 训练集
Xs, Xq, Xa, Y = dataProcess.vectorize_sqac_pairs_tokenizes(sqac_pairs_tokenizes, word2idx, story_maxlen, question_maxlen, answer_maxlen)
# 开发集
Xs_dev, Xq_dev, Xa_dev, Y_dev = dataProcess.vectorize_sqac_pairs_tokenizes(dev_sqac_pairs_tokenizes, word2idx, story_maxlen, question_maxlen, answer_maxlen)
# 测试集
Xs_test, Xq_test, Xa_test, Y_test = testReader.vectorize_sqac_pairs_tokenizes(test_sqac_pairs_tokenizes, word2idx, story_maxlen, question_maxlen, answer_maxlen)
# 将训练集和开发集合并成为新的训练集,19674 + 2834 = 22508
#Xs_all = np.vstack((Xs, Xs_dev))
#Xq_all = np.vstack((Xq, Xq_dev))
#Xa_all = np.vstack((Xa, Xa_dev))
#Y_all = np.vstack((Y, Y_dev))
# get embeddings from word2vec
print("Loading Word2Vec model and generating embedding matrix...")
#word2vec = KeyedVectors.load_word2vec_format(os.path.join(WORDVEC_DIR, WORD2VEC_BIN), binary=True)
word2vec = KeyedVectors.load_word2vec_format(os.path.join(WORDVEC_DIR, WORD2VEC_BIN), binary=False, unicode_errors='ignore')
embedding_weights = np.zeros((vocab_size, WORD2VEC_EMBED_SIZE))
num_unknownwords = 0
unknow_words = []
for word, index in word2idx.items():
try:
embedding_weights[index, :] = word2vec[word]
except KeyError:
num_unknownwords += 1
unknow_words.append(word)
embedding_weights[index, :] = np.random.uniform(-0.25, 0.25, WORD2VEC_EMBED_SIZE)
# print(word2vec.wv.vocab.__len__())
##########################################################################################################
print("Building model...")
# story encoder.
# output shape: (None, story_maxlen, QA_EMBED_SIZE)
senc = Sequential()
senc.add(Embedding(output_dim=WORD2VEC_EMBED_SIZE, input_dim=vocab_size,
input_length=story_maxlen,
weights=[embedding_weights], mask_zero=True))
#senc.add(LSTM(QA_EMBED_SIZE, return_sequences=True))
senc.add(Bidirectional(LSTM(QA_EMBED_SIZE, return_sequences=True),
merge_mode="sum"))
senc.add(Dropout(0.3))
# question encoder
# output shape: (None, question_maxlen, QA_EMBED_SIZE)
qenc = Sequential()
qenc.add(Embedding(output_dim=WORD2VEC_EMBED_SIZE, input_dim=vocab_size,
input_length=question_maxlen,
weights=[embedding_weights], mask_zero=True))
#qenc.add(LSTM(QA_EMBED_SIZE, return_sequences=True))
qenc.add(Bidirectional(LSTM(QA_EMBED_SIZE, return_sequences=True),
merge_mode="sum"))
qenc.add(Dropout(0.3))
# answer encoder
# output shape: (None, answer_maxlen, QA_EMBED_SIZE)
aenc = Sequential()
aenc.add(Embedding(output_dim=WORD2VEC_EMBED_SIZE, input_dim=vocab_size,
input_length=answer_maxlen,
weights=[embedding_weights], mask_zero=True))
#aenc.add(LSTM(QA_EMBED_SIZE, return_sequences=True))
aenc.add(Bidirectional(LSTM(QA_EMBED_SIZE, return_sequences=True),
merge_mode="sum"))
aenc.add(Dropout(0.3))
# merge story and question => facts
# output shape: (None, story_maxlen, question_maxlen)
facts = Sequential()
facts.add(Merge([senc, qenc], mode="dot", dot_axes=[2, 2]))
# merge question and answer => attention
# output shape: (None, answer_maxlen, question_maxlen)
attn = Sequential()
attn.add(Merge([aenc, qenc], mode="dot", dot_axes=[2, 2]))
# merge facts and attention => model
# output shape: (None, story+answer_maxlen, question_maxlen)
model = Sequential()
model.add(Merge([facts, attn], mode="concat", concat_axis=1))
model.add(Flatten())
model.add(Dense(2, activation="softmax"))
model.compile(optimizer="adam", loss="categorical_crossentropy",
metrics=["accuracy"])
print("Training...")
model.fit([Xs, Xq, Xa], Y, batch_size=BATCH_SIZE, epochs=EPOCHS)
predict_Y = model.predict([Xs_dev, Xq_dev, Xa_dev])
# 第0个答案偏向正确的概率
a_0 = (predict_Y[::2])[:,0]
# 第1个答案偏向正确的概率
a_1 = (predict_Y[1::2])[:,0]
# 自己动手计算准确率
# asw是提交文件的第三列
asw = []
for a in zip(a_0, a_1):
#print (i)
if a[0] > a[1]:
asw.append(0)
else:
asw.append(1)
aidx = []
dev_qa = list(open(DEV_QA_DIR, 'r'))
for line in dev_qa:
line = line.strip()
line = line.split("\t")
del line[0:5]
aidx.append(line)
selfEva = []
for t in zip(aidx, asw):
#print (t)
if int(t[0][0]) == t[1]:
selfEva.append(1)
else:
selfEva.append(0)
num_correct = 0
for i in selfEva:
if i == 1:
num_correct += 1
self_accuracy = num_correct/len(selfEva)
print (self_accuracy)
# 在测试集上的预测
predict_test_1 = model.predict([Xs_test, Xq_test, Xa_test])
# 第0个答案偏向正确的概率
test_a_0_1 = (predict_test_1[::2])[:,0]
# 第1个答案偏向正确的概率
test_a_1_1 = (predict_test_1[1::2])[:,0]
# 自己动手计算准确率
asw = []
for a in zip(test_a_0_1, test_a_1_1):
#print (i)
if a[0] > a[1]:
asw.append(0)
else:
asw.append(1)
aidx = []
test_qa = list(open(TEST_QA_DIR, 'r'))
for line in test_qa:
line = line.strip()
line = line.split("\t")
del line[0:6]
aidx.append(line)
selfEva = []
for t in zip(aidx, asw):
#print (t)
if int(t[0][0]) == t[1]:
selfEva.append(1)
else:
selfEva.append(0)
num_correct = 0
for i in selfEva:
if i == 1:
num_correct += 1
self_accuracy = num_correct/len(selfEva)
print (self_accuracy)