Esempio n. 1
0
    def test_save_and_load(self):
        recommender_class = NCRWrapper

        recommender_instance_original, URM_train, URM_validation, URM_test, URM_negative = get_data_and_rec_instance(
            recommender_class)
        n_users, n_items = URM_train.shape

        evaluator_test = EvaluatorNegativeItemSample(URM_test,
                                                     URM_negative, [50],
                                                     exclude_seen=True)

        folder_path = "./temp_folder/"
        file_name = "temp_file"

        if not os.path.exists(folder_path):
            os.makedirs(folder_path)

        recommender_instance_original.fit()
        recommender_instance_original.save_model(folder_path=folder_path,
                                                 file_name=file_name)

        results_run_original, _ = evaluator_test.evaluateRecommender(
            recommender_instance_original)

        recommender_instance_loaded = recommender_class(URM_train)
        recommender_instance_loaded.load_model(folder_path=folder_path,
                                               file_name=file_name)

        results_run_loaded, _ = evaluator_test.evaluateRecommender(
            recommender_instance_loaded)

        print("Result original: {}\n".format(results_run_original))
        print("Result loaded: {}\n".format(results_run_loaded))

        users_to_evaluate_mask = np.zeros(n_users, dtype=np.bool)
        rows = URM_test.indptr
        num_ratings = np.ediff1d(rows)
        new_mask = num_ratings > 0
        users_to_evaluate_mask = np.logical_or(users_to_evaluate_mask,
                                               new_mask)
        user_id_list = np.arange(n_users, dtype=np.int)[users_to_evaluate_mask]

        URM_items_to_rank = sps.csr_matrix(URM_test.copy().astype(
            np.bool)) + sps.csr_matrix(URM_negative.copy().astype(np.bool))
        URM_items_to_rank.eliminate_zeros()
        URM_items_to_rank.data = np.ones_like(URM_items_to_rank.data)

        for test_user in user_id_list:
            start_pos = URM_items_to_rank.indptr[test_user]
            end_pos = URM_items_to_rank.indptr[test_user + 1]
            items_to_compute = URM_items_to_rank.indices[start_pos:end_pos]

            item_scores_original = recommender_instance_original._compute_item_score(
                user_id_array=np.atleast_1d(test_user),
                items_to_compute=items_to_compute)

            item_scores_loaded = recommender_instance_loaded._compute_item_score(
                user_id_array=np.atleast_1d(test_user),
                items_to_compute=items_to_compute)

            self.assertTrue(
                np.allclose(item_scores_original, item_scores_loaded),
                "item_scores of the fitted model and of the loaded model are different"
            )

        shutil.rmtree(folder_path, ignore_errors=True)
Esempio n. 2
0
cutoff = 5

print_statistics(URM_train)

CMN_wrapper_train = CMN_RecommenderWrapper(URM_train)
user_KNNCF_Recommender = UserKNNCFRecommender(URM_train)
item_KNNCF_Recommender = ItemKNNCFRecommender(URM_train)
rp3_beta_Recommender = RP3betaRecommender(URM_train)

evaluator_negative_item_sample = EvaluatorNegativeItemSample(
    URM_test, URM_test_negative, cutoff_list=[5, 10])

CMN_wrapper_train.loadModel('result_experiments/SIGIR/CMN_pinterest/',
                            'CMN_RecommenderWrapper_best_model')
d, s = evaluator_negative_item_sample.evaluateRecommender(CMN_wrapper_train)
print('CMN_wrapper_train')
print(s)

user_KNNCF_Recommender.loadModel('result_experiments/SIGIR/CMN_pinterest/',
                                 'UserKNNCFRecommender_cosine_best_model')
d, s = evaluator_negative_item_sample.evaluateRecommender(
    user_KNNCF_Recommender)
print('user_KNNCF_Recommender')
print(s)

item_KNNCF_Recommender.loadModel('result_experiments/SIGIR/CMN_pinterest/',
                                 'ItemKNNCFRecommender_cosine_best_model')
d, s = evaluator_negative_item_sample.evaluateRecommender(
    item_KNNCF_Recommender)
print('item_KNNCF_Recommender')