예제 #1
0
 def test_fit(self):
     """
     [recommendation.models.TensorCoFi] Test size of matrix after tensorCoFi fit
     """
     tf = TensorCoFi(n_users=len(self.df.user.unique()), n_items=len(self.df.item.unique()), n_factors=2)
     tf.fit(self.df)
     #item and user are row vectors
     #self.assertEqual(len(self.df.user.unique()), tf.factors[0].shape[0])
     self.assertEqual(len(self.df.item.unique()), tf.factors[1].shape[0])
예제 #2
0
파일: wsgi.py 프로젝트: mozilla/frappe
def load_rest():
    User.load_to_cache()

    # Load main models
    Popularity.load_to_cache()
    TensorCoFi.load_to_cache()
    if "recommendation.language" in settings.INSTALLED_APPS:
        from recommendation.language.models import Region
        Region.load_to_cache()
    if "recommendation.diversity" in settings.INSTALLED_APPS:
        from recommendation.diversity.models import ItemGenre, Genre
        Genre.load_to_cache()
        ItemGenre.load_to_cache()
예제 #3
0
 def setup_class(cls, *args, **kwargs):
     """
     Put elements in db
     """
     for app in ITEMS:
         Item.objects.create(**app)
     for u in USERS:
         user = User.objects.create(external_id=u["external_id"])
         for i in u["items"]:
             Inventory.objects.create(user=user, item=Item.get_item_by_external_id(i))
     TensorCoFi.train_from_db()
     Popularity.train_from_db()
     TensorCoFi.load_to_cache()
     Popularity.load_to_cache()
예제 #4
0
    def test_tensor_score_against_testfm(self):
        """
        [recommendation.models.TensorCoFi] Test tensorcofi scores with test.fm benchmark
        """
        evaluator = Evaluator()
        tc = TensorCoFi(n_users=len(self.df.user.unique()), n_items=len(self.df.item.unique()), n_factors=2)
        ptc = PyTensorCoFi()
        training, testing = testfm.split.holdoutByRandom(self.df, 0.9)

        items = training.item.unique()
        tc.fit(training)
        ptc.fit(training)
        tc_score = evaluator.evaluate_model(tc, testing, all_items=items)[0]
        ptc_score = evaluator.evaluate_model(ptc, testing, all_items=items)[0]
        assert abs(tc_score-ptc_score) < .15, \
            "TensorCoFi score is not close enough to testfm benchmark (%.3f != %.3f)" % (tc_score, ptc_score)
예제 #5
0
파일: core.py 프로젝트: anukat2015/frappe
    def get_model(self):
        """
        Catch model

        :return: The Model
        """
        return TensorCoFi.get_model_from_cache()
예제 #6
0
 def setup_class(cls, *args, **kwargs):
     """
     Put elements in db
     """
     path = resource_filename(recommendation.__name__, "/")
     fill.FillTool({"items": True, "--mozilla": True, "prod": True}).load()
     fill.FillTool({"users": True, "--mozilla": True, "<path>": path+"data/user"}).load()
     modelcrafter.main("train", "popularity")
     modelcrafter.main("train", "tensorcofi")
     # Load user and items
     Item.load_to_cache()
     User.load_to_cache()
     # Load main models
     Popularity.load_to_cache()
     TensorCoFi.load_to_cache()
     cls.client = Client()
예제 #7
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"
예제 #8
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])
예제 #9
0
파일: wsgi.py 프로젝트: anukat2015/frappe
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "recommendation.default_settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

from django.conf import settings
from recommendation.models import Item, User, TensorCoFi, Popularity
# Load user and items
Item.load_to_cache()
User.load_to_cache()

# Load main models
Popularity.load_to_cache()

TensorCoFi.load_to_cache()


if "recommendation.language" in settings.INSTALLED_APPS:
    from recommendation.language.models import Locale, Region
    Locale.load_to_cache()
    Region.load_to_cache()


#if "recommendation.simple_logging" in recommendation.settings.INSTALLED_APPS:
#    print("Loading logs to cache...")
#    from recommendation.simple_logging.models import LogEntry
#    LogEntry.load_to_cache()
#    print("done!")

if "recommendation.diversity" in settings.INSTALLED_APPS:
예제 #10
0
파일: wsgi.py 프로젝트: mozilla/frappe
                          "recommendation.default_settings")

from django.core.wsgi import get_wsgi_application

application = get_wsgi_application()

from django.conf import settings
from recommendation.models import Item, User, TensorCoFi, Popularity
# Load user and items
Item.load_to_cache()
User.load_to_cache()

# Load main models
Popularity.load_to_cache()

TensorCoFi.load_to_cache()

if "recommendation.language" in settings.INSTALLED_APPS:
    from recommendation.language.models import Locale, Region
    Locale.load_to_cache()
    Region.load_to_cache()

#if "recommendation.simple_logging" in recommendation.settings.INSTALLED_APPS:
#    print("Loading logs to cache...")
#    from recommendation.simple_logging.models import LogEntry
#    LogEntry.load_to_cache()
#    print("done!")

if "recommendation.diversity" in settings.INSTALLED_APPS:
    from recommendation.diversity.models import ItemGenre, Genre
    Genre.load_to_cache()