48
48
49
49
#%% Load the data for one of the sentence lengths
50
50
51
- sent_length = 10
51
+ sent_length = 15
52
52
53
53
# Load
54
54
with open (args .data_files_map [sent_length ]["tokenizer" ], "rb" ) as inFile :
@@ -175,7 +175,7 @@ def LSTMN_search(parameters):
175
175
# Define the search space
176
176
space = {
177
177
'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 ]),
179
179
'bidirectional' : hp .choice ('bidirectional' , [False , True ]),
180
180
'sent_length' : hp .choice ("sent_length" , [8 , 10 , 12 , 15 ]),
181
181
'use_class_weights' : hp .choice ("use_class_weights" , [True , False ]),
@@ -189,7 +189,7 @@ def LSTMN_search(parameters):
189
189
from hyperopt .pyll .stochastic import sample
190
190
parameters = sample (space )
191
191
print (parameters )
192
- po = LSTMN_search (parameters )
192
+ # po = LSTMN_search(parameters)
193
193
194
194
#%% Run the optimizer
195
195
@@ -243,37 +243,41 @@ def LSTMN_search(parameters):
243
243
idx_to_label = data ["idx_to_label" ]
244
244
label_to_idx = data ["labels_to_idx" ]
245
245
246
+ #%%
247
+
246
248
best = { ###########################################################################################################
247
249
"bidirectional" : True ,
248
250
"dropout_prop" : 0.17665639259474208 ,
249
251
"learning_rate" : 0.02318120952027118 ,
250
252
"nb_lstm_units" : 64 ,
251
- "nb_lstm_layers" : 3 ,
253
+ "nb_lstm_layers" : 1 ,
252
254
"use_class_weights" : True ,
253
255
"batch_size" : 128 ,
254
256
"num_classes" : len (np .unique (labels_vect )),
255
257
"epochs" : 9
256
258
}
257
259
258
260
# 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 )
260
262
# Make dataset
261
263
test = WikiDocData (val [0 ], val [1 ])
262
264
# 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" ])
264
268
# To cuda
265
269
WikiLSTM .to (device )
266
270
# Set up optimizer
267
- optimizer = optim .Adam (WikiLSTM .parameters (), lr = best . learning_rate )
271
+ optimizer = optim .Adam (WikiLSTM .parameters (), lr = best [ " learning_rate" ] )
268
272
# Criterion
269
- if best . use_class_weights :
273
+ if best [ " use_class_weights" ] :
270
274
criterion = nn .CrossEntropyLoss (weight = cw )
271
275
else :
272
276
criterion = nn .CrossEntropyLoss ()
273
277
274
278
# Training routine
275
279
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" ] ,
277
281
device = device )
278
282
279
283
#%% Evaluate the model on test data
0 commit comments