-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_test_recommenders.py
172 lines (103 loc) · 5.39 KB
/
run_test_recommenders.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 22/11/2018
@author: Maurizio Ferrari Dacrema
"""
import traceback, os, shutil
from Evaluation.Evaluator import EvaluatorHoldout, EvaluatorNegativeItemSample
from Data_manager.Movielens.Movielens1MReader import Movielens1MReader
from Data_manager.DataSplitter_leave_k_out import DataSplitter_leave_k_out
from Recommenders.Incremental_Training_Early_Stopping import Incremental_Training_Early_Stopping
def write_log_string(log_file, string):
log_file.write(string)
log_file.flush()
def _get_recommender_instance(recommender_class, URM_train, ICM_train):
if recommender_class is ItemKNNCBFRecommender:
recommender_object = recommender_class(URM_train, ICM_train)
else:
recommender_object = recommender_class(URM_train)
return recommender_object
def run_recommender(recommender_class):
temp_save_file_folder = "./result_experiments/__temp_model/"
if not os.path.isdir(temp_save_file_folder):
os.makedirs(temp_save_file_folder)
try:
dataset_object = Movielens1MReader()
dataSplitter = DataSplitter_leave_k_out(dataset_object, k_out_value=2)
dataSplitter.load_data(save_folder_path= output_folder_path + dataset_object._get_dataset_name() + "_data/")
URM_train, URM_validation, URM_test = dataSplitter.get_holdout_split()
ICM_name = dataSplitter.get_all_available_ICM_names()[0]
ICM_train = dataSplitter.get_ICM_from_name(ICM_name)
write_log_string(log_file, "On Recommender {}\n".format(recommender_class))
recommender_object = _get_recommender_instance(recommender_class, URM_train, ICM_train)
if isinstance(recommender_object, Incremental_Training_Early_Stopping):
fit_params = {"epochs": 15}
else:
fit_params = {}
recommender_object.fit(**fit_params)
write_log_string(log_file, "Fit OK, ")
evaluator = EvaluatorHoldout(URM_test, [5], exclude_seen=True)
_, results_run_string = evaluator.evaluateRecommender(recommender_object)
write_log_string(log_file, "EvaluatorHoldout OK, ")
evaluator = EvaluatorNegativeItemSample(URM_test, URM_train, [5], exclude_seen=True)
_, _ = evaluator.evaluateRecommender(recommender_object)
write_log_string(log_file, "EvaluatorNegativeItemSample OK, ")
recommender_object.save_model(temp_save_file_folder, file_name="temp_model")
write_log_string(log_file, "save_model OK, ")
recommender_object = _get_recommender_instance(recommender_class, URM_train, ICM_train)
recommender_object.load_model(temp_save_file_folder, file_name= "temp_model")
evaluator = EvaluatorHoldout(URM_test, [5], exclude_seen=True)
_, results_run_string_2 = evaluator.evaluateRecommender(recommender_object)
write_log_string(log_file, "load_model OK, ")
shutil.rmtree(temp_save_file_folder, ignore_errors=True)
write_log_string(log_file, " PASS\n")
write_log_string(log_file, results_run_string + "\n\n")
except Exception as e:
print("On Recommender {} Exception {}".format(recommender_class, str(e)))
log_file.write("On Recommender {} Exception {}\n\n\n".format(recommender_class, str(e)))
log_file.flush()
traceback.print_exc()
import multiprocessing
from Recommenders.NonPersonalizedRecommender import Random, TopPop, GlobalEffects
from Recommenders.GraphBased.P3alphaRecommender import P3alphaRecommender
from Recommenders.GraphBased.RP3betaRecommender import RP3betaRecommender
from Recommenders.KNN.ItemKNNCFRecommender import ItemKNNCFRecommender
from Recommenders.KNN.UserKNNCFRecommender import UserKNNCFRecommender
from Recommenders.MatrixFactorization.PureSVDRecommender import PureSVDRecommender
from Recommenders.MatrixFactorization.IALSRecommender import IALSRecommender
from Recommenders.MatrixFactorization.Cython.MatrixFactorization_Cython import MatrixFactorization_BPR_Cython, MatrixFactorization_FunkSVD_Cython, MatrixFactorization_AsySVD_Cython
from Recommenders.SLIM.Cython.SLIM_BPR_Cython import SLIM_BPR_Cython
from Recommenders.SLIM.SLIMElasticNetRecommender import SLIMElasticNetRecommender
from Recommenders.EASE_R.EASE_R_Recommender import EASE_R_Recommender
from Recommenders.KNN.ItemKNNCBFRecommender import ItemKNNCBFRecommender
if __name__ == '__main__':
output_folder_path = "./result_experiments/rec_test/"
log_file_name = "run_test_recommender.txt"
recommender_list = [
Random,
TopPop,
GlobalEffects,
UserKNNCFRecommender,
ItemKNNCFRecommender,
ItemKNNCBFRecommender,
P3alphaRecommender,
RP3betaRecommender,
SLIM_BPR_Cython,
SLIMElasticNetRecommender,
MatrixFactorization_BPR_Cython,
MatrixFactorization_FunkSVD_Cython,
MatrixFactorization_AsySVD_Cython,
PureSVDRecommender,
IALSRecommender,
EASE_R_Recommender,
]
# If directory does not exist, create
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
log_file = open(output_folder_path + log_file_name, "w")
for recommender_class in recommender_list:
run_recommender(recommender_class)
#
# pool = multiprocessing.Pool(processes=int(multiprocessing.cpu_count()), maxtasksperchild=1)
# resultList = pool.map(run_dataset, dataset_list)