-
Notifications
You must be signed in to change notification settings - Fork 81
/
lstm.py
294 lines (248 loc) · 9.87 KB
/
lstm.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
from data_handler import get_data
import argparse
from keras.preprocessing.sequence import pad_sequences
from keras.layers import Embedding, Input, LSTM
from keras.models import Sequential, Model
from keras.layers import Activation, Dense, Dropout, Embedding, Flatten, Input, Merge, Convolution1D, MaxPooling1D, GlobalMaxPooling1D
import numpy as np
import pdb
from sklearn.metrics import make_scorer, f1_score, accuracy_score, recall_score, precision_score, classification_report, precision_recall_fscore_support
from sklearn.ensemble import GradientBoostingClassifier, RandomForestClassifier
from gensim.parsing.preprocessing import STOPWORDS
from sklearn.model_selection import KFold
from keras.utils import np_utils
import codecs
import operator
import gensim, sklearn
from string import punctuation
from collections import defaultdict
from batch_gen import batch_gen
import sys
from nltk import tokenize as tokenize_nltk
from my_tokenizer import glove_tokenize
### Preparing the text data
texts = [] # list of text samples
labels_index = {} # dictionary mapping label name to numeric id
labels = [] # list of label ids
# vocab generation
vocab, reverse_vocab = {}, {}
freq = defaultdict(int)
tweets = {}
EMBEDDING_DIM = None
GLOVE_MODEL_FILE = None
SEED = 42
NO_OF_FOLDS = 10
CLASS_WEIGHT = None
LOSS_FUN = None
OPTIMIZER = None
KERNEL = None
TOKENIZER = None
MAX_SEQUENCE_LENGTH = None
INITIALIZE_WEIGHTS_WITH = None
LEARN_EMBEDDINGS = None
EPOCHS = 10
BATCH_SIZE = 512
SCALE_LOSS_FUN = None
word2vec_model = None
def get_embedding(word):
#return
try:
return word2vec_model[word]
except Exception, e:
print 'Encoding not found: %s' %(word)
return np.zeros(EMBEDDING_DIM)
def get_embedding_weights():
embedding = np.zeros((len(vocab) + 1, EMBEDDING_DIM))
n = 0
for k, v in vocab.iteritems():
try:
embedding[v] = word2vec_model[k]
except:
n += 1
pass
print "%d embedding missed"%n
return embedding
def select_tweets():
# selects the tweets as in mean_glove_embedding method
# Processing
tweets = get_data()
X, Y = [], []
tweet_return = []
for tweet in tweets:
_emb = 0
words = TOKENIZER(tweet['text'].lower())
for w in words:
if w in word2vec_model: # Check if embeeding there in GLove model
_emb+=1
if _emb: # Not a blank tweet
tweet_return.append(tweet)
print 'Tweets selected:', len(tweet_return)
#pdb.set_trace()
return tweet_return
def gen_vocab():
# Processing
vocab_index = 1
for tweet in tweets:
text = TOKENIZER(tweet['text'].lower())
text = ''.join([c for c in text if c not in punctuation])
words = text.split()
words = [word for word in words if word not in STOPWORDS]
for word in words:
if word not in vocab:
vocab[word] = vocab_index
reverse_vocab[vocab_index] = word # generate reverse vocab as well
vocab_index += 1
freq[word] += 1
vocab['UNK'] = len(vocab) + 1
reverse_vocab[len(vocab)] = 'UNK'
def filter_vocab(k):
global freq, vocab
pdb.set_trace()
freq_sorted = sorted(freq.items(), key=operator.itemgetter(1))
tokens = freq_sorted[:k]
vocab = dict(zip(tokens, range(1, len(tokens) + 1)))
vocab['UNK'] = len(vocab) + 1
def gen_sequence():
y_map = {
'none': 0,
'racism': 1,
'sexism': 2
}
X, y = [], []
for tweet in tweets:
text = TOKENIZER(tweet['text'].lower())
text = ''.join([c for c in text if c not in punctuation])
words = text.split()
words = [word for word in words if word not in STOPWORDS]
seq, _emb = [], []
for word in words:
seq.append(vocab.get(word, vocab['UNK']))
X.append(seq)
y.append(y_map[tweet['label']])
return X, y
def shuffle_weights(model):
weights = model.get_weights()
weights = [np.random.permutation(w.flat).reshape(w.shape) for w in weights]
model.set_weights(weights)
def lstm_model(sequence_length, embedding_dim):
model_variation = 'LSTM'
print('Model variation is %s' % model_variation)
model = Sequential()
model.add(Embedding(len(vocab)+1, embedding_dim, input_length=sequence_length, trainable=LEARN_EMBEDDINGS))
model.add(Dropout(0.25))#, input_shape=(sequence_length, embedding_dim)))
model.add(LSTM(50))
model.add(Dropout(0.5))
model.add(Dense(3))
model.add(Activation('softmax'))
model.compile(loss=LOSS_FUN, optimizer=OPTIMIZER, metrics=['accuracy'])
print model.summary()
return model
def train_LSTM(X, y, model, inp_dim, weights, epochs=EPOCHS, batch_size=BATCH_SIZE):
cv_object = KFold(n_splits=NO_OF_FOLDS, shuffle=True, random_state=42)
print cv_object
p, r, f1 = 0., 0., 0.
p1, r1, f11 = 0., 0., 0.
sentence_len = X.shape[1]
for train_index, test_index in cv_object.split(X):
if INITIALIZE_WEIGHTS_WITH == "glove":
model.layers[0].set_weights([weights])
elif INITIALIZE_WEIGHTS_WITH == "random":
shuffle_weights(model)
else:
print "ERROR!"
return
X_train, y_train = X[train_index], y[train_index]
X_test, y_test = X[test_index], y[test_index]
y_train = y_train.reshape((len(y_train), 1))
X_temp = np.hstack((X_train, y_train))
for epoch in xrange(epochs):
for X_batch in batch_gen(X_temp, batch_size):
x = X_batch[:, :sentence_len]
y_temp = X_batch[:, sentence_len]
class_weights = None
if SCALE_LOSS_FUN:
class_weights = {}
class_weights[0] = np.where(y_temp == 0)[0].shape[0]/float(len(y_temp))
class_weights[1] = np.where(y_temp == 1)[0].shape[0]/float(len(y_temp))
class_weights[2] = np.where(y_temp == 2)[0].shape[0]/float(len(y_temp))
try:
y_temp = np_utils.to_categorical(y_temp, nb_classes=3)
except Exception as e:
print e
print y_temp
print x.shape, y.shape
loss, acc = model.train_on_batch(x, y_temp, class_weight=class_weights)
print loss, acc
y_pred = model.predict_on_batch(X_test)
y_pred = np.argmax(y_pred, axis=1)
print classification_report(y_test, y_pred)
print precision_recall_fscore_support(y_test, y_pred)
print y_pred
p += precision_score(y_test, y_pred, average='weighted')
p1 += precision_score(y_test, y_pred, average='micro')
r += recall_score(y_test, y_pred, average='weighted')
r1 += recall_score(y_test, y_pred, average='micro')
f1 += f1_score(y_test, y_pred, average='weighted')
f11 += f1_score(y_test, y_pred, average='micro')
print "macro results are"
print "average precision is %f" %(p/NO_OF_FOLDS)
print "average recall is %f" %(r/NO_OF_FOLDS)
print "average f1 is %f" %(f1/NO_OF_FOLDS)
print "micro results are"
print "average precision is %f" %(p1/NO_OF_FOLDS)
print "average recall is %f" %(r1/NO_OF_FOLDS)
print "average f1 is %f" %(f11/NO_OF_FOLDS)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='LSTM based models for twitter Hate speech detection')
parser.add_argument('-f', '--embeddingfile', required=True)
parser.add_argument('-d', '--dimension', required=True)
parser.add_argument('--tokenizer', choices=['glove', 'nltk'], required=True)
parser.add_argument('--loss', default=LOSS_FUN, required=True)
parser.add_argument('--optimizer', default=OPTIMIZER, required=True)
parser.add_argument('--epochs', default=EPOCHS, required=True)
parser.add_argument('--batch-size', default=BATCH_SIZE, required=True)
parser.add_argument('-s', '--seed', default=SEED)
parser.add_argument('--folds', default=NO_OF_FOLDS)
parser.add_argument('--kernel', default=KERNEL)
parser.add_argument('--class_weight')
parser.add_argument('--initialize-weights', choices=['random', 'glove'], required=True)
parser.add_argument('--learn-embeddings', action='store_true', default=False)
parser.add_argument('--scale-loss-function', action='store_true', default=False)
args = parser.parse_args()
GLOVE_MODEL_FILE = args.embeddingfile
EMBEDDING_DIM = int(args.dimension)
SEED = int(args.seed)
NO_OF_FOLDS = int(args.folds)
CLASS_WEIGHT = args.class_weight
LOSS_FUN = args.loss
OPTIMIZER = args.optimizer
KERNEL = args.kernel
if args.tokenizer == "glove":
TOKENIZER = glove_tokenize
elif args.tokenizer == "nltk":
TOKENIZER = tokenize_nltk.casual.TweetTokenizer(strip_handles=True, reduce_len=True).tokenize
INITIALIZE_WEIGHTS_WITH = args.initialize_weights
LEARN_EMBEDDINGS = args.learn_embeddings
EPOCHS = int(args.epochs)
BATCH_SIZE = int(args.batch_size)
SCALE_LOSS_FUN = args.scale_loss_function
np.random.seed(SEED)
print 'GLOVE embedding: %s' %(GLOVE_MODEL_FILE)
print 'Embedding Dimension: %d' %(EMBEDDING_DIM)
print 'Allowing embedding learning: %s' %(str(LEARN_EMBEDDINGS))
word2vec_model = gensim.models.Word2Vec.load_word2vec_format(GLOVE_MODEL_FILE)
tweets = select_tweets()
gen_vocab()
#filter_vocab(20000)
X, y = gen_sequence()
#Y = y.reshape((len(y), 1))
MAX_SEQUENCE_LENGTH = max(map(lambda x:len(x), X))
print "max seq length is %d"%(MAX_SEQUENCE_LENGTH)
data = pad_sequences(X, maxlen=MAX_SEQUENCE_LENGTH)
y = np.array(y)
data, y = sklearn.utils.shuffle(data, y)
W = get_embedding_weights()
model = lstm_model(data.shape[1], EMBEDDING_DIM)
#model = lstm_model(data.shape[1], 25, get_embedding_weights())
train_LSTM(data, y, model, EMBEDDING_DIM, W)
pdb.set_trace()