Comparing matrix factorization with transformers for MovieLens recommendations using PyTorch-accelerated

Chris Hughes4 Jan 2022

Problem

A team wants to predict how a user will rate an item. The usual choice is matrix factorization, which treats every rating as independent of the ratings around it and so ignores order: a user who rated the first two films of a trilogy highly will probably like the third. A fair comparison with a sequence model also needs a split that never shows the model later ratings.

Example from the article: on MovieLens-1M (one million ratings from 6,000 users on 4,000 movies), a transformer reading the last ten ratings lowers mean absolute error (MAE) from 0.890 to 0.726.

Solution in brief

Hold out each user's last rating for validation, so every prediction uses only earlier history. Train matrix factorization with user and movie biases as the reference. Then pass a sliding window of ten movie IDs per user through a transformer encoder and predict the last movie's rating with a small fully connected network.

Questions this case answers
  • How to predict a user's response to an item from the ordered history of their past interactions and compare it fairly with a model that ignores order
  • How to build an evaluation split for sequential data so that no future interaction informs a prediction about the past
  • How to predict the rating a user will give from the sequence of items they rated before?
  • How to split rating data into train and validation without leaking the future into the past?

Tools and methods

  • Matrix factorization with biases — user and movie vectors plus a bias for each; the reference model.
  • Transformer encoder over rating sequences — mixes the embeddings of recently rated movies with self-attention.
  • Leave-one-out split by time — each user's last chronological rating is the validation row.
  • Median-rating baseline — one constant prediction.
  • PyTorch, pandas, scikit-learn — models, data preparation and metrics; training uses the author's PyTorch-accelerated library.
Continue reading — it's free

Google or a one-time email code. No password.