Architectures / Recommendation
๐ Search-based Interest Model (SIM)
Alibaba 2020 - a lifelong sequence (2000 events) is cut to the 50 most relevant before anything quadratic runs, then attended against the candidate.
From Pi et al. (2020). Search-based User Interest Modeling with Lifelong Sequential Behavior Data for Click-Through Rate Prediction. CIKM 2020. This page is the graph, not the PDF: open it, edit it, verify it, export it.
Every number on this page is computed from the graph by the same functions the app runs, not written by hand.
When to pick it
Structure
16 layers. Output shapes are propagated from the input shape, batch dimension excluded.
| Layer | Type | Parameters | Output shape | |
|---|---|---|---|---|
| 1 | Lifelong Seq (2000 events) | Input | shape=[2000] | 2000 |
| 2 | Item Embed | Embedding | vocabSize=1000000 | 2000 ร 64 |
| 3 | Candidate Item | Input | shape=[1] | 1 |
| 4 | Candidate Embed | Embedding | vocabSize=1000000 | 1 ร 64 |
| 5 | GSU: top-50 by relevance | Long-Sequence Retrieval (SIM/ETA GSU) | embedDim=64, topK=50 | 50 ร 64 |
| 6 | ESU: multi-head target attention | Target Attention (DIN) | embedDim=64 | 64 |
| 7 | User Profile | Input | shape=[16] | 16 |
| 8 | User Tower | Linear | outFeatures=32, inFeatures=16 | 32 |
| 9 | [interest; user] | Concatenate | 96 | |
| 10 | MLP 200 | Linear | outFeatures=200, inFeatures=96 | 200 |
| 11 | Dice (~PReLU) | PReLU | 200 | |
| 12 | MLP 80 | Linear | outFeatures=80, inFeatures=200 | 80 |
| 13 | Dice (~PReLU) | PReLU | 80 | |
| 14 | CTR Head | Linear | outFeatures=1, inFeatures=80 | 1 |
| 15 | Sigmoid | Sigmoid | 1 | |
| 16 | pCTR | Output | 1 |
What the verifier says
The same 43 structural checks that run on every edit in the app, on this graph.
output-activation
vanishing-gradient
deep-no-norm
init-activation-mismatch
The PyTorch it exports
Generated from the graph above. First 46 lines; the app exports the whole file, plus the training loop, the data contract and a deploy bundle.
# Architecture designed with Neurarch: https://neurarch.com
# PyTorch: compatible with Python 3.8+ and torch>=1.12
# Colab: pip install torch torchvision (usually pre-installed)
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Tuple
class TargetAttention(nn.Module):
"""DIN's local activation unit. The CANDIDATE is the query over the
behaviour sequence, so the sequence axis is consumed and one interest
vector comes out per candidate."""
def __init__(self, embed_dim: int, hidden_dim: int = 36):
super().__init__()
self.mlp = nn.Sequential(
nn.Linear(4 * embed_dim, hidden_dim),
nn.PReLU(),
nn.Linear(hidden_dim, 1),
)
def forward(self, query: torch.Tensor, keys: torch.Tensor) -> torch.Tensor:
if query.dim() == keys.dim() - 1:
query = query.unsqueeze(-2)
q = query[..., :1, :].expand_as(keys)
feats = torch.cat([q, keys, q - keys, q * keys], dim=-1)
w = self.mlp(feats).softmax(dim=-2)
return (w * keys).sum(dim=-2)
class BehaviorRetrieval(nn.Module):
"""SIM / ETA general search unit: cut a lifelong behaviour sequence to its
top-k most relevant events BEFORE anything quadratic runs. Hard mode is a
category lookup and learns nothing."""
def __init__(self, top_k: int = 50, embed_dim: int = 64, mode: str = "soft"):
super().__init__()
self.top_k = top_k
self.mode = mode
self.proj = None if mode == "hard" else nn.Linear(embed_dim, embed_dim, bias=False)
def forward(self, seq: torch.Tensor, target=None) -> torch.Tensor:
k = min(self.top_k, seq.size(-2))
h = seq if self.proj is None else self.proj(seq)
if target is None:
For agents
This architecture is machine-readable end to end. An agent can list the set, fetch this graph, edit it, and have the edit verified before any GPU time is spent.