DIN model
"TLDR: This article introduces the DIN model, which uses an attention mechanism to measure the relevance of each item in a user's history to the target item. The article elaborates on the principles of DIN, the computation method of the attention mechanism, strategies for initializing model parameters, and approaches to addressing cold-start and data imbalance issues."
Main Idea:
User interests are diverse, and a user's current interest is only related to a portion of their historical behaviors.
DIN uses an attention mechanism to measure the relevance between each item in the user's history and the target item. Historical behaviors with high relevance receive higher weights, while irrelevant historical behaviors can be ignored.
Principle
Specifically, the model takes three types of input: user features (e.g., age, gender, etc.), user historical behaviors (previously clicked items), and target item features (product information).
Here is an example, assuming there is a user (u) and a target item (i):
- User (u) features might be
- User (u)'s historical behavior sequence might be
- Target item (i) features might be
- If user (u) clicked item (i), the label is 1; if not clicked, it is 0
First, the system converts all features into dense embedding vectors.
Attention Mechanism
Through the attention mechanism, the system computes the relevance between each historical behavior and the target item. Each element in the user's historical behavior sequence [item1, item2, item3] represents a user behavior, and each behavior is converted into an embedding vector . Then, the relevance score between each and the target is calculated. These relevance scores are multiplied by the original historical behavior embeddings and summed to obtain the user interest representation .

The calculation formula is as follows: .
Here, are the historical behavior feature embeddings of user U, represents the embedding vector of candidate ad A, and denotes the degree of relevance between the historical behavior item and the current ad A. represents a feedforward neural network, i.e., the attention mechanism.
It is worth noting that in addition to the historical behavior vectors and the candidate ad vector, the input also includes their outer product operation, which helps the model perform explicit relevance modeling.
It should be particularly noted that the sum of these weights is not 1. To be precise, standardized weights are not used here; instead, the relevance scores are directly used as weights (i.e., the scores before softmax). This preserves the intensity information of user interest.
Another point to note is that historical behaviors have a sequential nature, and the length of historical behavior features may vary across different users. Since neural networks require sequences of equal length, we typically use padding operations (filling with 0) to make sequences reach the maximum length. During computation, a mask is used to mark the padded positions to ensure computational accuracy.
Dice
Formula:
In essence, it is a further transformation applied on top of Batch Normalization (BN).
Initializing Model Parameters
This is a common problem faced by deep learning models. It is typically alleviated through Xavier initialization or He initialization to mitigate gradient vanishing or explosion issues.
- Xavier initialization: Samples the weight matrix from distribution, where
- He initialization: Proposed to alleviate the gradient vanishing problem of ReLU, it samples the weight matrix from distribution, where , and denotes the number of input nodes of the matrix. Since ReLU tends to produce zero gradients in small-value regions, this issue is mitigated by increasing the standard deviation of the weights.
Cold Start
The solution is to first use content-based recommendation or rule-based recommendation, and then switch to DIN once sufficient data has been accumulated.
Data Imbalance Problem
In the search ad recommendation domain, a user clicking an item usually indicates genuine interest, but not clicking does not necessarily mean disinterest—it could simply be overlooked. In the collected data, positive samples (interested) are often far fewer than negative samples (not interested). Solutions include: oversampling/undersampling, or applying smaller weights to majority-class samples and larger weights to minority-class samples in the loss function.