-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
331 lines (251 loc) · 9.26 KB
/
script.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#author: Sachin_singh
# coding: utf-8
import numpy as np
import pandas as pd
import csv
import re
import sys
import os
train_file = str(sys.argv[1])
test_file = str(sys.argv[2])
os.system("sed -i '/\"/d' ./"+str(train_file))
os.system("sed -i '/\"/d' ./"+str(test_file))
# Had to remove '"' entries from the 'train_data.tsv' file as they interfere in the reading/parsing of read_csv
# process.
# Reading the training file
names = ['tweetID', 'userID', 'st', 'ed', 'token', 'tag']
df = pd.read_csv(train_file, delimiter='\t', names = names, header=None)
print(df.tail())
print()
print("Train size:", len(df))
# reading the testing file
names = ['tweetID', 'userID', 'st', 'ed', 'token', 'tag']
df1 = pd.read_csv(test_file, delimiter='\t', names = names, header=None)
print(df1.tail())
print()
print("Test size:", len(df1))
# Concatinating both for getting total words and chars embeddings.
dt = [df, df1]
D = pd.concat(dt)
print("Total Data:")
print(D.tail())
# Total words in the corpus (train+test)
words = list(set(D["token"].values))
n_words = len(words)
print(n_words)
# number of tags
tags = list(set(D["tag"].values))
n_tags = len(tags)
print("Number of tags", n_tags) #es, en, other
# Creatinng Training data
t_id = df['tweetID'][0]
u_id = df['userID'][0]
tup = ()
sents_train = []
tmp_sent = []
for i in range(len(df)):
if(t_id == df['tweetID'][i] and u_id == df['userID'][i]):
tup = (str(df['token'][i]), str(df['tag'][i]))
tmp_sent.append(tup)
else:
t_id = df['tweetID'][i]
u_id = df['userID'][i]
sents_train.append(tmp_sent)
tmp_sent = []
tup = (str(df['token'][i]), str(df['tag'][i]))
tmp_sent.append(tup)
# Creating Testing data
t_id = df1['tweetID'][0]
u_id = df1['userID'][0]
tup = ()
sents_test = []
tmp_sent = []
for i in range(len(df)):
if(t_id == df['tweetID'][i] and u_id == df['userID'][i]):
tup = (str(df['token'][i]), str(df['tag'][i]))
tmp_sent.append(tup)
else:
t_id = df['tweetID'][i]
u_id = df['userID'][i]
sents_test.append(tmp_sent)
tmp_sent = []
tup = (str(df['token'][i]), str(df['tag'][i]))
tmp_sent.append(tup)
print("Train sents len: ", len(sents_train))
print("Test Sents len: ", len(sents_test))
# Fixing paramenters for future refereces
max_len = 75
max_len_char = 10
# Creaing dictionaries for words, tags as well as chars
word2idx = {w: i + 2 for i, w in enumerate(words)}
word2idx["UNK"] = 1
word2idx["PAD"] = 0
idx2word = {i: w for w, i in word2idx.items()}
tag2idx = {t: i + 1 for i, t in enumerate(tags)}
tag2idx["PAD"] = 0
idx2tag = {i: w for w, i in tag2idx.items()}
# Testing for the indexes
print(word2idx["where"])
print("En: ", tag2idx["en"])
print("Es: ", tag2idx["es"])
print("Other: ", tag2idx["other"])
# Creating word embeddings for train and test
X_word_tr = [[word2idx[w[0]] for w in s] for s in sents_train]
X_word_te = [[word2idx[w[0]] for w in s] for s in sents_test]
# Padding sequences to make all of same size
from keras.preprocessing.sequence import pad_sequences
# Padded word embeddings for traina and test words
X_word_tr = pad_sequences(maxlen=max_len, sequences=X_word_tr, value=word2idx["PAD"], padding='post', truncating='post')
X_word_te = pad_sequences(maxlen=max_len, sequences=X_word_te, value=word2idx["PAD"], padding='post', truncating='post')
# Total chars in the corpus
chars = set([w_i for w in words for w_i in w])
n_chars = len(chars)
print("Total characters: ", n_chars)
char2idx = {c: i + 2 for i, c in enumerate(chars)}
char2idx["UNK"] = 1
char2idx["PAD"] = 0
# train
X_char_tr = []
for sentence in sents_train:
sent_seq = []
for i in range(max_len):
word_seq = []
for j in range(max_len_char):
try:
word_seq.append(char2idx.get(sentence[i][0][j]))
except:
word_seq.append(char2idx.get("PAD"))
sent_seq.append(word_seq)
X_char_tr.append(np.array(sent_seq))
# test
X_char_te = []
for sentence in sents_test:
sent_seq = []
for i in range(max_len):
word_seq = []
for j in range(max_len_char):
try:
word_seq.append(char2idx.get(sentence[i][0][j]))
except:
word_seq.append(char2idx.get("PAD"))
sent_seq.append(word_seq)
X_char_te.append(np.array(sent_seq))
# Tags set for training and testing
y_tr = [[tag2idx[w[1]] for w in s] for s in sents_train]
y_te = [[tag2idx[w[1]] for w in s] for s in sents_test]
# Padding the tags sequence
y_tr = pad_sequences(maxlen=max_len, sequences=y_tr, value=tag2idx["PAD"], padding='post', truncating='post')
y_te = pad_sequences(maxlen=max_len, sequences=y_te, value=tag2idx["PAD"], padding='post', truncating='post')
# Importing modules for our DNN model
from keras.models import Model, Input
from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Conv1D
from keras.layers import Bidirectional, concatenate, SpatialDropout1D, GlobalMaxPooling1D
# input and embedding for words
word_in = Input(shape=(max_len,))
emb_word = Embedding(input_dim=n_words + 2, output_dim=20,
input_length=max_len, mask_zero=True)(word_in)
# input and embeddings for characters
char_in = Input(shape=(max_len, max_len_char,))
emb_char = TimeDistributed(Embedding(input_dim=n_chars + 2, output_dim=10,
input_length=max_len_char, mask_zero=True))(char_in)
# character LSTM to get word encodings by characters
char_enc = TimeDistributed(LSTM(units=20, return_sequences=False,
recurrent_dropout=0.5))(emb_char)
# main LSTM
x = concatenate([emb_word, char_enc])
x = SpatialDropout1D(0.3)(x)
main_lstm = Bidirectional(LSTM(units=50, return_sequences=True,
recurrent_dropout=0.6))(x)
out = TimeDistributed(Dense(n_tags + 1, activation="softmax"))(main_lstm)
model = Model([word_in, char_in], out)
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["sparse_categorical_crossentropy"])
print("Model Summary: ")
print(model.summary())
from keras.models import load_model
# fixing parameters for our BiLSTM model
print("Starting the training of our model......")
epochs = 10
batch_size = 256
history = model.fit([X_word_tr,
np.array(X_char_tr).reshape((len(X_char_tr), max_len, max_len_char))],
np.array(y_tr).reshape(len(y_tr), max_len, 1),
batch_size=batch_size, epochs=epochs, validation_split=0.2, verbose=1)
model.save('BiLSTMmodel.hdf5')
hist = pd.DataFrame(history.history)
# Displaying the variations of our Loss as well as validation loss while training
import matplotlib.pyplot as plt
plt.style.use("ggplot")
plt.figure(figsize=(12,12))
plt.plot(hist["loss"])
plt.plot(hist["val_loss"])
plt.show()
# Prediction using our trained Model
print("Initiating our prediction of test data...............")
y_pred = model.predict([X_word_te,
np.array(X_char_te).reshape((len(X_char_te),
max_len, max_len_char))])
# Printing the true and predicted tags for the test tokesns
# This is also important as we'll derive our classifiction report as well as confusion matrix from
# this result only
print(30 * "-")
print("Comparision of true vs predicted tags")
print(30 * "-")
print("{:15}||{:5}||{}".format("Word", "True", "Pred"))
print(30 * "=")
Y1 = []
Y2 = []
for i in range(len(y_pred)):
p = np.argmax(y_pred[i], axis=-1)
for w, t, pred in zip(X_word_te[i], y_te[i], p):
if w != 0:
print("{:15}: {:5} {}".format(idx2word[w], idx2tag[t], idx2tag[pred]))
Y1.append(idx2tag[t])
Y2.append(idx2tag[pred])
print("Counts of different tags in Test set")
print("En: ", Y1.count('en'))
print("Es: ", Y1.count('es'))
print("Other: ", Y1.count('other'))
k = len(Y1)
# These are computed as to be used to check our models performance
P1 = np.zeros((k,3), dtype=np.float32)
P2 = np.zeros((k,3), dtype=np.float32)
for i in range(k):
if Y1[i]=='other':
P1[i][0]=1.0
elif Y1[i]=='en':
P1[i][1]=1.0
elif Y1[i]=='es':
P1[i][2]=1.0
for i in range(k):
if Y2[i]=='other':
P2[i][0]=1.0
elif Y2[i]=='en':
P2[i][1]=1.0
elif Y2[i]=='es':
P2[i][2]=1.0
# print(P1, P2)
from sklearn.metrics import classification_report, confusion_matrix
# Confusion matrix for our prediction model
P1 = np.argmax(P1, axis=1)
for ix in range(3):
print(ix, confusion_matrix(np.argmax(P2, axis=1), P1)[ix].sum())
cm = confusion_matrix(np.argmax(P2, axis=1), P1)
print(cm)
import seaborn as sn
import matplotlib.pyplot as plt
# An image of our confusion matrix
df_cm = pd.DataFrame(cm, range(3), range(3))
plt.figure(figsize=(10,7))
sn.set(font_scale=1.4)
sn.heatmap(df_cm, annot=False)
sn.set_context("poster")
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.title("Confusion Matrix")
plt.show()
# for computng the classification report as well as the accuracy of our model.
target_names = ['other', 'en', 'es']
print("Classification report for our model running on the test file")
print(classification_report(np.argmax(P2, axis=1), P1, target_names=target_names))
from sklearn.metrics import accuracy_score
print("Accuracy: ", accuracy_score(np.argmax(P2, axis=1), P1, normalize=True))