Factorization Machines
MF sees only users and items; linear regression sees any feature but only linearly. Factorization Machines do both: they accept arbitrary one-hot feature vectors (users, items, genres, context) and learn factorised pairwise interactions between every feature pair. Instead of a dense O(n²) weight matrix W, FM sets W ≈ VVᵀ — one latent vector per feature — unifying global effects, MF, hybrid, and context-aware recommenders in a single equation.
The big idea
Start with a linear model on one-hot features — global effects and per-feature biases. Add all pairwise feature interactions — but a full weight matrix is , far too large for sparse data. The FM insight: factorise , exactly as Chapter 8 factorised the URM. Each feature gets a latent vector and . Parameters drop from to — and the model now generalises across feature pairs, because every feature shares one latent vector in all its interactions.
01 · One feature vector per interaction
From rating matrix to feature matrix
FM does not work on the URM directly. It reshapes every (user, item, rating) triple into a single feature vector.
The FM input is a feature matrix where each row is one user–item interaction and each column is a binary feature. For a pure collaborative setting the columns are the one-hot encoding of both the users and the items:
The row for user rating item has exactly two 1’s — at position and at position — and zeros elsewhere. This is the same information as the URM, arranged differently: FM sees one row per observed rating, the URM one row per user. The target for row is the rating — so FM is a supervised regression on .
Why one-hot?
One-hot encoding makes every feature independent. FM sees “user=Bob” and “item=Interstellar” as separate columns and learns a latent vector for each. When we later add content or context features, they are simply more one-hot columns appended to the same row — no algorithm change. This is the key to FM’s generality: any categorical feature becomes a column.
02 · Constant + linear + interactions
The FM prediction equation
Three terms: a global bias, per-feature weights, and factorised pairwise feature interactions.
The three components:
- Constant — the global intercept; estimate only this and you get the average rating across all interactions. The same role as in global effects (Ch. 2).
- Linear — each feature gets a weight for its independent contribution. For collaborative one-hot input the only active features are user and item , so — exactly from Ch. 2. The linear term is global effects.
- Quadratic — pairwise interactions; the product only when both features are active. This is what lifts FM beyond linear regression: it can learn that “user=Bob” and “item=Interstellar” interact positively (Bob likes sci-fi) while “user=Dan” interacts negatively.
The interaction term is the secret sauce
Without it, FM is just global effects — it cannot personalise. The interaction between user and item features is what makes FM a collaborative model. And because the interactions are factorised (next section), FM generalises to unseen pairs, exactly as MF does.
03 · The core contribution
Factorising the interaction matrix
A full pairwise interaction matrix has O(n²) parameters. Factorising it reduces to O(nf) and enables generalisation.
In vector form, with symmetric. The problem: has free parameters — with features, far more than the number of observed ratings. We cannot learn this from sparse data. The fix is to factorise exactly as Ch. 8 factorised the URM:
Each feature now has a latent vector (row of ), and the interaction weight is their dot product. Parameters drop from to — from quadratic to linear in . For 100 users and 1000 items (), the unfactorised needs about 600k parameters; the factorised version with needs only 12,101.
Without factorisation
With factorisation
This is MF, applied to features
Ch. 8 factorised the rating matrix into user and item embedding tables. FM factorises the feature interaction matrix into a single feature embedding table — every feature, whatever its type, shares one latent space. That shared space is what makes FM a unified model.
The final FM with factorised interactions:
FM prediction · constant + linear + interaction
A collaborative FM on the shared URM. The prediction splits into ω₀ (global), ω_u+ω_i (the linear global-effects term), and v_u·v_i (the factorised interaction — the MF dot product). The interaction is what moves the score off a per-user constant.
Bob · Interstellar — f=2: interaction v_u·v_i = 0.837 · observed 4
04 · One model, many special cases
FM as a unifying model
Depending on which features you include, FM reproduces global effects, MF, SVD++, hybrid, and context-aware recommenders.
The beauty of FM is that different recommendation models are just different choices of input features fed into the same quadratic equation:
- Global effects (Ch. 2): user and item one-hot features, but drop the quadratic term. Then .
- Matrix factorization (Ch. 8): user and item one-hot, keep the quadratic. The interaction is the dot product of latent factors — exactly . The lecture confirms an FM with only collaborative data is equivalent to SVD++.
- Hybrid (Ch. 9): add content features (genres, actors) as extra one-hot columns. The quadratic term now includes user–genre and item–actor interactions — no model change.
- Context-aware: add context features (day, time, location). The quadratic term learns user–context and item–context interactions.
Collaborative FM
Collaborative + content
Context-aware
The key difference from S-SLIM (Ch. 9)
S-SLIM extends SLIM by adding the ICM as pseudo-users in a stacked matrix. FM extends any feature set by factorising pairwise interactions. FM is more general — it handles categorical features (day, time, location) that don’t fit a user–item matrix — but both rely on factorisation to beat sparsity in the interactions.
05 · The imbalance problem
Factorization Machines with implicit feedback
With implicit data, the model must see negatives. Without them, it learns to predict 1 for everything.
A dataset with implicit ratings (interacted / not, 1/0) contains only positives in raw form. Train FM on positives alone and it learns to predict 1 for every — useless for ranking. The fix is the same as for BPR (Ch. 7) and BCE (Ch. 11): negative sampling. For each positive , randomly draw an equal number of non-interacted items for and add them as negatives (target 0), balancing the training set:
Sampling matters
If you draw negatives uniformly from the vast pool of non-interactions, the model sees far more negatives than positives and learns to predict 0 for everything. Balance the two sets (50/50 is common) — identical to the BCE sampling strategy of Ch. 11. The same principle holds regardless of model.
06 · Exam intel
What the exam tests
Write the FM equation with all three terms; explain the one-hot input ( bits, two 1’s per row); show the linear term equals global effects; explain why reduces parameters from to ; derive ; name at least three models FM subsumes; and describe the implicit-feedback sampling strategy.
Worked question — count the parameters
A collaborative FM has users and items. (a) Length of ? (b) With , how many parameters? (c) Without factorisation, how many would the quadratic term alone need? (d) One model FM reduces to with only user and item features?
- (a) . Each row has exactly two 1’s (one user, one item).
- (b) .
- (c) — far more than the observed ratings on sparse data. FM avoids this explosion.
- (d) SVD++ / matrix factorization: with the global bias.
Traps: counting columns as instead of (it’s one row per rating, features); saying FM factorises the rating matrix (it factorises the feature interaction matrix , not the URM); forgetting the linear term is global effects; and confusing FM with S-SLIM (which stacks matrices, item-based only).
07 · Exam · past papers
Past-paper questions
Past paper Exam 2020 · CF recommender with FMs (6 pts)
Q. Implement a CF recommender with FMs: the input data structure (1); the prediction equation and its parameters (1); how parameters are learnt (1); the analogy with global effects + MF for explicit ratings (2); how FMs do context-aware recommendation (1).
Model answer. Input. One sparse feature vector per interaction: one-hot user block ⊕ one-hot item block ⊕ optional context/side-info blocks. Model. ; parameters are the global bias , per-feature weights , and a -dim factor per feature. Learning. Minimise a loss (MSE for explicit, BCE/BPR for implicit) by SGD or ALS with L2 regularization. Analogy. With only user/item one-hots, reproduces global effects and the MF dot product — so FM = global effects + MF. Context-aware. Append context blocks (time, device, location); the pairwise term automatically models user×context and item×context through the shared factors.
Past paper RS Exam · FM data structure and terms (7 pts)
Q. The data structure used by FMs and its relation to the URM (2); write the FM model (2); explain the terms and their relation to other models (2); how to use FMs for context-aware recommendation (1).
Model answer. Data ↔ URM. A flat design matrix: each observed URM entry becomes one training row with its user and item one-hot blocks active (label = rating). The URM is “unrolled” into feature vectors, which lets extra columns (context, attributes) be appended. Model. . Terms. = global average; = linear per-feature bias ( global effects); = factorised pairwise interaction ( MF / SVD++). Context-aware. Add context blocks; their factors interact with user/item factors automatically.
Past paper FT-Sample · FM ↔ MF + global effects (6 pts)
Q. Input data structure (1); prediction equation + parameters (1); how parameters are learned (1); the FM ↔ MF + global effects relationship for explicit ratings (2); context-aware use (1).
Model answer. Identical in substance to the 2020 question: input = sparse one-hot user ⊕ item ⊕ context; model ; learned by SGD/ALS on a regularised loss; the linear terms are global effects, the pairwise factor term is MF; context is handled by appending feature blocks.
Past paper Practice Exam 1 · Input structure & side information (5 pts)
Q. The input data structure, including how categorical features are handled (2); the FM model in summation notation with each component explained (1); how FMs incorporate side information and context (2).
Model answer. Input / categoricals. Each interaction is a sparse vector; categorical variables (user id, item id, genre, device…) are one-hot encoded into dedicated blocks; numeric features stay as single columns. Model. — a global bias, linear per-feature effects, factorised pairwise interactions. Side info / context. Append more one-hot/numeric blocks (attributes, time, location); the factorised term learns interactions between these and the user/item without adding per-pair parameters.
Past paper Practice Exam 3 · Global effects & implicit imbalance (5 pts)
Q. How global effects are incorporated into FM (2); the FM ↔ MF + global effects relationship for explicit ratings (1); how the imbalance problem is addressed with implicit ratings (2).
Model answer. Global effects. They are exactly the linear part: (global mean) plus the active for the user and item one-hots give the global + user + item biases. FM ↔ MF + GE. Linear terms = global effects; pairwise factor term = MF — so an FM on user/item one-hots equals global effects + MF. Imbalance. Implicit data is overwhelmingly negative, so training would be swamped by zeros; FMs build a balanced positive/negative set — sampling a comparable number of unobserved pairs per positive — so the optimiser sees real signal.
Past paper Practice Exam 5 · Factorised interactions & SVD++ (5 pts)
Q. How FMs factorise the interaction term to cut parameters (2); the FM ↔ SVD++ equivalence with only collaborative data (1); how implicit ratings are handled with balanced sampling (2).
Model answer. Factorised interactions. A naive model needs a free per pair (, and untrainable for never-co-observed pairs); FMs replace it with using -dim factors (), letting interactions generalise across pairs via shared factors — and the pairwise sum even computes in linear time. FM = SVD++. With user, item, and the user’s implicit-feedback features, an FM reproduces SVD++ — the factor interactions recover both the latent dot product and the implicit-feedback term. Balanced sampling. Sample a balanced set of negatives per positive so the loss is not dominated by the majority class.
08 · Self-check
Three questions before you move on
Why does FM factorise the interaction weight matrix W ≈ VVᵀ instead of learning W directly?
A collaborative-only FM with one-hot user and item features, keeping the quadratic term, is equivalent to which classic model?
How does FM achieve hybrid recommendation compared to S-SLIM (Ch. 9)?
09 · Recap
One-screen summary
Chapter 12 — load-bearing ideas
- FM = linear + factorised interactions: . Constant = global bias, linear = per-feature weights (global effects), quadratic = pairwise interactions factorised via latent feature vectors.
- Factorise , not : turns into parameters. Each feature shares its vector across all interactions — that is how FM generalises to unseen pairs.
- One model, many inputs: collaborative features → MF/SVD++; add content columns → hybrid; add context columns → context-aware. The FM equation never changes, only the feature vector does — a unified framework.
- Implicit feedback needs balanced negative sampling, or the model predicts 1 (or 0) everywhere.