Tag: Ranking

  • DL0033 Transformer Computation

    In a Transformer architecture, which components are the primary contributors to computational cost, and why?

    Question card with three dashed panels: a chat workload of short 128-token requests, a document workload of 8,192-token inputs on the same d_model 512 Transformer, and two optimization tickets (slim the FFN, or sliding-window attention) with the question of which ticket fits which workload.

    The Problem: A serving team runs one Transformer (d_model = 512, standard 4x FFN) on two workloads: 128-token chat requests and 8,192-token document summaries. They can fund only one speed-up ticket per workload and must first decide which component is eating the compute in each case.

    Answer

    It depends on sequence length. For short sequences, the feed-forward network (FFN) usually dominates: its two wide GEMMs cost O(n d^2) while attention’s quadratic term is still small. For long sequences, multi-head attention takes over: forming the n \times n score matrix costs O(n^2 d) and grows much faster than anything else in the block.

    (1) Multi-Head Attention: Q/K/V projections cost O(nd^2), but the score matrix QK^\top and its product with V cost O(n^2 d), the quadratic term that explodes on long sequences.
    (2) Feed-Forward Network: Two dense layers with expansion factor 4 cost O(nd^2), dominant when n is small but d is large.
    (3) Crossover Point: Attention overtakes the FFN roughly when n \approx 2d; near n = 1024 for the classic d_{model} = 512 design.

    Line chart of MHA and FFN share of total FLOPs versus sequence length on a log scale, crossing at sequence length 1024 where each takes fifty percent.

    Figure 1: FLOP share vs sequence length (d = 512): FFN dominates below n \approx 1024; past the crossover, attention’s quadratic cost becomes the bottleneck.

    Mathematical Formulation (per block):
    \text{Cost}_{\text{attn}} = \underbrace{4nd^2}_{\text{QKVO projections}} + \underbrace{2n^2 d}_{QK^\top \text{ and } AV}
    \text{Cost}_{\text{FFN}} = 2 \cdot n \cdot d \cdot 4d = 8nd^2

    Where:

    • n is the sequence length and d = d_{model}; constants count multiply–adds per GEMM.
    • The 2n^2 d term comes from multiplying (n \times d) \cdot (d \times n) to form scores, and again to mix values.
    • Softmax itself is cheap elementwise work but also scales with n^2 entries.
    Sequence Length nMHA Share (%)FFN Share (%)Dominant Component
    6434.6965.31FFN
    25638.4661.54FFN
    102450.0050.00Tie
    409671.4328.57MHA

    Table 1: FLOP breakdown at d = 512: the tie at n = 1024 marks where quadratic attention catches up with the linear-in-n FFN.

    Flowchart of one Transformer block annotating each operation with its FLOP complexity: projections linear in n, score matrix quadratic in n, FFN linear in n but quadratic in d.

    Figure 2: Where the FLOPs live inside one block: only the QK^\top and AV pair grows quadratically with sequence length.

    Practical Caveat: The FFN’s dominance at short n assumes the standard 4x expansion; shrink the expansion to 1x and the Q/K/V projections become the largest term, while efficient attention variants (sliding-window, linear attention) specifically attack the n^2 term for long contexts.


    Login to view more content
  • DL0015 Cold Start

    What is a “cold start” problem in deep learning?

    Whiteboard question card with three dashed panels from a music app review: long-time listener Maya's tailored feed, new signup Sam's generic popular-chart feed, and a track uploaded today with zero plays that appears in no feeds.

    The Problem: At TuneLoop’s weekly recommender review, long-time listener Maya (840 ratings) gets spot-on picks. Sam, who signed up five minutes ago with 0 ratings, gets a generic chart list, and a track uploaded this morning with 0 plays has not appeared in a single feed. What problem is the team looking at?

    Answer

    The cold start problem is the difficulty of making reliable predictions for new entities (users, items, or contexts) that have little or no historical data. Models that depend on past interactions, especially recommender systems built on collaborative filtering, have no signal to learn a meaningful representation of a brand-new user or item, so their predictions degrade to near-random or popularity-biased guesses until data accumulates.

    (1) Missing Interaction History: Collaborative filtering infers taste from a user-item matrix; a new row (user) or column (item) is empty, so the model cannot locate the entity in its embedding space.
    (2) Feedback Loop Risk: Poor early predictions reduce engagement, which further slows data collection for the new entity. The problem compounds itself.
    (3) Three Flavors: New-user cold start, new-item cold start, and new-system cold start (no data at all) each demand different remedies.

    User-item rating matrix with observed ratings shaded by value and a dashed red new-user row and new-item column filled with question marks to illustrate the cold start problem.

    Figure 1: The new user row and new item column contain no interactions. Collaborative filtering has nothing to condition on for them.

    Mitigation Strategies: The common theme is supplying side information until interaction data accumulates: transfer learning borrows representations from related domains; hybrid models mix collaborative signals with content features; and active onboarding explicitly gathers a few preferences from new users.

    (1) Transfer Learning / Pre-trained Models: Initialize from embeddings or models trained on similar tasks so the new domain starts from useful structure rather than random weights.
    (2) Hybrid Recommendation Models: Combine collaborative filtering with content-based features (user demographics, item metadata) so predictions remain reasonable with zero interactions.
    (3) Active Learning / User Onboarding: Ask new users to rate a handful of popular or diverse items, turning cold start into a short warm-up phase.

    Diagram of three mitigation strategies, transfer learning, hybrid model, and active onboarding, feeding into a recommender that produces reasonable predictions for new users and items.

    Figure 2: All three strategies inject auxiliary signal into the recommender so cold entities get reasonable predictions before their interaction rows fill in.

    Mathematical Formulation:
    \hat{r}_{ui} = \mu + b_u + b_i + p_u^{\top} q_i

    Where:

    • \hat{r}_{ui} is the predicted rating of user u for item i; \mu is the global mean rating.
    • p_u and q_i are the learned latent factor vectors for user u and item i; b_u, b_i are bias terms.
    • Cold start means p_u or q_i was never trained: with an empty interaction row/column, the factors stay at random init, so \hat{r}_{ui} is meaningless.

    Login to view more content
  • ML0016 AUC

    What is AUC?

    Two spam filters score the same eight emails on a 0 to 1 scale. Both reach 75 percent accuracy at threshold 0.5, and a small table shows their accuracies swapping as the threshold moves to 0.3 and 0.7.

    The Problem: Two spam filters score the same eight emails, and both reach 75% accuracy at threshold 0.5. Moving the threshold changes which filter looks better. What single number could rank the filters without committing to one threshold?

    Answer

    AUC (Area Under the Curve) measures a binary classifier’s ability to distinguish between the positive and negative classes, computed as the area under its ROC curve, the curve that trades off true positive rate against false positive rate across all thresholds. Its value ranges from 0 to 1: 1.0 is a perfect classifier, 0.5 is random guessing (the diagonal), and below 0.5 is worse than random, which rarely happens and can be flipped into a better model. AUC’s advantages: it is threshold-independent, summarizing performance over every decision threshold at once; it handles imbalanced data well because it evaluates the ranking of predictions rather than absolute counts; and it gives an intuitive single number for comparing different models.

    (1) Definition: The area under the ROC curve: one scalar summarizing threshold-free discrimination.
    (2) Probabilistic Meaning: AUC equals the probability that a random positive scores higher than a random negative.
    (3) Reading The Scale: 1.0 perfect, 0.5 random, below 0.5 inverted.

    ROC curve with the area under it shaded and AUC value labeled

    Figure 1: AUC is literally the shaded area under the ROC curve; the wider the curve bows toward the top-left corner, the larger the area.

    Mathematical Formulation:
    \text{AUC} = P\big(s(x^{+}) > s(x^{-})\big)
    \text{AUC} = \int_{0}^{1} \text{TPR}(\text{FPR}) \, d\text{FPR}

    Where:

    • s(x) is the model’s score (e.g., predicted probability) for sample x.
    • x^{+} is a randomly chosen positive sample and x^{-} a randomly chosen negative one.
    • \text{TPR}(\text{FPR}) is the ROC curve itself, true positive rate as a function of false positive rate.
    • The two lines are equivalent: the geometric area (integral) equals the pairwise-ranking probability, which is why AUC measures ranking quality.

    Login to view more content