class HybridLinear10Recommneder(BaseItemSimilarityMatrixRecommender): RECOMMENDER_NAME = "HybridLinear10Recommender" def __init__(self, URM_train, seed: int): super(HybridLinear10Recommneder, self).__init__(URM_train) self.slimBPR = SLIM_BPR_Cython(URM_train) self.userKnnCF = UserKNNCFRecommender(URM_train) #self.itemcf = ItemKNNCFRecommender(urm) def fit(self, alpha=1): self.slimBPR.fit(epochs=135, topK=933, symmetric=False, sgd_mode='adagrad', lambda_i=1.054e-05, lambda_j=1.044e-05, learning_rate=0.00029) self.userKnnCF.fit(topK=201, shrink=998, similarity='cosine', normalize=True, feature_weighting='TF-IDF') self.alpha = alpha self.beta = 1 - alpha #self.gamma = alpha_gamma_ratio def _compute_item_score(self, user_id_array, items_to_compute=None): # ATTENTION! # THIS METHOD WORKS ONLY IF user_id_array IS A SCALAR AND NOT AN ARRAY # TODO scores_slimBPR = self.slimBPR._compute_item_score( user_id_array=user_id_array) scores_userKnnCF = self.userKnnCF._compute_item_score( user_id_array=user_id_array) # normalization #slim_max = scores_slim.max() #rp3_max = scores_rp3.max() #itemcf_max = scores_itemcf.max() #if not slim_max == 0: # scores_slim /= slim_max #if not rp3_max == 0: # scores_rp3 /= rp3_max #if not itemcf_max == 0: # scores_itemcf /= itemcf_max scores_total = self.alpha * scores_slimBPR + self.beta * scores_userKnnCF #+ self.gamma * scores_itemcf return scores_total def save_model(self, folder_path, file_name=None): if file_name is None: file_name = self.RECOMMENDER_NAME self._print("Saving model in file '{}'".format(folder_path + file_name)) dataIO = DataIO(folder_path=folder_path) dataIO.save_data(file_name=file_name, data_dict_to_save={}) self._print("Saving complete")
class LinearHybrid006(BaseItemSimilarityMatrixRecommender): RECOMMENDER_NAME = "LinearHybrid006" # set the seed equal to the one of the parameter search!!!! def __init__(self, URM_train, ICM_train, submission=False, verbose=True, seed=1205): super(LinearHybrid006, self).__init__(URM_train, verbose=verbose) self.URM_train = URM_train self.ICM_train = ICM_train self.__rec1 = P3alphaRecommender(URM_train, verbose=False) self.__rec1_params = { 'topK': 131, 'alpha': 0.33660811631883863, 'normalize_similarity': False } self.__rec2 = ItemKNNCBFRecommender(URM_train, ICM_train, verbose=False) self.__rec2_params = { 'topK': 205, 'shrink': 1000, 'similarity': 'cosine', 'normalize': True, 'feature_weighting': 'BM25' } self.__rec3 = SLIM_BPR_Cython(URM_train, verbose=False) self.__rec3_params = { 'topK': 979, 'epochs': 130, 'symmetric': False, 'sgd_mode': 'adam', 'lambda_i': 0.004947329669424629, 'lambda_j': 1.1534760845071758e-05, 'learning_rate': 0.0001 } self.__a = self.__b = self.__c = None self.seed = seed self.__submission = submission def fit(self, alpha=0.5, l1_ratio=0.5): self.__a = alpha * l1_ratio self.__b = alpha - self.__a self.__c = 1 - self.__a - self.__b if not self.__submission: try: self.__rec1.load_model( f'stored_recommenders/seed_{str(self.seed)}_{self.__rec1.RECOMMENDER_NAME}/', f'best_for_{self.RECOMMENDER_NAME}') print(f"{self.__rec1.RECOMMENDER_NAME} loaded.") except: print(f"Fitting {self.__rec1.RECOMMENDER_NAME} ...") self.__rec1.fit(**self.__rec1_params) print(f"done.") self.__rec1.save_model( f'stored_recommenders/seed_{str(self.seed)}_{self.__rec1.RECOMMENDER_NAME}/', f'best_for_{self.RECOMMENDER_NAME}') try: self.__rec2.load_model( f'stored_recommenders/seed_{str(self.seed)}_{self.__rec2.RECOMMENDER_NAME}/', f'best_for_{self.RECOMMENDER_NAME}') print(f"{self.__rec2.RECOMMENDER_NAME} loaded.") except: print(f"Fitting {self.__rec2.RECOMMENDER_NAME} ...") self.__rec2.fit(**self.__rec2_params) print(f"done.") self.__rec2.save_model( f'stored_recommenders/seed_{str(self.seed)}_{self.__rec2.RECOMMENDER_NAME}/', f'best_for_{self.RECOMMENDER_NAME}') try: self.__rec3.load_model( f'stored_recommenders/seed_{str(self.seed)}_{self.__rec3.RECOMMENDER_NAME}/', f'best_for_{self.RECOMMENDER_NAME}') print(f"{self.__rec3.RECOMMENDER_NAME} loaded.") except: print(f"Fitting {self.__rec3.RECOMMENDER_NAME} ...") self.__rec3.fit(**self.__rec3_params) print(f"done.") self.__rec3.save_model( f'stored_recommenders/seed_{str(self.seed)}_{self.__rec3.RECOMMENDER_NAME}/', f'best_for_{self.RECOMMENDER_NAME}') else: self.__rec1.fit(**self.__rec1_params) self.__rec2.fit(**self.__rec2_params) self.__rec3.fit(**self.__rec3_params) def _compute_item_score(self, user_id_array, items_to_compute=None): item_weights_1 = self.__rec1._compute_item_score(user_id_array) item_weights_2 = self.__rec2._compute_item_score(user_id_array) item_weights_3 = self.__rec3._compute_item_score(user_id_array) item_weights = item_weights_1 * self.__a + item_weights_2 * self.__b + item_weights_3 * self.__c return item_weights def save_model(self, folder_path, file_name=None): if file_name is None: file_name = self.RECOMMENDER_NAME self._print("Saving model in file '{}'".format(folder_path + file_name)) dataIO = DataIO(folder_path=folder_path) dataIO.save_data(file_name=file_name, data_dict_to_save={}) self._print("Saving complete")