Architectures / Time-series
๐ PatchTST
Channel-independent patching + Transformer for multivariate time-series (Nie 2023)
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
19 layers. Output shapes are propagated from the input shape, batch dimension excluded.
| 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 |
What the verifier says
The same 41 structural checks that run on every edit in the app, on this graph.
No finding. Shapes propagate end to end, every divisibility condition holds, and no advisory rule fires. See the checks.
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 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
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.