forked from MaurizioFD/RecSys2019_DeepLearning_Evaluation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_WWW_18_Mult_VAE.py
510 lines (344 loc) · 22.5 KB
/
run_WWW_18_Mult_VAE.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 22/11/17
@author: Maurizio Ferrari Dacrema
"""
from Recommender_import_list import *
from Conferences.WWW.MultiVAE_our_interface.MultiVAE_RecommenderWrapper import Mult_VAE_RecommenderWrapper
from ParameterTuning.SearchSingleCase import SearchSingleCase
from ParameterTuning.SearchAbstractClass import SearchInputRecommenderArgs
from ParameterTuning.run_parameter_search import runParameterSearch_Collaborative
from Utils.ResultFolderLoader import ResultFolderLoader, generate_latex_hyperparameters
from functools import partial
import os, traceback, argparse
import numpy as np
from Conferences.WWW.MultiVAE_our_interface.EvaluatorUserSubsetWrapper import EvaluatorUserSubsetWrapper, MF_cold_user_wrapper
from Utils.assertions_on_data_for_experiments import assert_implicit_data, assert_disjoint_matrices
######################################################################
from skopt.space import Real, Integer, Categorical
from ParameterTuning.SearchBayesianSkopt import SearchBayesianSkopt
from ParameterTuning.SearchSingleCase import SearchSingleCase
from ParameterTuning.SearchAbstractClass import SearchInputRecommenderArgs
def runParameterSearch_cold_user_MF(recommender_class, URM_train, URM_train_last_test = None, metric_to_optimize = "PRECISION",
evaluator_validation = None, evaluator_test = None, evaluator_validation_earlystopping = None,
output_folder_path ="result_experiments/",
n_cases = 35, n_random_starts = 5, resume_from_saved = True):
# If directory does not exist, create
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
earlystopping_keywargs = {"validation_every_n": 5,
"stop_on_validation": True,
"evaluator_object": evaluator_validation_earlystopping,
"lower_validations_allowed": 5,
"validation_metric": metric_to_optimize,
}
URM_train = URM_train.copy()
if URM_train_last_test is not None:
URM_train_last_test = URM_train_last_test.copy()
try:
output_file_name_root = recommender_class.RECOMMENDER_NAME
##########################################################################################################
if recommender_class is MatrixFactorization_FunkSVD_Cython:
hyperparameters_range_dictionary = {}
hyperparameters_range_dictionary["sgd_mode"] = Categorical(["sgd", "adagrad", "adam"])
hyperparameters_range_dictionary["epochs"] = Categorical([500])
hyperparameters_range_dictionary["use_bias"] = Categorical([True, False])
hyperparameters_range_dictionary["batch_size"] = Categorical([1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024])
hyperparameters_range_dictionary["num_factors"] = Integer(1, 200)
hyperparameters_range_dictionary["item_reg"] = Real(low = 1e-5, high = 1e-2, prior = 'log-uniform')
hyperparameters_range_dictionary["user_reg"] = Real(low = 1e-5, high = 1e-2, prior = 'log-uniform')
hyperparameters_range_dictionary["learning_rate"] = Real(low = 1e-4, high = 1e-1, prior = 'log-uniform')
hyperparameters_range_dictionary["negative_interactions_quota"] = Real(low = 0.0, high = 0.5, prior = 'uniform')
recommender_input_args = SearchInputRecommenderArgs(
CONSTRUCTOR_POSITIONAL_ARGS = [recommender_class, URM_train],
CONSTRUCTOR_KEYWORD_ARGS = {},
FIT_POSITIONAL_ARGS = [],
FIT_KEYWORD_ARGS = earlystopping_keywargs
)
##########################################################################################################
if recommender_class is MatrixFactorization_AsySVD_Cython:
hyperparameters_range_dictionary = {}
hyperparameters_range_dictionary["sgd_mode"] = Categorical(["sgd", "adagrad", "adam"])
hyperparameters_range_dictionary["epochs"] = Categorical([500])
hyperparameters_range_dictionary["use_bias"] = Categorical([True, False])
hyperparameters_range_dictionary["batch_size"] = Categorical([1])
hyperparameters_range_dictionary["num_factors"] = Integer(1, 200)
hyperparameters_range_dictionary["item_reg"] = Real(low = 1e-5, high = 1e-2, prior = 'log-uniform')
hyperparameters_range_dictionary["user_reg"] = Real(low = 1e-5, high = 1e-2, prior = 'log-uniform')
hyperparameters_range_dictionary["learning_rate"] = Real(low = 1e-4, high = 1e-1, prior = 'log-uniform')
hyperparameters_range_dictionary["negative_interactions_quota"] = Real(low = 0.0, high = 0.5, prior = 'uniform')
recommender_input_args = SearchInputRecommenderArgs(
CONSTRUCTOR_POSITIONAL_ARGS = [recommender_class, URM_train],
CONSTRUCTOR_KEYWORD_ARGS = {},
FIT_POSITIONAL_ARGS = [],
FIT_KEYWORD_ARGS = earlystopping_keywargs
)
##########################################################################################################
if recommender_class is MatrixFactorization_BPR_Cython:
hyperparameters_range_dictionary = {}
hyperparameters_range_dictionary["sgd_mode"] = Categorical(["sgd", "adagrad", "adam"])
hyperparameters_range_dictionary["epochs"] = Categorical([1500])
hyperparameters_range_dictionary["num_factors"] = Integer(1, 200)
hyperparameters_range_dictionary["batch_size"] = Categorical([1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024])
hyperparameters_range_dictionary["positive_reg"] = Real(low = 1e-5, high = 1e-2, prior = 'log-uniform')
hyperparameters_range_dictionary["negative_reg"] = Real(low = 1e-5, high = 1e-2, prior = 'log-uniform')
hyperparameters_range_dictionary["learning_rate"] = Real(low = 1e-4, high = 1e-1, prior = 'log-uniform')
recommender_input_args = SearchInputRecommenderArgs(
CONSTRUCTOR_POSITIONAL_ARGS = [recommender_class, URM_train],
CONSTRUCTOR_KEYWORD_ARGS = {},
FIT_POSITIONAL_ARGS = [],
FIT_KEYWORD_ARGS = {**earlystopping_keywargs,
"positive_threshold_BPR": None}
)
##########################################################################################################
if recommender_class is IALSRecommender:
hyperparameters_range_dictionary = {}
hyperparameters_range_dictionary["num_factors"] = Integer(1, 200)
hyperparameters_range_dictionary["confidence_scaling"] = Categorical(["linear", "log"])
hyperparameters_range_dictionary["alpha"] = Real(low = 1e-3, high = 50.0, prior = 'log-uniform')
hyperparameters_range_dictionary["epsilon"] = Real(low = 1e-3, high = 10.0, prior = 'log-uniform')
hyperparameters_range_dictionary["reg"] = Real(low = 1e-5, high = 1e-2, prior = 'log-uniform')
recommender_input_args = SearchInputRecommenderArgs(
CONSTRUCTOR_POSITIONAL_ARGS = [recommender_class, URM_train],
CONSTRUCTOR_KEYWORD_ARGS = {},
FIT_POSITIONAL_ARGS = [],
FIT_KEYWORD_ARGS = earlystopping_keywargs
)
##########################################################################################################
if recommender_class is PureSVDRecommender:
hyperparameters_range_dictionary = {}
hyperparameters_range_dictionary["num_factors"] = Integer(1, 350)
recommender_input_args = SearchInputRecommenderArgs(
CONSTRUCTOR_POSITIONAL_ARGS = [recommender_class, URM_train],
CONSTRUCTOR_KEYWORD_ARGS = {},
FIT_POSITIONAL_ARGS = [],
FIT_KEYWORD_ARGS = {}
)
##########################################################################################################
if recommender_class is NMFRecommender:
hyperparameters_range_dictionary = {}
hyperparameters_range_dictionary["num_factors"] = Integer(1, 350)
hyperparameters_range_dictionary["solver"] = Categorical(["coordinate_descent", "multiplicative_update"])
hyperparameters_range_dictionary["init_type"] = Categorical(["random", "nndsvda"])
hyperparameters_range_dictionary["beta_loss"] = Categorical(["frobenius", "kullback-leibler"])
recommender_input_args = SearchInputRecommenderArgs(
CONSTRUCTOR_POSITIONAL_ARGS = [recommender_class, URM_train],
CONSTRUCTOR_KEYWORD_ARGS = {},
FIT_POSITIONAL_ARGS = [],
FIT_KEYWORD_ARGS = {}
)
#########################################################################################################
if URM_train_last_test is not None:
recommender_input_args_last_test = recommender_input_args.copy()
recommender_input_args_last_test.CONSTRUCTOR_POSITIONAL_ARGS[1] = URM_train_last_test
else:
recommender_input_args_last_test = None
parameterSearch = SearchBayesianSkopt(MF_cold_user_wrapper, evaluator_validation=evaluator_validation, evaluator_test=evaluator_test)
hyperparameters_range_dictionary["estimate_model_for_cold_users"] = Categorical(["itemKNN", "mean_item_factors"])
hyperparameters_range_dictionary["estimate_model_for_cold_users_topK"] = Integer(5, 1000)
## Final step, after the hyperparameter range has been defined for each type of algorithm
parameterSearch.search(recommender_input_args,
parameter_search_space = hyperparameters_range_dictionary,
n_cases = n_cases,
n_random_starts = n_random_starts,
output_folder_path = output_folder_path,
output_file_name_root = output_file_name_root,
metric_to_optimize = metric_to_optimize,
resume_from_saved = resume_from_saved,
recommender_input_args_last_test = recommender_input_args_last_test)
except Exception as e:
print("On recommender {} Exception {}".format(recommender_class, str(e)))
traceback.print_exc()
error_file = open(output_folder_path + "ErrorLog.txt", "a")
error_file.write("On recommender {} Exception {}\n".format(recommender_class, str(e)))
error_file.close()
def read_data_split_and_search(dataset_name,
flag_baselines_tune = False,
flag_DL_article_default = False, flag_MF_baselines_tune = False, flag_DL_tune = False,
flag_print_results = False):
from Conferences.WWW.MultiVAE_our_interface.Movielens20M.Movielens20MReader import Movielens20MReader
from Conferences.WWW.MultiVAE_our_interface.NetflixPrize.NetflixPrizeReader import NetflixPrizeReader
split_type = "cold_user"
result_folder_path = "result_experiments/{}/{}_{}_{}/".format(CONFERENCE_NAME, ALGORITHM_NAME, dataset_name, split_type)
if dataset_name == "movielens20m":
dataset = Movielens20MReader(result_folder_path, split_type = split_type)
elif dataset_name == "netflixPrize":
dataset = NetflixPrizeReader(result_folder_path)
# If directory does not exist, create
if not os.path.exists(result_folder_path):
os.makedirs(result_folder_path)
metric_to_optimize = "NDCG"
n_cases = 50
n_random_starts = 15
if split_type == "cold_user":
collaborative_algorithm_list = [
Random,
TopPop,
# UserKNNCFRecommender,
ItemKNNCFRecommender,
P3alphaRecommender,
RP3betaRecommender,
# PureSVDRecommender,
# IALSRecommender,
# NMFRecommender,
# MatrixFactorization_BPR_Cython,
# MatrixFactorization_FunkSVD_Cython,
EASE_R_Recommender,
SLIM_BPR_Cython,
SLIMElasticNetRecommender,
]
URM_train = dataset.URM_DICT["URM_train"].copy()
URM_train_all = dataset.URM_DICT["URM_train_all"].copy()
URM_validation = dataset.URM_DICT["URM_validation"].copy()
URM_test = dataset.URM_DICT["URM_test"].copy()
# Ensure IMPLICIT data and DISJOINT sets
assert_implicit_data([URM_train, URM_train_all, URM_validation, URM_test])
assert_disjoint_matrices([URM_train, URM_validation, URM_test])
assert_disjoint_matrices([URM_train_all, URM_validation, URM_test])
from Base.Evaluation.Evaluator import EvaluatorHoldout
evaluator_validation = EvaluatorHoldout(URM_validation, cutoff_list=[100])
evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[20, 50, 100])
evaluator_validation = EvaluatorUserSubsetWrapper(evaluator_validation, URM_train_all)
evaluator_test = EvaluatorUserSubsetWrapper(evaluator_test, URM_train_all)
runParameterSearch_Collaborative_partial = partial(runParameterSearch_Collaborative,
URM_train = URM_train,
URM_train_last_test = URM_train + URM_validation,
metric_to_optimize = metric_to_optimize,
evaluator_validation_earlystopping = evaluator_validation,
evaluator_validation = evaluator_validation,
evaluator_test = evaluator_test,
output_folder_path = result_folder_path,
parallelizeKNN = False,
allow_weighting = True,
resume_from_saved = True,
n_cases = n_cases,
n_random_starts = n_random_starts)
if flag_baselines_tune:
for recommender_class in collaborative_algorithm_list:
try:
runParameterSearch_Collaborative_partial(recommender_class)
except Exception as e:
print("On recommender {} Exception {}".format(recommender_class, str(e)))
traceback.print_exc()
################################################################################################
###### Matrix Factorization Cold users
collaborative_MF_algorithm_list = [
PureSVDRecommender,
IALSRecommender,
NMFRecommender,
MatrixFactorization_BPR_Cython,
MatrixFactorization_FunkSVD_Cython,
]
runParameterSearch_cold_user_MF_partial = partial(runParameterSearch_cold_user_MF,
URM_train = URM_train,
URM_train_last_test = URM_train + URM_validation,
metric_to_optimize = metric_to_optimize,
evaluator_validation_earlystopping = evaluator_validation,
evaluator_validation = evaluator_validation,
evaluator_test = evaluator_test,
output_folder_path = result_folder_path,
resume_from_saved = True,
n_cases = n_cases,
n_random_starts = n_random_starts)
if flag_MF_baselines_tune:
for recommender_class in collaborative_MF_algorithm_list:
try:
runParameterSearch_cold_user_MF_partial(recommender_class)
except Exception as e:
print("On recommender {} Exception {}".format(recommender_class, str(e)))
traceback.print_exc()
################################################################################################
######
###### DL ALGORITHM
######
if flag_DL_article_default:
try:
if dataset_name == "movielens20m":
epochs = 100
elif dataset_name == "netflixPrize":
epochs = 200
multiVAE_article_hyperparameters = {
"epochs": epochs,
"batch_size": 500,
"total_anneal_steps": 200000,
"p_dims": None,
}
multiVAE_earlystopping_hyperparameters = {
"validation_every_n": 5,
"stop_on_validation": True,
"evaluator_object": evaluator_validation,
"lower_validations_allowed": 5,
"validation_metric": metric_to_optimize,
}
parameterSearch = SearchSingleCase(Mult_VAE_RecommenderWrapper,
evaluator_validation=evaluator_validation,
evaluator_test=evaluator_test)
recommender_input_args = SearchInputRecommenderArgs(
CONSTRUCTOR_POSITIONAL_ARGS = [URM_train],
FIT_KEYWORD_ARGS = multiVAE_earlystopping_hyperparameters)
recommender_input_args_last_test = recommender_input_args.copy()
recommender_input_args_last_test.CONSTRUCTOR_POSITIONAL_ARGS[0] = URM_train + URM_validation
parameterSearch.search(recommender_input_args,
recommender_input_args_last_test = recommender_input_args_last_test,
fit_hyperparameters_values=multiVAE_article_hyperparameters,
output_folder_path = result_folder_path,
resume_from_saved = True,
output_file_name_root = Mult_VAE_RecommenderWrapper.RECOMMENDER_NAME)
except Exception as e:
print("On recommender {} Exception {}".format(Mult_VAE_RecommenderWrapper, str(e)))
traceback.print_exc()
################################################################################################
######
###### PRINT RESULTS
######
if flag_print_results:
n_test_users = np.sum(np.ediff1d(URM_test.indptr)>=1)
file_name = "{}..//{}_{}_".format(result_folder_path, ALGORITHM_NAME, dataset_name)
result_loader = ResultFolderLoader(result_folder_path,
base_algorithm_list = None,
other_algorithm_list = [Mult_VAE_RecommenderWrapper],
KNN_similarity_list = KNN_similarity_to_report_list,
ICM_names_list = None,
UCM_names_list = None)
result_loader.generate_latex_results(file_name + "{}_latex_results.txt".format("article_metrics"),
metrics_list = ["RECALL", "NDCG"],
cutoffs_list = [20, 50, 100],
table_title = None,
highlight_best = True)
result_loader.generate_latex_results(file_name + "{}_latex_results.txt".format("all_metrics"),
metrics_list = ["PRECISION", "RECALL", "MAP_MIN_DEN", "MRR", "NDCG", "F1", "HIT_RATE", "ARHR_ALL_HITS",
"NOVELTY", "DIVERSITY_MEAN_INTER_LIST", "DIVERSITY_HERFINDAHL", "COVERAGE_ITEM", "DIVERSITY_GINI", "SHANNON_ENTROPY"],
cutoffs_list = [50],
table_title = None,
highlight_best = True)
result_loader.generate_latex_time_statistics(file_name + "{}_latex_results.txt".format("time"),
n_evaluation_users=n_test_users,
table_title = None)
from functools import partial
if __name__ == '__main__':
ALGORITHM_NAME = "Mult_VAE"
CONFERENCE_NAME = "WWW"
parser = argparse.ArgumentParser()
parser.add_argument('-b', '--baseline_tune', help="Baseline hyperparameter search", type = bool, default = False)
parser.add_argument('-a', '--DL_article_default', help="Train the DL model with article hyperparameters", type = bool, default = False)
parser.add_argument('-p', '--print_results', help="Print results", type = bool, default = True)
parser.add_argument('-m', '--MF_baseline_tune', help="Matrix Factorization hyperparameter search", type = bool, default = False)
input_flags = parser.parse_args()
print(input_flags)
KNN_similarity_to_report_list = ["cosine", "dice", "jaccard", "asymmetric", "tversky"]
dataset_list = ["movielens20m", "netflixPrize"]
for dataset_name in dataset_list:
read_data_split_and_search(dataset_name,
flag_baselines_tune=input_flags.baseline_tune,
flag_MF_baselines_tune = input_flags.MF_baseline_tune,
flag_DL_article_default= input_flags.DL_article_default,
flag_print_results = input_flags.print_results,
)
if input_flags.print_results:
generate_latex_hyperparameters(result_folder_path ="result_experiments/{}/".format(CONFERENCE_NAME),
algorithm_name= ALGORITHM_NAME,
experiment_subfolder_list = ["{}_cold_user".format(dataset) for dataset in dataset_list],
other_algorithm_list = [Mult_VAE_RecommenderWrapper],
KNN_similarity_to_report_list = KNN_similarity_to_report_list,
split_per_algorithm_type = True)