Architectures / Recommendation
๐ฏ Deep Interest Network (DIN)
Alibaba 2018 - the candidate item is the attention QUERY over the behaviour sequence, so the user embedding is computed per candidate instead of once.
From Zhou et al. (2018). Deep Interest Network for Click-Through Rate Prediction. KDD 2018. 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
15 layers. Output shapes are propagated from the input shape, batch dimension excluded.
| Layer | Type | Parameters | Output shape | |
|---|---|---|---|---|
| 1 | Behavior Seq (50 items) | Input | shape=[50] | 50 |
| 2 | Item Embed | Embedding | vocabSize=1000000 | 50 ร 64 |
| 3 | Candidate Item | Input | shape=[1] | 1 |
| 4 | Candidate Embed | Embedding | vocabSize=1000000 | 1 ร 64 |
| 5 | Local Activation Unit | Target Attention (DIN) | embedDim=64 | 64 |
| 6 | User Profile | Input | shape=[16] | 16 |
| 7 | User Tower | Linear | outFeatures=32, inFeatures=16 | 32 |
| 8 | [interest; user] | Concatenate | 96 | |
| 9 | MLP 200 | Linear | outFeatures=200, inFeatures=96 | 200 |
| 10 | Dice (~PReLU) | PReLU | 200 | |
| 11 | MLP 80 | Linear | outFeatures=80, inFeatures=200 | 80 |
| 12 | Dice (~PReLU) | PReLU | 80 | |
| 13 | CTR Head | Linear | outFeatures=1, inFeatures=80 | 1 |
| 14 | Sigmoid | Sigmoid | 1 | |
| 15 | 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 DeepInterestNetworkDIN(nn.Module):
def __init__(self):
super().__init__()
self.embedding_1 = nn.Embedding(1000000, 64)
self.embedding_2 = nn.Embedding(1000000, 64)
self.targetAttention_1 = TargetAttention(embed_dim=64, hidden_dim=36)
self.linear_1 = nn.Linear(16, 32)
self.linear_2 = nn.Linear(96, 200)
self.prelu_1 = nn.PReLU(num_parameters=1)
self.linear_3 = nn.Linear(200, 80)
self.prelu_2 = nn.PReLU(num_parameters=1)
self.linear_4 = nn.Linear(80, 1)
def forward(self, src, tgt=None, in3=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.