GithubHelp home page GithubHelp logo

hulalazz / explicit-vs-implicit Goto Github PK

View Code? Open in Web Editor NEW

This project forked from maciejkula/explicit-vs-implicit

0.0 1.0 0.0 3 KB

An experiment on explicit vs implicit feedback recommenders

License: MIT License

Jupyter Notebook 100.00%

explicit-vs-implicit's Introduction

Explicit vs implicit feedback models

This short notebook is meant to illustrate the importance of modelling implicit feedback in recommender systems. It reproduces the results of Harald Steck's seminal paper, Training and Testing of Recommender Systems on Data Missing Not at Random.

Its main thrust is this: it is never appropriate the evaluate recommender system on observed ratings only, and when one evaluates a system based on constructing a ranking over all items, modelling implicit feedback is crucial.

For this experiment we're going to use Spotlight to:

  1. Fit an explicit recommender system based on observed ratings only.
  2. Fit an implicit recommender system based on what ratings were and were not observed.
  3. Compare their performance in ranking all items using the Mean Reciprocal Rank, (MRR) metric.

Let's import Spotlight first.

import numpy as np

from spotlight.cross_validation import random_train_test_split
from spotlight.datasets.movielens import get_movielens_dataset
from spotlight.factorization import explicit, implicit
from spotlight.evaluation import mrr_score, rmse_score

Set some hyperparameters, get the dataset and split it into test and train.

RANDOM_SEED = 42
LATENT_DIM = 32
NUM_EPOCHS = 10
BATCH_SIZE = 256
L2 = 1e-6
LEARNING_RATE = 1e-3

dataset = get_movielens_dataset('100K')
train, test = random_train_test_split(dataset, random_state=np.random.RandomState(RANDOM_SEED))

Create two models: and explicit feedback model trained to minimize the squared difference of true and predicted ratings on observed ratings only, and an implicit model whose goal is to rank all watched items over all items that weren't watched.

explicit_model = explicit.ExplicitFactorizationModel(loss='regression',
                                                     embedding_dim=LATENT_DIM,
                                                     n_iter=NUM_EPOCHS,
                                                     learning_rate=LEARNING_RATE,
                                                     batch_size=BATCH_SIZE,
                                                     l2=L2,
                                                     random_state=np.random.RandomState(RANDOM_SEED))
implicit_model = implicit.ImplicitFactorizationModel(loss='bpr',
                                                     embedding_dim=LATENT_DIM,
                                                     n_iter=NUM_EPOCHS,
                                                     learning_rate=LEARNING_RATE,
                                                     batch_size=BATCH_SIZE,
                                                     l2=L2,
                                                     random_state=np.random.RandomState(RANDOM_SEED))

Fit the models (shouldn't take more than 30 seconds).

explicit_model.fit(train)
implicit_model.fit(train)

Just to make sure that the explicit model is of decent quality, we compute its RMSE score on the test set. Anything below 1.0 is a reasonable model.

print('Explicit RMSE: {:.2f}.'.format(rmse_score(explicit_model, test).mean()))
Explicit RMSE: 0.94.

How good are the two models when trying to rank all items? A perfect score would rank all seen items in the test set over all unseen items (we exclude seen items from the training set from the evaluation), and return a score of 1.0.

print('Explicit MRR: {:.2f}'.format(mrr_score(explicit_model, test, train=train).mean()))
Explicit MRR: 0.02
print('Implicit MRR: {:.2f}'.format(mrr_score(implicit_model, test, train=train).mean()))
Implicit MRR: 0.07

The explicit model is awful: the two models are not even close. You should never use pure explicit feedback models.

explicit-vs-implicit's People

Contributors

maciejkula avatar

Watchers

Zhang JinXiong(张金雄) avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.