-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_recommenders_on_dataset.py
105 lines (77 loc) · 4.13 KB
/
run_recommenders_on_dataset.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
from Recommenders.Recommender_import_list import *
from Data_manager.Movielens.Movielens1MReader import Movielens1MReader
from Data_manager.DataSplitter_leave_k_out import DataSplitter_leave_k_out
from Data_manager.HMDatasetReader import HMDatasetReader
from Recommenders.Incremental_Training_Early_Stopping import Incremental_Training_Early_Stopping
from Recommenders.BaseCBFRecommender import BaseItemCBFRecommender, BaseUserCBFRecommender
from Evaluation.Evaluator import EvaluatorHoldout
import traceback, os
dataset_name = "hm-exponential-decay20-Validation_salesWeek-Train_3months"
"""
Name of the folder inside processed where the dataset was saved with Dataset.save_data()
"""
def _get_instance(recommender_class, URM_train, ICM_all, UCM_all):
if issubclass(recommender_class, BaseItemCBFRecommender):
recommender_object = recommender_class(URM_train, ICM_all)
elif issubclass(recommender_class, BaseUserCBFRecommender):
recommender_object = recommender_class(URM_train, UCM_all)
else:
recommender_object = recommender_class(URM_train)
return recommender_object
if __name__ == '__main__':
reader = HMDatasetReader(False)
PROCESSED_PATH = os.getenv('PROCESSED_PATH')
dataset_object = reader.load_data('{}/{}/'.format("processed", dataset_name))
print("Loaded dataset into memory...")
# Here all URMs and ICMs must be loaded, if no URM_all is present an error will occur in Dataset library
URM_train = dataset_object.get_URM_from_name('URM_train')
URM_test = dataset_object.get_URM_from_name('URM_validation') # Temporary solution, this should use cross-validation
ICM_all = []
UCM_all = []
print(URM_train.shape)
print(URM_test.shape)
dataset_object.print_statistics_global()
recommender_class_list = [
explicit_TopPop
]
evaluator = EvaluatorHoldout(URM_test, [5, 12], exclude_seen=True)
# from MatrixFactorization.PyTorch.MF_MSE_PyTorch import MF_MSE_PyTorch
earlystopping_keywargs = {"validation_every_n": 5,
"stop_on_validation": True,
"evaluator_object": EvaluatorHoldout(URM_test, [20], exclude_seen=True),
"lower_validations_allowed": 5,
"validation_metric": "MAP",
}
output_root_path = "./result_experiments/"
# If directory does not exist, create
if not os.path.exists(output_root_path):
os.makedirs(output_root_path)
logFile = open(output_root_path + "result_all_algorithms.txt", "a")
for recommender_class in recommender_class_list:
try:
print("Algorithm: {}".format(recommender_class))
recommender_object = _get_instance(recommender_class, URM_train, ICM_all, UCM_all)
if isinstance(recommender_object, Incremental_Training_Early_Stopping):
fit_params = {"epochs": 15, **earlystopping_keywargs}
else:
fit_params = {}
recommender_object.fit(**fit_params)
results_run_1, results_run_string_1 = evaluator.evaluateRecommender(recommender_object)
# recommender_object.save_model(output_root_path, file_name="temp_model.zip")
#
# recommender_object = _get_instance(recommender_class, URM_train, ICM_all, UCM_all)
# recommender_object.load_model(output_root_path, file_name="temp_model.zip")
#
# os.remove(output_root_path + "temp_model.zip")
#
# results_run_2, results_run_string_2 = evaluator.evaluateRecommender(recommender_object)
#
# if recommender_class not in [Random]:
# assert results_run_1.equals(results_run_2)
print("Algorithm: {}, results: \n{}".format(recommender_class, results_run_string_1))
logFile.write("Algorithm: {}, results: \n{}\n".format(recommender_class, results_run_string_1))
logFile.flush()
except Exception as e:
traceback.print_exc()
logFile.write("Algorithm: {} - Exception: {}\n".format(recommender_class, str(e)))
logFile.flush()