Пример #1
0
    def get_model(self):
        """
        Catch model

        :return: The Model
        """
        return TensorCoFi.get_model_from_cache()
Пример #2
0
 def test_training(self):
     """
     [recommendation.models.TensorCoFi] Test train from database
     """
     try:
         TensorCoFi.train_from_db()
     except Exception:
         assert False, "Training is not working for jumping ids"
     TensorCoFi.load_to_cache()
     t = TensorCoFi.get_model_from_cache()
     for user in User.objects.all():
         if len(user.owned_items) > 2:
             assert isinstance(t.get_recommendation(user), np.ndarray), "Recommendation is not a numpy array"
         else:
             try:
                 t.get_recommendation(user)
             except KeyError:
                 pass
             else:
                 assert False, "User with less than 3 items give a static recommendation"
Пример #3
0
 def test_recommendation_with_testfm(self):
     """
     [recommendation.api.GetRecommendation] Test recommendation with testfm
     """
     data = np.array(zip(*map(lambda x: (x["user_id"]-1, x["item_id"]-1, 1.),
                              Inventory.objects.all().values("user_id", "item_id"))), dtype=np.float32)
     users, items = zip(*Inventory.objects.all().values_list("user_id", "item_id"))
     df = pd.DataFrame({"user": pd.Series(users), "item": pd.Series(items)}, dtype=np.float32)
     evaluator = Evaluator(use_multi_threading=False)
     tensor = TensorCoFi.get_model_from_cache()
     tfm_tensor = PyTensorCoFi()
     tfm_tensor.data_map = tensor.data_map
     tfm_tensor.users_size = lambda: tensor.users_size()
     tfm_tensor.items_size = lambda: tensor.items_size()
     tfm_tensor.get_score = lambda user, item: \
         np.dot(tfm_tensor.factors[0][tfm_tensor.data_map[tfm_tensor.get_user_column()][user]],
                tfm_tensor.factors[1][tfm_tensor.data_map[tfm_tensor.get_item_column()][item]].transpose())
     tfm_tensor.train(data.transpose())
     items = df.item.unique()
     t = evaluator.evaluate_model(tensor, df, all_items=items, non_relevant_count=100)
     tfm = evaluator.evaluate_model(tfm_tensor, df, all_items=items, non_relevant_count=100)
     assert abs(t[0] - tfm[0]) < 0.15, \
         "Difference between testfm implementation and frappe is to high (%f, %f)" % (t[0], tfm[0])