Architectures / Recommendation
🕒 SLi-Rec
Yu et al. 2019 — Short and Long-term Interest Recommender. Time-LSTM + ASVD attention fused via gate
From Yu et al. (2019). Adaptive User Modeling with Long and Short-Term Preferences for Personalized Recommendation. IJCAI 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
21 layers. Output shapes are propagated from the input shape, batch dimension excluded.
| Layer | Type | Parameters | Output shape | |
|---|---|---|---|---|
| 1 | User History (T items) | Input | shape=[50] | 50 |
| 2 | Item Embed | Embedding | vocabSize=1000000 | 50 × 32 |
| 3 | Time-LSTM (short) | LSTM | inFeatures=32, hiddenSize=64, numLayers=1 | 64 |
| 4 | ASVD Attn (long) | Self-Attention | numHeads=4 | 50 × 32 |
| 5 | to_channels | Permute | 32 × 50 | |
| 6 | Pool Long | GlobalAvgPool1D | 32 | |
| 7 | Long Proj | Linear | outFeatures=64, inFeatures=32 | 64 |
| 8 | Target Item | Input | shape=[1] | 1 |
| 9 | Target Embed | Embedding | vocabSize=1000000 | 1 × 32 |
| 10 | Flatten | Flatten | 32 | |
| 11 | Target Proj | Linear | outFeatures=64, inFeatures=32 | 64 |
| 12 | Concat [s; l; t] | Concatenate | 192 | |
| 13 | Fusion Gate σ(W·[s;l;t]) | Linear | outFeatures=64, inFeatures=192 | 64 |
| 14 | Gate σ | Sigmoid | 64 | |
| 15 | α·short + (1−α)·long | Add | 64 | |
| 16 | Concat [user, target] | Concatenate | 128 | |
| 17 | MLP 1 | Linear | outFeatures=64, inFeatures=128 | 64 |
| 18 | PReLU | PReLU | 64 | |
| 19 | Score Head | Linear | outFeatures=1, inFeatures=64 | 1 |
| 20 | Sigmoid | Sigmoid | 1 | |
| 21 | P(click) | Output | 1 |
What the verifier says
The same 43 single-graph checks that run on every edit in the app, on this graph.
output-activation
attention-no-pe
vanishing-gradient
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 SLi_Rec(nn.Module):
def __init__(self):
super().__init__()
self.embedding_1 = nn.Embedding(1000000, 32)
self.lstm_1 = nn.LSTM(32, 64, num_layers=1, batch_first=True)
self.selfAttention_1 = nn.MultiheadAttention(embed_dim=32, num_heads=4, batch_first=True)
self.linear_1 = nn.Linear(32, 64)
self.embedding_2 = nn.Embedding(1000000, 32)
self.linear_2 = nn.Linear(32, 64)
self.linear_3 = nn.Linear(192, 64)
self.linear_4 = nn.Linear(128, 64)
self.prelu_1 = nn.PReLU(num_parameters=1)
self.linear_5 = nn.Linear(64, 1)
def forward(self, src, tgt=None):
# User History (T items) shape: [50]
# Target Item shape: [1]
embedding_st_emb = self.embedding_1(src)
lstm_lstm = self.lstm_1(embedding_st_emb)[0][:, -1, :]
self_attention_g_attn = self.selfAttention_1(embedding_st_emb, embedding_st_emb, embedding_st_emb)[0]
permute_g_swap = self_attention_g_attn.permute(0, 2, 1)
global_avg_pool1d_g_pool = permute_g_swap.mean(dim=-1)
linear_ong_fc = self.linear_1(global_avg_pool1d_g_pool)
embedding_gt_emb = self.embedding_2(tgt)
flatten_t_flat = torch.flatten(embedding_gt_emb, 1)
linear_tgt_fc = self.linear_2(flatten_t_flat)
concatenate_concat = torch.cat([lstm_lstm, linear_ong_fc, linear_tgt_fc], dim=-1)
linear_gate = self.linear_3(concatenate_concat)
sigmoid_te_sig = torch.sigmoid(linear_gate)
add_fused = sigmoid_te_sig + lstm_lstm + linear_ong_fc
concatenate__final = torch.cat([add_fused, linear_tgt_fc], dim=-1)
linear_fc1 = self.linear_4(concatenate__final)
prelu_act1 = self.prelu_1(linear_fc1)
linear_fc2 = self.linear_5(prelu_act1)
sigmoid_sig = torch.sigmoid(linear_fc2)
# Output
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.