TL;DR
A factorization machine is a model that adds up the effect of each input and also the effect of every pair of inputs. Instead of learning a separate weight for each pair, as an explicit cross does, it gives each input a short vector of numbers and scores a pair by the dot product of the two vectors. Because each vector is learned from every pair the input appears in, the model can estimate the effect of a pair it never saw together. It was designed for sparse data such as user and item IDs, where most pairs are missing.
Builds on: Linear Regression, Matrix Factorization, One-Hot Encoding · Leads to: DeepFM, Embedding Layer
This lesson is about a machine learning method. It is not advice for any particular decision.
Where it's used
Click-through prediction. An online ad or product listing is shown to a user, and the system must estimate the chance that the user clicks. The inputs are categorical, such as user country, device, ad category and hour of day, and almost every impression switches on only a few of thousands of possible values. Factorization machines were built for this kind of data, and they were used widely in ad systems and in click-prediction competitions in the early 2010s.
Recommendation from ratings and IDs. A recommender sees pairs of user ID and item ID, together with side information such as genre or price. A factorization machine handles all of these as one-hot inputs. With only the user and item IDs active, it reduces to the matrix factorization model, so it contains that method as a special case.
How it works
Use three inputs in a tiny model: user , user and item are one-hot, so an impression with user and item has , , and all other inputs 0. Use vectors with numbers. Set all linear weights and the bias to 0, so only the pair term matters. The vectors are and . The target rating is 3.
- Write down the active inputs. Only and are on, so there is one active pair, . Check: every pair with an input that is 0 contributes nothing.
- Compute the pair score. The dot product is . Since both inputs are 1, the pair term is 1. The prediction is . Check: the linear part is 0, so the whole prediction comes from the pair.
- Check the shortcut. Add the two vectors to get . Its squared length is . The squared lengths of the separate vectors are . Half the difference is , which matches step 2.
- Measure the error. The prediction minus the target is . The negative sign says the model should increase its score.
- Compute the gradient for . The pair term changes with in proportion to the other active vector, . With squared loss , the gradient is .
- Compute the gradient for . Likewise it is . Both gradients use the old vectors.
- Take a step against the gradient with learning rate . Subtract times each gradient. The new vectors are and .
- Check the new prediction. The new dot product is . The prediction moved from 1 toward the target 3, as it should.
- sigmoid(z)
flowchart LR A["Active inputs, e.g. user A and item X"] --> B["Linear part: sum of w_i x_i"] A --> C["Look up each active input's vector v_i"] C --> D["Dot product of every active pair"] B --> E["Add bias, linear part and pair part"] D --> E E --> F["Prediction (regression) or sigmoid (yes-or-no)"]
The math
Let be the input, a bias, linear weights, and vectors with factors. The prediction is
where is the dot product of the two vectors.
Read it in words: the first two terms are a linear model, and the last term adds, for every pair of inputs, the dot product of their vectors times the product of their values. For one-hot inputs the product is 1 exactly when both are on, so the pair term is the sum of dot products over the active pairs.
Step 1 in symbols: the pair term
For the example, and all others are 0. The sum over pairs has only one nonzero term, . That is the pair score from the walkthrough.
Step 2 in symbols: the shortcut that makes it fast
A naive evaluation loops over all pairs, which is too slow when is in the millions. The pairwise sum can be rewritten. For each factor , let . Then
To see why, expand the square. Each cross term with appears twice, once for and once for , and the terms are removed by the second sum. The halving removes the double count. The cost is instead of . With only the active inputs, the cost depends on how many are on, not on .
Step 3 in symbols: the gradient
For squared loss with error , the derivative of the prediction with respect to one component of a vector is
Here means the sum over all other inputs. The gradient step is , with the learning rate. In the walkthrough, , which gives the update in step 7. For the update rule itself see Stochastic Gradient Descent.
Step 4 in symbols: from pairs to matrix factorization
Let the active inputs be one user and one item , and no other inputs, with and the linear weights zero. Then . This is exactly the rating prediction in Matrix Factorization, with as the user factor and as the item factor. Adding back the linear weights gives the usual bias terms, so the factorization machine contains the matrix factorization model as a special case.
For classification
For a yes-or-no outcome, use the same score as the input to a sigmoid, and train with the log-loss as in Logistic Regression. The pair term is unchanged; only the output and the loss differ.
Worked examples
Use , so each input has one number. Take , , , , and , , . The input is .
- The linear part is .
- The only active pair is , with pair term .
- The prediction is .
- Check with the shortcut: , so . The same.
- Interpretation: the two active inputs have a large positive pair effect, because their factors multiply to a large positive number.
Three users and three items are one-hot. With , the learned vectors are , , for users, and , , for items. Assume no linear weights. User 2 and item 2 never appear together in the training data.
- The active inputs are user 2 and item 2. The prediction is the dot product .
- Check with the shortcut: , so summed over factors is . The squared lengths are and , which sum to 6. The pair term is . The same.
- An explicit cross model would have a separate weight for this pair. That weight was never updated, so it would stay at its starting value, usually 0.
- Interpretation: the factorization machine predicts 1 for the unseen pair because user 2 and item 2 each have vectors learned from other pairs. The shared structure of the vectors is what lets it generalise.
- Try it: what does the factorization machine predict for user 2 and item 3, and what does the shortcut give as a check?
Answer: . Check: , summed is , the squared lengths are and , so .
An ad impression has four active one-hot inputs: country US, device phone, ad category games, and hour evening. Use with vectors US , phone , games and evening . The bias is , and the linear weights are US , phone , games , evening .
- The linear part is .
- The six pair dot products are: US and phone ; US and games ; US and evening ; phone and games ; phone and evening ; games and evening .
- Add the pairs: .
- The score is .
- The probability is .
- Check with the shortcut: the sum of the four vectors is , and its squared length is . The squared lengths of the four vectors add to . Then , which matches step 3.
- Interpretation: the games and evening pair adds the most, so the model sees that games in the evening are a strong combination, while the phone and evening pair pulls the score down.
- Try it: remove the evening input from the impression. What are the score and the probability ?
Answer: the pair sum drops by the three pairs that contain evening, which total , so the pairs now sum to . Then and . Check: without evening the sum of vectors is , with squared length ; the squared lengths are ; .
Common mistakes and tradeoffs
It is tempting to think that a factorization machine learns a separate weight for every pair, like an explicit cross. In fact, every pair weight is a dot product of two vectors, so pairs share parameters. This is the source of its strength on sparse data, and also of its limit: with small, not every table of pair weights can be represented.
It is tempting to think the pair term costs work and so is impractical. In fact the identity in Step 2 reduces it to , and with one-hot inputs only the active inputs matter. Implementations use this shortcut, and a naive double loop is only for checking small cases.
It is tempting to think the factors have fixed meanings, such as "comedy" or "phone". In fact the vectors are only determined up to a rotation: multiplying every vector by the same orthogonal matrix leaves all dot products, and so all predictions, unchanged. Read the factors as useful structure, not labels.
It is tempting to think that more factors always help. In fact a larger adds parameters and can overfit when data is thin, so and the penalty on the vectors are chosen by validation.
When to use it. Use a factorization machine for sparse categorical data with many pairs, where some pairs are rare or unseen and the model must generalise. It is fast to train and serve, and it handles user IDs, item IDs and side features in one model.
When not to use it. Avoid it when the inputs are dense numbers, when effects of three or more inputs matter, or when a neural network with enough data would do better. Degree-2 factorization machines do not model three-way interactions.
Alternatives. An explicit Cross-Product Transformation gives each pair its own weight, which is exact but needs data for every pair. Matrix factorization handles only two fields, such as user and item. A Feedforward Neural Network over embeddings learns richer interactions and can be combined with a factorization machine, as in DeepFM.
| Approach | Pair effects | Unseen pairs | Higher-order effects |
|---|---|---|---|
| Linear model | None | Not applicable | No |
| Explicit cross | Own weight per pair | Weight stays at start value | No |
| Factorization machine | Dot product of vectors | Predicted from vectors | No, degree 2 only |
| Neural network with embeddings | Learned, flexible | Predicted from embeddings | Yes |
History
Before factorization machines, the standard tools for ratings were matrix factorization, which handled only two kinds of ID, and polynomial regression, which needed separate weights for each pair. In 2010 Steffen Rendle introduced the factorization machine as a model that covers both cases, with the pairwise sharing of parameters that makes it work on sparse data.
Factorization machines spread through click-prediction work in the early 2010s, and some winning entries in click-prediction competitions of 2014 and 2015 used variants of the model. A later variant, the field-aware factorization machine, gave each input a separate vector for each group of inputs it might be paired with.
In 2017 DeepFM combined a factorization machine with a neural network that shares the same embeddings, so the model learns low-order and higher-order effects together. That design is now a common reference point for click prediction.
Recap
- A factorization machine scores each input with a linear weight and each pair of inputs with the dot product of their vectors.
- The pair term can be computed in linear time with the squared-sum identity, which makes it practical for sparse data.
- Each vector is learned from all the pairs its input appears in, so the model predicts pairs it never saw together.
- With only user and item IDs active it reduces to Matrix Factorization.
- Read next: DeepFM, which adds a neural network to the same embeddings, and Embedding Layer for the neural view of these vectors.