# Two-Tower

> User+Item dual encoder for retrieval — embeddings → MLP per side → dot product score

Pick for retrieval at scale (billions of items) where item embeddings can be precomputed and indexed. Not suitable for re-ranking — no cross-features.

- Category: Recommendation
- Layers: 12
- Parameters: 70.43M
- Input shape (batchless): 1
- Output shape: 1 × 64
- Verifier verdict: pass
- Graph JSON: https://neurarch.com/templates/two-tower/model.json
- Open on the canvas: https://neurarch.com/?template=two-tower

## Structure

| # | 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 | Dot Score | MatMul |  | 1 × 64 |
| 12 | Score | Output |  | 1 × 64 |

## Verifier findings

- **info** `deep-no-norm`: 9 layers with no BatchNorm, LayerNorm, or GroupNorm. Without normalization, activations can explode or vanish across layers, causing slow or unstable training. Fix: Add BatchNorm after Conv2d (CV tasks), LayerNorm after attention/FFN (NLP/LLM), or GroupNorm for small batch sizes.

## Exported PyTorch (first 46 lines)

```python
# 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)
#
# WARNING: 1 layer(s) below are not yet supported by the PyTorch
# exporter and pass their input through UNCHANGED in forward():
#   - Dot Score (matmul)

import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Tuple

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)

    def forward(self, src, tgt=None):
        # User Input shape: [1]
        # Item Input shape: [1]
        embedding_er_emb = self.embedding_1(src)
        linear_er_fc1 = self.linear_1(embedding_er_emb)
        relu_r_relu = F.relu(linear_er_fc1)
        linear_er_fc2 = self.linear_2(relu_r_relu)
        embedding_em_emb = self.embedding_2(tgt)
        linear_em_fc1 = self.linear_3(embedding_em_emb)
        relu_m_relu = F.relu(linear_em_fc1)
        linear_em_fc2 = self.linear_4(relu_m_relu)
        # TODO: layer 'Dot Score' (matmul) is not yet supported by the exporter; passing through unchanged
        # Output
        return linear_er_fc2


if __name__ == '__main__':
    model = Two_Tower()
    model.eval()

    src = torch.randint(0, 1, (1))  # (batch, src_seq_len)
    tgt = torch.randint(0, 1, (1))  # (batch, tgt_seq_len)
```

## Machine access

- Every architecture: https://neurarch.com/a/index.json
- Verify a graph of your own: `POST https://www.neurarch.com/api/v1/check` (see https://neurarch.com/developer.html)
- MCP server, so an agent edits the graph with the checks in the loop: https://neurarch.com/docs/mcp.md
