Architectures / Recommendation
๐งฎ Neural Collaborative Filtering
He et al. 2017 NCF โ user/item embeddings โ concat โ MLP โ score (concat-then-MLP variant)
From He et al. (2017). Neural Collaborative Filtering. WWW 2017. 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
12 layers. Output shapes are propagated from the input shape, batch dimension excluded.
| Layer | Type | Parameters | Output shape | |
|---|---|---|---|---|
| 1 | User ID | Input | shape=[1] | 1 |
| 2 | User Embedding | Embedding | vocabSize=100000 | 1 ร 32 |
| 3 | Item ID | Input | shape=[1] | 1 |
| 4 | Item Embedding | Embedding | vocabSize=1000000 | 1 ร 32 |
| 5 | Concat [u; i] | Concatenate | 1 ร 64 | |
| 6 | MLP 1 | Linear | outFeatures=64, inFeatures=64 | 1 ร 64 |
| 7 | ReLU 1 | ReLU | 1 ร 64 | |
| 8 | MLP 2 | Linear | outFeatures=32, inFeatures=64 | 1 ร 32 |
| 9 | ReLU 2 | ReLU | 1 ร 32 | |
| 10 | Score Head | Linear | outFeatures=1, inFeatures=32 | 1 ร 1 |
| 11 | Sigmoid | Sigmoid | 1 ร 1 | |
| 12 | P(click) | Output | 1 ร 1 |
What the verifier says
The same 43 single-graph 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 NeuralCollaborativeFiltering(nn.Module):
def __init__(self):
super().__init__()
self.embedding_1 = nn.Embedding(100000, 32)
self.embedding_2 = nn.Embedding(1000000, 32)
self.linear_1 = nn.Linear(64, 64)
self.linear_2 = nn.Linear(64, 32)
self.linear_3 = nn.Linear(32, 1)
def forward(self, src, tgt=None):
# User ID shape: [1]
# Item ID shape: [1]
embedding_er_emb = self.embedding_1(src)
embedding_em_emb = self.embedding_2(tgt)
concatenate_concat = torch.cat([embedding_er_emb, embedding_em_emb], dim=-1)
linear_fc1 = self.linear_1(concatenate_concat)
relu_act1 = F.relu(linear_fc1)
linear_fc2 = self.linear_2(relu_act1)
relu_act2 = F.relu(linear_fc2)
linear_fc3 = self.linear_3(relu_act2)
sigmoid_sig = torch.sigmoid(linear_fc3)
# Output
return sigmoid_sig
if __name__ == '__main__':
model = NeuralCollaborativeFiltering()
model.eval()
src = torch.randint(0, 100000, (1, 1)) # User ID
tgt = torch.randint(0, 1000000, (1, 1)) # Item ID
with torch.no_grad():
output = model(src, tgt)
print(f'src shape : {tuple(src.shape)}')
print(f'tgt shape : {tuple(tgt.shape)}')
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.