-
Notifications
You must be signed in to change notification settings - Fork 2
/
wikileaks.py
344 lines (243 loc) · 10.3 KB
/
wikileaks.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""
Analysis of influential words in the Wikileaks Cables Dataset
-------------------------------------------------------------
The is a modified version of the keras tutorial on sentiment classification
of the IMDB movie review dataset. We use the Wikileaks dataset instead, and
perform a 3-way/2-way classification of unclassified, confidential and secret
(if 3-way) documents.
This tutorial shows how to extract the top words that make a document belong
to a particular class. We achieve this by using adversarial machine learning
tecniques that consist on computing the gradients of the loss of the model
with respect to the input. The loss is computed as:
loss = pred_class - target_class
where the target_class is a class in the dataset that is most different
compared to the pred_class. For example, if the pred_class of a document
is "unclassified", the target class will be "secret". This will give us
a high absolute value for the gradients of the words that are the least
secret. We say that these words are the most unclassified.
We record the top 3 words with the highest gradients, and get an overall
distribution of the most influential words for each class.
By Raquel Alvarez
"""
import pickle
import sys
import argparse
import numpy as np
from tensorflow import Session
from keras.backend import set_session
from keras.models import load_model
from keras.utils import to_categorical
#from sklearn.metrics import confusion_matrix
from utils.sequence_generator import SequenceGenerator, SequenceGeneratorForPrediction
from utils.colors import colors
from utils import data_preprocessing, models, influential_vocab
from tqdm import tqdm
#################################
# Arguments #
#################################
# Setup argument parser
parser = argparse.ArgumentParser(sys.argv[0], description='Extract the most influential words per class of a dataset.')
parser.add_argument('--num-classes', type=int, default=2, help='Number of classes in the dataset')
parser.add_argument('--pre-trained', type=bool, default=False, help='True/False use pre-trained model. Default is True.')
parser.add_argument('--use-class-weights', type=bool, default=False, help='True/False use class weights. Default is False.')
args = parser.parse_args()
# Parse arguments
num_classes = args.num_classes
pre_trained = args.pre_trained
use_class_weights = args.use_class_weights
#################################
# Keras Setup #
#################################
sess = Session() # Allows us to be able to pass the session to other libs
set_session(sess) # Keras uses the session we pass here
#################################
# Wikileaks Data Loading #
#################################
# Load Data
print("")
print(colors.BOLD + "Loading Data..." + colors.END)
# Open dataset files
x_file = open("dataset/wikileaks/"+str(num_classes)+"-way/dataset_vars/x_dataset.pkl", "rb")
y_file = open("dataset/wikileaks/"+str(num_classes)+"-way/dataset_vars/y_dataset.pkl", "rb")
word_index_file = open("dataset/wikileaks/"+str(num_classes)+"-way/dataset_vars/word_index_dict.pkl", "rb")
index_word_file = open("dataset/wikileaks/"+str(num_classes)+"-way/dataset_vars/index_word_dict.pkl", "rb")
max_doc_len_file = open("dataset/wikileaks/"+str(num_classes)+"-way/dataset_vars/max_len.pkl", "rb")
# Load precomputed structures
X = pickle.load(x_file)
y = pickle.load(y_file)
word_index_dict = pickle.load(word_index_file)
index_word_dict = pickle.load(index_word_file)
max_doc_len = pickle.load(max_doc_len_file)
# Convert labels to indices
# unclassified 0
# confidential 1
# secret 2
idx = 0
for label in y:
if label == "unclassified":
y[idx] = 0
elif label == "confidential":
y[idx] = 1
else:
y[idx] = 2
#print("Wrong in 2-way data...")
idx += 1
# Convert lists to numpy arrays
X = np.asarray(X)
y = np.asarray(y)
# Shuffle dataset
np.random.seed(113)
indices = np.arange(len(X))
np.random.shuffle(indices)
X = X[indices]
y = y[indices]
# Separate dataset
mid = int(len(X) / 2)
x_train = X[0:mid]
y_train = y[0:mid]
x_test = X[mid+1:]
y_test = y[mid+1:]
# Gather stats for training data
uncl_cnt = 0
conf_cnt = 0
secr_cnt = 0
for label in y_train:
if label == 0:
uncl_cnt += 1
elif label == 1:
conf_cnt += 1
else:
#print("Error 2")
secr_cnt += 1
print("--- Training Stats ---")
print("Unclassified: "+str(uncl_cnt))
print("Confidential: "+str(conf_cnt))
print("Secret: "+str(secr_cnt))
# Close files
x_file.close()
y_file.close()
word_index_file.close()
index_word_file.close()
max_doc_len_file.close()
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
print("")
#################################
# Parameters #
#################################
# Dataset Parameters
max_features = len(word_index_dict) # max number of unique words recognized from the dataset
maxlen = max_doc_len # max length of a document
num_classes = num_classes # number of classes
# Training Parameters
batch_size = 32 # run groups of 32 samples every training iteration
epochs = 5 # number of times to run over entire dataset during training
# Model Parameters
embedding_dims = 50 # number of dimensions of the word embedding vectors
filters = 250 # number of filters to apply on the conv. layer
kernel_size = 3 # window size of 3 words (3-grams)
hidden_dims = 250 # dense layer of 250 dimensions
model_exists = pre_trained # set this to True if a pre-trained model exists
use_class_weights = use_class_weights # train model using class weights
#################################
# Unbalanced Dataset Handling #
#################################
# Setup weights so the model "pays more attention" to
# the under-represented class "Secret"
class_weight = dict()
class_weight[0] = 1 # Unclassified
class_weight[1] = 2 # Confidential
class_weight[2] = 4 # Secret
#################################
# Data Preprocessing #
#################################
# Pad documents accordingly
print(colors.BOLD + "Padding sequences..." + colors.END)
x_train, x_test = data_preprocessing.pad_samples(x_train, x_test, maxlen)
# Save index-like (0,1,2) y vector for highest gradient computation
y_train_idx = np.copy(y_train)
y_test_idx = np.copy(y_test)
# Convert labels to one-hot vectors
print(colors.BOLD + "Converting word indices to one-hot vectors...\n" + colors.END)
y_train, y_test = data_preprocessing.one_hot_labels(y_train, y_test, num_classes)
# Setup data generator to feed to the model
print(colors.BOLD + "Setting up Data Generators...\n" + colors.END)
train_generator = SequenceGenerator(x_train, y_train, batch_size, max_features, num_classes)
test_generator = SequenceGenerator(x_test, y_test, batch_size, max_features, num_classes)
predict_generator = SequenceGeneratorForPrediction(x_test, batch_size, max_features)
#################################
# Classification Model #
#################################
model_file = 'pre_trained_models/wikileaks_model_'+str(num_classes)+'-way.h5'
if model_exists:
# Load model
print(colors.BOLD + colors.GREEN + 'Loading model...' + colors.END)
model = load_model(model_file)
print(model.summary())
else:
# Build model
print(colors.BOLD + colors.GREEN + 'Building model...' + colors.END)
model = models.get_cnn_classifier(maxlen, max_features, embedding_dims, filters, kernel_size, hidden_dims, num_classes, one_hot_input=True)
print(model.summary())
# Generate model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train and test model
#model_history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test))
if use_class_weights:
model_history = model.fit_generator(generator=train_generator, validation_data=test_generator, epochs=epochs, class_weight=class_weight)
else:
model_history = model.fit_generator(generator=train_generator, validation_data=test_generator, epochs=epochs)
# Save model for future use
model.save(model_file)
# Print model accuracy
print("")
print(colors.BOLD + colors.UNDERLINE + "Model Performance" + colors.END)
print(colors.WHITE + colors.BLUE_BG + "Training Accuracy: " + str(100*model_history.history['acc'][-1]) + colors.END)
print(colors.WHITE + colors.BLUE_BG + "Validation Accuracy: " + str(100*model_history.history['val_acc'][-1]) + colors.END)
# Write model performance to a file
#f = open("model_history.log", 'w')
#models.save_model_performance(f, model_history)
#f.close()
#################################
# Confusion Matrix #
#################################
print("")
print(colors.BOLD + colors.UNDERLINE + "Confusion Matrix" + colors.END)
conf_matrix = models.get_confusion_matrix(model, predict_generator, y_test)
print(conf_matrix)
print("")
###############################################
# Most Influential Words for the Test Dataset #
###############################################
print("")
print(colors.BOLD + colors.UNDERLINE + "Most Influential Words - Dataset (word: # of times it was the top 3 word on 1 of 100 random documents)" + colors.END)
print("")
top3_counter_unclassified, top3_counter_confidential, top3_counter_secret = influential_vocab.get_most_influential_words_for_dataset(x_train, y_train_idx, max_features, maxlen, num_classes, model, index_word_dict)
print(colors.GREEN_BG + colors.BLACK + "Unclassified Class Words: " + colors.END + colors.GREEN)
print(top3_counter_unclassified.most_common())
print(colors.END)
print(colors.YELLOW_BG + colors.BLACK + "Confidential Class Words: " + colors.END + colors.YELLOW)
print(top3_counter_confidential.most_common())
print(colors.END)
print(colors.RED_BG + colors.WHITE + "Secret Class Words: " + colors.END + colors.RED)
print(top3_counter_secret.most_common())
print(colors.END)
##########################################
# Top 3 Influential Words for a Document #
##########################################
print("")
print(colors.BOLD + colors.UNDERLINE + "Most Influential Words - Document" + colors.END)
document = x_test[18]
classification = y_test_idx[18] # can be replaced with model.predict(document) if the document's unlabeled
top3_document = influential_vocab.get_most_influential_words_for_document(document, classification, max_features, maxlen, num_classes, model, index_word_dict)
# Print results
if classification == 0:
print(colors.GREEN)
elif classification == 1:
print(colors.YELLOW)
else:
print(colors.RED)
print(top3_document)
print(colors.END)
# Close TF session
sess.close()