Architectures / Recommendation
๐ผ Two-Tower
User+Item dual encoder for retrieval โ embeddings โ MLP per side โ dot product score
From Yi et al. (2019). Sampling-Bias-Corrected Neural Modeling for Large Corpus Item Recommendations. RecSys 2019. 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 | User Input | Input | shape=[1] | 1 |
| 2 | User Embed | Embedding | vocabSize=100000 | 1 ร 64 |
| 3 | User FC 1 | Linear | outFeatures=128, inFeatures=64 | 1 ร 128 |
| 4 | User ReLU | ReLU | 1 ร 128 | |
| 5 | User Tower Out | Linear | outFeatures=64, inFeatures=128 | 1 ร 64 |
| 6 | Item Input | Input | shape=[1] | 1 |
| 7 | Item Embed | Embedding | vocabSize=1000000 | 1 ร 64 |
| 8 | Item FC 1 | Linear | outFeatures=128, inFeatures=64 | 1 ร 128 |
| 9 | Item ReLU | ReLU | 1 ร 128 | |
| 10 | Item Tower Out | Linear | outFeatures=64, inFeatures=128 | 1 ร 64 |
| 11 | Item Transpose | Permute | 64 ร 1 | |
| 12 | Dot Score | MatMul | 1 ร 1 | |
| 13 | Score | Output | 1 ร 1 | |
| 14 | User Projection (owns temperature) | Contrastive Head (InfoNCE / CLIP) | 1 ร 64 | |
| 15 | Item Projection (shared temperature) | Contrastive Head (InfoNCE / CLIP) | 1 ร 64 |
What the verifier says
The same 43 single-graph checks that run on every edit in the app, on this graph.
deep-no-norm
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 ContrastiveHead(nn.Module):
"""CLIP-style projection into the shared space: bias-free, L2-normalised,
with the temperature as a learned parameter. After the normalisation a dot
product is a cosine and is bounded, which is what makes the temperature
mean anything."""
def __init__(self, d_in: int, proj_dim: int = 512,
temperature: float = 0.07, learnable_temp: bool = True,
normalize: bool = True):
super().__init__()
self.proj = nn.Linear(d_in, proj_dim, bias=False)
self.normalize = normalize
scale = torch.tensor(float(1.0 / max(temperature, 1e-6))).log()
self.logit_scale = nn.Parameter(scale) if learnable_temp else scale
def forward(self, x: torch.Tensor) -> torch.Tensor:
z = self.proj(x)
return F.normalize(z, dim=-1) if self.normalize else z
class Two_Tower(nn.Module):
def __init__(self):
super().__init__()
self.embedding_1 = nn.Embedding(100000, 64)
self.linear_1 = nn.Linear(64, 128)
self.linear_2 = nn.Linear(128, 64)
self.embedding_2 = nn.Embedding(1000000, 64)
self.linear_3 = nn.Linear(64, 128)
self.linear_4 = nn.Linear(128, 64)
self.contrastiveHead_1 = ContrastiveHead(d_in=64, proj_dim=64, temperature=0.05, learnable_temp=True, normalize=True)
self.contrastiveHead_2 = ContrastiveHead(d_in=64, proj_dim=64, temperature=0.05, learnable_temp=False, normalize=True)
def forward(self, src, tgt=None):
# User Input shape: [1]
# Item Input shape: [1]
embedding_er_emb = self.embedding_1(src)
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.