Skip to content

Commit fc73e69

Browse files
committed
Add images etc.
1 parent f83bccd commit fc73e69

9 files changed

+1458
-217
lines changed

.idea/workspace.xml

+108-58
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

3_cnn_model.R

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
rm(list=ls())
2+
13
#################################Start Here########################################
24
library(keras)
35
library(tidyverse)
@@ -107,9 +109,6 @@ saveRDS(Final_calibrated, "Final_calibrated.rds")
107109
#best model performance: accuracy 86.7%
108110
#with parameters: Round = 82 filter_n = 512.0000 filter_size = 4.0000 neuron_n = 512.0000 dropout = 0.0007 Value = 0.910
109111

110-
111-
112-
113112
####6.Final CNN Model#####
114113
model_cnn <- keras_model_sequential() %>%
115114
layer_embedding(input_dim = max_words, output_dim = embedding_dim,
@@ -161,6 +160,14 @@ prediction_cnn <- model_cnn %>% predict(x_test) %>%
161160

162161
prediction_cnn <- prediction_cnn - 1
163162

163+
# Save
164+
write.csv(data.frame(ypred=prediction_cnn, ytrue = y_test), "CNN_results.csv", row.names = FALSE)
165+
166+
library(reticulate)
167+
sklearn <- import("sklearn")
168+
rep <- sklearn$metrics$classification_report(prediction_cnn, y_train)
169+
cat(rep)
170+
164171
label_acc <- numeric()
165172

166173
for (i in 0:10) {

4_lstm_model.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
#%% Load the data for one of the sentence lengths
5050

51-
sent_length = 10
51+
sent_length = 15
5252

5353
# Load
5454
with open(args.data_files_map[sent_length]["tokenizer"], "rb") as inFile:
@@ -175,7 +175,7 @@ def LSTMN_search(parameters):
175175
# Define the search space
176176
space = {
177177
'nb_lstm_units': hp.choice('nb_lstm_units', [32,64,128]),
178-
'nb_lstm_layers': hp.choice('nb_lstm_layers', [1,2,3]),
178+
'nb_lstm_layers': hp.choice('nb_lstm_layers', [1,2]),
179179
'bidirectional': hp.choice('bidirectional', [False, True]),
180180
'sent_length': hp.choice("sent_length", [8, 10, 12, 15]),
181181
'use_class_weights': hp.choice("use_class_weights", [True, False]),
@@ -189,7 +189,7 @@ def LSTMN_search(parameters):
189189
from hyperopt.pyll.stochastic import sample
190190
parameters = sample(space)
191191
print(parameters)
192-
po = LSTMN_search(parameters)
192+
#po = LSTMN_search(parameters)
193193

194194
#%% Run the optimizer
195195

@@ -243,37 +243,41 @@ def LSTMN_search(parameters):
243243
idx_to_label = data["idx_to_label"]
244244
label_to_idx = data["labels_to_idx"]
245245

246+
#%%
247+
246248
best = { ###########################################################################################################
247249
"bidirectional" : True,
248250
"dropout_prop" : 0.17665639259474208,
249251
"learning_rate" : 0.02318120952027118,
250252
"nb_lstm_units" : 64,
251-
"nb_lstm_layers" : 3,
253+
"nb_lstm_layers" : 1,
252254
"use_class_weights" : True,
253255
"batch_size" : 128,
254256
"num_classes" : len(np.unique(labels_vect)),
255257
"epochs" : 9
256258
}
257259

258260
# Split
259-
train, val = split_data(docs_vectorized, labels_vect, 6754, p=0.05)
261+
train, val = split_data(docs_vectorized_lstm, labels_vect, 6754, p=0.05)
260262
# Make dataset
261263
test = WikiDocData(val[0], val[1])
262264
# Set up the model
263-
WikiLSTM = LSTMN(FTEMB, best.hidden_size, best.hidden_size, best.batch_size, best.num_classes)
265+
WikiLSTM = LSTMN(FTEMB, best["batch_size"], best["num_classes"],
266+
best["bidirectional"], best["nb_lstm_layers"],
267+
best["nb_lstm_units"], best["dropout_prop"])
264268
# To cuda
265269
WikiLSTM.to(device)
266270
# Set up optimizer
267-
optimizer = optim.Adam(WikiLSTM.parameters(), lr= best.learning_rate)
271+
optimizer = optim.Adam(WikiLSTM.parameters(), lr= best["learning_rate"])
268272
# Criterion
269-
if best.use_class_weights:
273+
if best["use_class_weights"]:
270274
criterion = nn.CrossEntropyLoss(weight=cw)
271275
else:
272276
criterion = nn.CrossEntropyLoss()
273277

274278
# Training routine
275279
WikiLSTM_out, history = train_lstmn(train[0], train[1], WikiLSTM, optimizer, criterion,
276-
epochs = best.epochs, val_split = 0.1, batch_size = best.batch_size,
280+
epochs = best["epochs"], val_split = 0.1, batch_size = best["batch_size"],
277281
device = device)
278282

279283
#%% Evaluate the model on test data

0 commit comments

Comments
 (0)