Search

Search papers and lessons

Cross-Product Transformation

Level 3 · 13 min read

A cross-product transformation adds one new input that is 1 only when two inputs are both on, so a linear model can learn that a pair of features matters together.

On this page

  • Colour = topic mix
  • Probability
  • Discrete math
  • Calculus
  • Linear algebra
  • Classical models
  • Inference
  • ReadReady to readNot yet

TL;DR

A linear model adds up the effect of each input on its own, so it cannot by itself say that two inputs matter only when they occur together. A cross-product transformation fixes this by creating one extra input for each pair of inputs. That new input is 1 only when both original inputs are on, and 0 otherwise. Feeding these crossed inputs to a model such as Logistic Regression lets it learn a separate weight for the combination, which is how large recommendation and advertising systems have handled pairs like "this user" and "this app".

Builds on: Logistic Regression, One-Hot Encoding, Interaction Effect · Leads to: Wide & Deep learning, Embedding Layer

This lesson is about machine learning methods. It is not advice for any particular decision.

Where it's used

App recommendations. In the 2016 Wide & Deep paper, a team at Google built a recommender for the Google Play store. The "wide" part of the model is a linear model over many sparse yes-or-no features, and it uses cross-product transformations to capture pairs. The paper's example is a feature that is 1 only when the user has already installed one app and the app being shown is another. A linear model over single features cannot represent that pairing, but the crossed feature can carry a weight of its own. The paper pairs this with a neural "deep" part that generalises to pairs it has never seen.

Click prediction in advertising. Ad systems predict whether a user will click an ad from features such as the user's country, the device, the publisher site and the ad's category. Many useful signals are pairs: a particular category of ad may do well on phones but poorly on desktops. Engineers at large ad companies have described training logistic models on huge numbers of crossed categorical features, since a linear model is cheap enough to train and serve over very large data.

How it works

Take two yes-or-no inputs. M=1M = 1 means the impression happened in the morning, and P=1P = 1 means the user is on a phone. Both are indicators, and each is written as a one-hot input. The cross of MM and PP is a third input, C=M⋅PC = M \cdot P. For yes-or-no inputs, the product is the same as "both are on".

Use a logistic model with bias b=−1b = -1, weight wM=0.4w_M = 0.4 on MM, weight wP=−0.2w_P = -0.2 on PP, and weight v=1.5v = 1.5 on the cross CC. The score is z=b+wMM+wPP+vCz = b + w_M M + w_P P + v C, and the predicted probability is p=σ(z)p = \sigma(z), where σ\sigma is the sigmoid, which turns any score into a value between 0 and 1.

  1. Start with no inputs on. M=0M = 0 and P=0P = 0, so the cross is C=0C = 0. The score is z=−1z = -1. The probability is σ(−1)≈0.269\sigma(-1) \approx 0.269. Check: only the bias is counting.
  2. Turn on the morning input only. M=1M = 1, P=0P = 0, C=0C = 0. The score rises by 0.4 to z=−0.6z = -0.6, and p≈0.354p \approx 0.354. Check: one input moved the score by its own weight, and nothing else changed.
  3. Turn on the phone input only. M=0M = 0, P=1P = 1, C=0C = 0. The score falls by 0.2 to z=−1.2z = -1.2, and p≈0.231p \approx 0.231. Check: this weight is negative, so the phone lowers the score here.
  4. Turn on both, without the cross. Add the two single effects: z=−1+0.4−0.2=−0.8z = -1 + 0.4 - 0.2 = -0.8, and p≈0.310p \approx 0.310. Check: this is what a model with no cross would predict for both inputs on.
  5. Turn on both, with the cross. Now C=1C = 1, so the extra 1.51.5 is added: z=−0.8+1.5=0.7z = -0.8 + 1.5 = 0.7, and p=σ(0.7)≈0.668p = \sigma(0.7) \approx 0.668. Check: the jump from step 4 is exactly the cross weight, and it appears only in this case.
  6. Read the result. The cross raised the probability from about 0.31 to about 0.67 for the pair, but left the three other cases alone. That is the whole idea: one extra input whose only job is to switch on when the pair is present.
−4−3−2−10123400.20.40.60.81score zprobability
  • p = sigmoid(z)
The sigmoid turns the score into a probability. The two marked points are the scores -1 (no inputs on) and 0.7 (both on, with the cross) from the walkthrough.
flowchart LR
  A["Morning M (0 or 1)"] --> C["Score z = b + wM M + wP P + v C"]
  B["Phone P (0 or 1)"] --> C
  A --> D["Cross C = M times P"]
  B --> D
  D --> C
  C --> E["Sigmoid gives probability"]

The math

Definition: Cross-product transformation

Given two inputs uu and vv taking values in finite sets, a cross-product transformation creates one new input for every pair of values (i,j)(i, j). The new input is cij=1c_{ij} = 1 when u=iu = i and v=jv = j, and cij=0c_{ij} = 0 otherwise. For yes-or-no inputs this reduces to c=u⋅vc = u \cdot v.

Read the definition in words. If uu can be one of 3 values and vv one of 4, there are 3×4=123 \times 4 = 12 crossed inputs, and exactly one of them is 1 for any example. This is the same one-hot idea as in One-Hot Encoding, applied to pairs instead of single values.

Step 1 in symbols: the cross of two indicators

For M,P∈{0,1}M, P \in \{0, 1\}, the cross is C=M⋅PC = M \cdot P. Its truth table is:

M=0, P=0⇒C=0M=1, P=0⇒C=0M=0, P=1⇒C=0M=1, P=1⇒C=1\begin{aligned} M = 0,\ P = 0 &\Rightarrow C = 0 \\ M = 1,\ P = 0 &\Rightarrow C = 0 \\ M = 0,\ P = 1 &\Rightarrow C = 0 \\ M = 1,\ P = 1 &\Rightarrow C = 1 \end{aligned}

Step 2 in symbols: the score with a cross

The score is additive over the inputs, with the cross as one more input:

z=b+wMM+wPP+v Cz = b + w_M M + w_P P + v\,C

Here bb is the bias, wMw_M and wPw_P are the single-input weights, and vv is the weight on the cross. With C=M⋅PC = M \cdot P the score for the four cases is bb, b+wMb + w_M, b+wPb + w_P, and b+wM+wP+vb + w_M + w_P + v. The first three match a model with no cross. Only the last one carries vv, which is why the cross behaves like an interaction.

Step 3 in symbols: learning the cross weight

Training is a gradient descent step on the log-loss, as for any logistic model. For one example with label y∈{0,1}y \in \{0, 1\} and predicted probability pp, the gradient with respect to vv is (p−y) C(p - y)\,C. Since CC is 0 for most examples, the cross weight is learned only from examples where both inputs are on, which is why sparse crosses need a lot of data.

Step 4 in symbols: the logit view

Write the Log-Odds for the four cases with logit⁡(p)=ln⁡ ⁣(p/(1−p))\operatorname{logit}(p) = \ln\!\big(p/(1-p)\big). The effect of turning on MM when P=0P = 0 is wMw_M, and the effect of turning on MM when P=1P = 1 is wM+vw_M + v. The difference between these two effects is vv. This is the interaction term in log-odds form.

Worked examples

Example
Tiny: one cross, four cases

Use the model from the walkthrough: b=−1b = -1, wM=0.4w_M = 0.4, wP=−0.2w_P = -0.2, v=1.5v = 1.5.

  1. Case (M,P)=(0,0)(M, P) = (0, 0): C=0C = 0, so z=−1z = -1. Then σ(−1)≈0.269\sigma(-1) \approx 0.269.
  2. Case (1,0)(1, 0): C=0C = 0, so z=−1+0.4=−0.6z = -1 + 0.4 = -0.6. Then σ(−0.6)≈0.354\sigma(-0.6) \approx 0.354.
  3. Case (0,1)(0, 1): C=0C = 0, so z=−1−0.2=−1.2z = -1 - 0.2 = -1.2. Then σ(−1.2)≈0.231\sigma(-1.2) \approx 0.231.
  4. Case (1,1)(1, 1): C=1C = 1, so z=−1+0.4−0.2+1.5=0.7z = -1 + 0.4 - 0.2 + 1.5 = 0.7. Then σ(0.7)≈0.668\sigma(0.7) \approx 0.668.
  5. Interpretation: without the cross, case 4 would have z=−0.8z = -0.8 and probability 0.3100.310. The cross adds 1.51.5 to the score only when both inputs are on, and it moves that case from 0.31 to 0.67.
Example
Standard: recovering a cross weight from a 2 by 2 table

Suppose the observed click rates are p00=0.2p_{00} = 0.2, p10=0.3p_{10} = 0.3, p01=0.2p_{01} = 0.2 and p11=0.6p_{11} = 0.6, where the first digit is MM and the second is PP. We want a logistic model with a cross that reproduces these rates.

  1. Convert each rate to log-odds. logit⁡(0.2)=ln⁡(0.25)≈−1.386\operatorname{logit}(0.2) = \ln(0.25) \approx -1.386.
  2. logit⁡(0.3)=ln⁡(3/7)≈−0.847\operatorname{logit}(0.3) = \ln(3/7) \approx -0.847.
  3. logit⁡(0.6)=ln⁡(1.5)≈0.405\operatorname{logit}(0.6) = \ln(1.5) \approx 0.405.
  4. The bias is the log-odds of the all-off case: b=−1.386b = -1.386.
  5. The morning weight is the change from (0,0)(0,0) to (1,0)(1,0): wM=−0.847−(−1.386)≈0.54w_M = -0.847 - (-1.386) \approx 0.54.
  6. The phone weight is the change from (0,0)(0,0) to (0,1)(0,1): wP=−1.386−(−1.386)=0w_P = -1.386 - (-1.386) = 0. The phone alone does not change the click rate.
  7. The cross weight is v=logit⁡(p11)−logit⁡(p10)−logit⁡(p01)+logit⁡(p00)≈0.405+0.847+1.386−1.386≈1.25v = \operatorname{logit}(p_{11}) - \operatorname{logit}(p_{10}) - \operatorname{logit}(p_{01}) + \operatorname{logit}(p_{00}) \approx 0.405 + 0.847 + 1.386 - 1.386 \approx 1.25.
  8. Check the fourth cell: −1.386+0.54+0+1.25≈0.40-1.386 + 0.54 + 0 + 1.25 \approx 0.40, and σ(0.40)≈0.60\sigma(0.40) \approx 0.60. This matches p11=0.6p_{11} = 0.6.
  9. Interpretation: the table cannot be reproduced by single-input weights alone. The cross of 1.251.25 is the part that says the two inputs together push clicks up more than either does alone.
Example
Realistic: an ad impression with a sparse cross

An ad system has 20 device types and 500 ad categories. Crossing them gives 20×500=10,00020 \times 500 = 10{,}000 possible cross inputs, and each impression switches on only one of them. Take one impression with these active inputs: a bias of −2.5-2.5, device "phone" with weight 0.30.3, country "US" with weight 0.10.1, and the cross of phone with the category "games" with weight 0.90.9.

  1. Add the active weights: z=−2.5+0.3+0.1+0.9=−1.2z = -2.5 + 0.3 + 0.1 + 0.9 = -1.2.
  2. Apply the sigmoid: p=σ(−1.2)≈0.23p = \sigma(-1.2) \approx 0.23.
  3. Interpretation: a 23 percent predicted click probability. The cross of phone and games contributes 0.90.9, which is the largest feature weight in the sum, so the model is saying that games on phones are a strong pairing.
  4. Now the same impression with the category "news" instead. The cross is not active, and no other inputs change.
  5. Try it: with the category "news", what is the score zz and the probability pp?

Answer: z=−2.5+0.3+0.1=−2.1z = -2.5 + 0.3 + 0.1 = -2.1, and p=σ(−2.1)≈0.11p = \sigma(-2.1) \approx 0.11. The probability falls from about 0.23 to about 0.11, and the whole drop comes from the cross weight of 0.90.9 no longer applying.

Common mistakes and tradeoffs

It is tempting to think that crossing features always improves a model. In fact, a cross adds a weight for every pair that appears in the data, and most pairs are rare or never seen. A rare pair gets a weight estimated from very few examples, so it can be noisy. This is why large systems often keep crosses only for pairs with enough data, or regularise them with an L2 penalty.

It is tempting to think that the cross replaces the single-input features. In fact, the single inputs must stay in the model. The cross measures only the extra effect of the pair, on top of each input's own effect. Without the single inputs, the model cannot represent the common case where one input alone matters.

It is tempting to think that a cross of two numbers means something like adding them. In fact, for yes-or-no inputs the cross is an AND, and for real-valued inputs it is a product. A product of two numbers is a different thing from either input, so its scale and sign need care.

When to use it. Use crosses when the inputs are categorical, the model is linear, the number of examples is large, and you have a clear reason to think certain pairs matter together. Crosses are a cheap and interpretable way to capture interactions.

When not to use it. Avoid crosses when data is small, when many inputs might interact in complex ways, or when most pairs are never observed. A linear model with a huge number of crosses can overfit and become slow to store.

Alternatives. An embedding inside a neural network can learn pair effects without listing every pair by hand, and it can generalise to pairs it never saw. Factorisation-style models, which learn a short vector for each input and combine vectors to score pairs, need fewer weights than a full cross table.

ApproachLearns pair effectsNeeds data for each pairInterpretable
Single inputs onlyNoNoYes
Explicit crossYes, one weight per pairYesYes
Neural network with embeddingsYes, through shared structureLessLess

History

Before modern machine learning, statisticians captured interactions in regression by adding product terms, which is the same idea as the cross applied to numeric inputs. The approach was standard in applied statistics for decades.

For very large categorical data, the problem was the number of possible pairs. Models in the early 2010s for click prediction used logistic regression over one-hot inputs, and engineers at large advertising companies described explicitly crossing categorical features to capture pairs that single features missed. The approach was practical because the linear model could be trained and served cheaply on very large data.

The 2016 Wide & Deep paper combined this with a neural network. Its linear "wide" part kept explicit cross-product features, while its "deep" part learned from embeddings. The combination showed that memorising known pairs and generalising to new ones could be done in one model, and the idea spread through recommendation and ranking systems.

Recap

  • A linear model adds each input's effect, so it cannot capture an effect that exists only when two inputs are both on.
  • A cross-product input is 1 only when a specific pair of values occurs, and 0 otherwise.
  • Adding a cross to a logistic model gives the pair its own weight, which is an interaction in log-odds, as in Interaction Effect.
  • The cross needs data for each pair, and the single inputs must stay in the model.
  • Read next: Wide & Deep learning for the combined wide and deep model, and Embedding Layer for the neural alternative.

Check yourself

QuestionRecall: what does a cross-product input equal when both of two yes-or-no inputs are 1, and when is it 0?Show answer
It equals 1 only when both inputs are 1. In every other case, including when only one input is 1, it is 0. That is the AND of the two inputs.
QuestionComputation: with bias b=0b = 0, weight wM=1w_M = 1 on MM, weight wP=1w_P = 1 on PP, and cross weight v=−2v = -2, what is the score zz when M=1M = 1 and P=1P = 1? Give a number.Show answer
The cross is active, so C=1C = 1. The score is z=0+1+1+(−2)⋅1=0z = 0 + 1 + 1 + (-2) \cdot 1 = 0. Answer: z=0z = 0.
QuestionWhy: a model without crosses predicts the same click rate for a phone-and-games impression as it would for the phone and the games category separately. Why can that be wrong?Show answer
Without a cross, the model adds the separate effects of phone and games, so it cannot represent a pairing that matters only when both occur. If phones and games together convert much better than either alone, the additive model underestimates that pair. A cross weight fixes this by adding an extra term that applies only to the pair.
QuestionApplied: a team has 3 million impressions, 40 device types and 2,000 ad categories. Someone proposes crossing every device with every category. Give one reason this could go wrong and one step to reduce the risk.Show answer
The number of crosses is 40×2,000=80,00040 \times 2{,}000 = 80{,}000, and most pairs will have very few impressions, so their weights will be noisy and may overfit. A sensible step is to keep only pairs with enough examples, or to regularise the cross weights with an L2 penalty and keep the single-input features in the model.

Sources