Architectures / Recommendation
๐ธ GraphSAGE Recommender
Inductive node embeddings via neighbor sampling + aggregation โ for graph-based recommenders (PinSage style)
From Hamilton et al. (2017). Inductive Representation Learning on Large Graphs. NeurIPS 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
10 layers. Output shapes are propagated from the input shape, batch dimension excluded.
| Layer | Type | Parameters | Output shape | |
|---|---|---|---|---|
| 1 | Node Features | Input | shape=[128] | 128 |
| 2 | SAGE Layer 1 | GraphSAGE | outChannels=256, inChannels=128 | 128 ร 64 |
| 3 | ReLU | ReLU | 128 ร 64 | |
| 4 | Dropout | Dropout | p=0.2 | 128 ร 64 |
| 5 | SAGE Layer 2 | GraphSAGE | outChannels=256, inChannels=256 | 128 ร 64 |
| 6 | ReLU | ReLU | 128 ร 64 | |
| 7 | Dropout | Dropout | p=0.2 | 128 ร 64 |
| 8 | SAGE Layer 3 | GraphSAGE | outChannels=128, inChannels=256 | 128 ร 64 |
| 9 | LayerNorm | LayerNorm | normalizedShape=[128] | 128 ร 64 |
| 10 | Node Embedding | Output | 128 ร 64 |
What the verifier says
The same 43 single-graph checks that run on every edit in the app, on this graph.
bn-at-output
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
from torch_geometric.nn import GCNConv, GATConv, SAGEConv
class GraphSAGERecommender(nn.Module):
def __init__(self):
super().__init__()
self.graphSAGE_1 = SAGEConv(128, 64)
self.dropout_1 = nn.Dropout(p=0.2)
self.graphSAGE_2 = SAGEConv(64, 64)
self.dropout_2 = nn.Dropout(p=0.2)
self.graphSAGE_3 = SAGEConv(64, 64)
self.layerNorm_1 = nn.LayerNorm(64)
def forward(self, x, edge_index=None):
# Node Features shape: [128]
graph_s_a_g_e_sage1 = self.graphSAGE_1(x, edge_index)
relu_relu1 = F.relu(graph_s_a_g_e_sage1)
dropout_drop1 = self.dropout_1(relu_relu1)
graph_s_a_g_e_sage2 = self.graphSAGE_2(dropout_drop1, edge_index)
relu_relu2 = F.relu(graph_s_a_g_e_sage2)
dropout_drop2 = self.dropout_2(relu_relu2)
graph_s_a_g_e_sage3 = self.graphSAGE_3(dropout_drop2, edge_index)
layer_norm_norm = self.layerNorm_1(graph_s_a_g_e_sage3)
# Output
return layer_norm_norm
if __name__ == '__main__':
model = GraphSAGERecommender()
model.eval()
edge_index = torch.tensor([[0, 1, 2, 3], [1, 2, 3, 0]], dtype=torch.long) # (2, num_edges) COO
x = torch.randn(4, 128) # (num_nodes, in_features)
with torch.no_grad():
output = model(x, edge_index)
print(f'Input shape : {tuple(x.shape)}')
print(f'Output shape : {tuple(output.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.