# PatchTST

> Channel-independent patching + Transformer for multivariate time-series (Nie 2023)

Pick for multivariate time-series with long windows where channels can be treated independently. Same backbone serves classification and forecasting.

- Category: Time-series
- Layers: 19
- Parameters: 395.4K
- Input shape (batchless): 1 × 22 × 1000
- Output shape: 4
- Verifier verdict: pass
- Graph JSON: https://neurarch.com/templates/patch-tst/model.json
- Open on the canvas: https://neurarch.com/?template=patch-tst

## Structure

| # | Layer | Type | Parameters | Output shape |
| --- | --- | --- | --- | --- |
| 1 | ts_window | Input | shape=[1, 22, 1000] | 1 × 22 × 1000 |
| 2 | patch_embed | Patch Embed | stride=8, embedDim=128, patchSize=16 | 62 × 128 |
| 3 | pos_embed | Positional Encoding | embedDim=128, maxLen=256 | 62 × 128 |
| 4 | norm | LayerNorm | normalizedShape=128 | 62 × 128 |
| 5 | self_attn | Multi-Head Attention | embedDim=128, numHeads=16 | 62 × 128 |
| 6 | residual | Add |  | 62 × 128 |
| 7 | norm | LayerNorm | normalizedShape=128 | 62 × 128 |
| 8 | dense | Feed Forward | embedDim=128, ffDim=256 | 62 × 128 |
| 9 | residual | Add |  | 62 × 128 |
| 10 | norm | LayerNorm | normalizedShape=128 | 62 × 128 |
| 11 | self_attn | Multi-Head Attention | embedDim=128, numHeads=16 | 62 × 128 |
| 12 | residual | Add |  | 62 × 128 |
| 13 | norm | LayerNorm | normalizedShape=128 | 62 × 128 |
| 14 | dense | Feed Forward | embedDim=128, ffDim=256 | 62 × 128 |
| 15 | residual | Add |  | 62 × 128 |
| 16 | norm | LayerNorm | normalizedShape=128 | 62 × 128 |
| 17 | flatten | Flatten |  | 7936 |
| 18 | classifier | Linear | outFeatures=4 | 4 |
| 19 | logits | Output |  | 4 |

## Verifier findings

No finding. Shapes propagate end to end and no advisory rule fires.

## 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)

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

class PatchTST(nn.Module):
    def __init__(self):
        super().__init__()

        self.patchEmbed_1 = nn.Conv2d(3, 128, kernel_size=16, stride=16)  # Patch embedding (ViT-style)
        self.layerNorm_1 = nn.LayerNorm(128)
        self.multiHeadAttention_1 = nn.MultiheadAttention(embed_dim=128, num_heads=16, batch_first=True)
        self.layerNorm_2 = nn.LayerNorm(128)
        self.feedForward_1 = nn.Sequential(
            nn.Linear(128, 256),
            nn.ReLU(),
            nn.Linear(256, 128)
        )
        self.layerNorm_3 = nn.LayerNorm(128)
        self.multiHeadAttention_2 = nn.MultiheadAttention(embed_dim=128, num_heads=16, batch_first=True)
        self.layerNorm_4 = nn.LayerNorm(128)
        self.feedForward_2 = nn.Sequential(
            nn.Linear(128, 256),
            nn.ReLU(),
            nn.Linear(256, 128)
        )
        self.layerNorm_5 = nn.LayerNorm(128)
        self.linear_1 = nn.Linear(7936, 4)

    def forward(self, x):
        # ts_window shape: [1,22,1000]
        patch_embed__embed = self.patchEmbed_1(x).flatten(2).transpose(1, 2)  # [B, num_patches, embed_dim]
        # positionalEncoding: add positional encoding externally (e.g. sinusoidal or learned PE)
        layer_norm_attn_1 = self.layerNorm_1(patch_embed__embed)
        multi_head_attention_mha_1 = self.multiHeadAttention_1(layer_norm_attn_1, layer_norm_attn_1, layer_norm_attn_1)[0]
        add_attn_1 = multi_head_attention_mha_1 + patch_embed__embed
        layer_norm__ffn_1 = self.layerNorm_2(add_attn_1)
        feed_forward_ffn_1 = self.feedForward_1(layer_norm__ffn_1)
        add__ffn_1 = feed_forward_ffn_1 + add_attn_1
        layer_norm_attn_2 = self.layerNorm_3(add__ffn_1)
        multi_head_attention_mha_2 = self.multiHeadAttention_2(layer_norm_attn_2, layer_norm_attn_2, layer_norm_attn_2)[0]
        add_attn_2 = multi_head_attention_mha_2 + add__ffn_1
```

## 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
