The values for n_heads, n_ctx, d_model, and d_head in ViT and Swin Transformer models are not fixed. They depend on the specific variant of the model (e.g., Base, Large, Tiny) and the input image/patch size.
Here's a breakdown for common configurations, primarily based on their original papers and popular implementations:
1. Vision Transformer (ViT)
ViT treats images as sequences of fixed-size patches. n_ctx refers to the total number of tokens (patches + CLS token) in the sequence.
d_model (Embedding Dimension): The hidden size of the transformer blocks.
n_heads (Number of Attention Heads): How many parallel attention mechanisms are used.
d_head (Head Dimension): The dimension of each individual attention head. Calculated as d_model / n_heads.
n_ctx (Context Length / Number of Tokens): For ViT, this is derived from the image size and patch size.
n_patches = (image_height / patch_height) * (image_width / patch_width)
n_ctx = n_patches + 1 (for the learnable [CLS] token)
Common ViT Variants (trained on ImageNet-1k, input 224x224, patch size 16x16):
| Variant | d_model | n_heads | d_head (d_model / n_heads) | n_ctx (224x224, 16x16 patch) |
|---|
| ViT-Base | 768 | 12 | 64 | (224/16)^2 + 1 = 14^2 + 1 = 197 |
| ViT-Large | 1024 | 16 | 64 | (224/16)^2 + 1 = 14^2 + 1 = 197 |
| ViT-Huge | 1280 | 16 | 80 | (224/16)^2 + 1 = 14^2 + 1 = 197 |
Notes on n_ctx:
- If using a different input resolution (e.g., 384x384 with 16x16 patches):
(384/16)^2 + 1 = 24^2 + 1 = 576 + 1 = 577.
- If using different patch sizes (e.g., 14x14 for 224x224):
(224/14)^2 + 1 = 16^2 + 1 = 256 + 1 = 257.
2. Swin Transformer
Swin Transformer is a hierarchical vision transformer that computes self-attention within local windows and uses shifted windows to enable cross-window connections. This means n_ctx is per window, not global.
d_model (Channel Dimension C): The base channel dimension. Swin Transformer's d_model changes across stages due to patch merging (C, 2C, 4C, 8C). The values below refer to the initial C.
n_heads (Number of Attention Heads): The number of heads also scales with d_model across stages (e.g., if d_head is constant, n_heads doubles when d_model doubles). The values below often refer to the heads in the first stage.
d_head (Head Dimension): Typically kept constant across stages, usually 32.
n_ctx (Context Length / Number of Tokens): This refers to the number of tokens within each attention window. For a typical 7x7 window, n_ctx = 7 * 7 = 49. The total number of tokens in the image changes across stages due to patch merging, but attention is always computed within these fixed-size windows.
Common Swin Transformer Variants (trained on ImageNet-1k, input 224x224, window size 7x7):
| Variant | Initial d_model (C) | n_heads (in stage 1) | d_head (across all stages) | n_ctx (per window, typically 7x7) |
|---|
| Swin-T | 96 | 3 | 32 | 7 * 7 = 49 |
| Swin-S | 96 | 3 | 32 | 7 * 7 = 49 |
| Swin-B | 128 | 4 | 32 | 7 * 7 = 49 |
| Swin-L | 192 | 6 | 32 | 7 * 7 = 49 |
Notes on Swin Transformer:
- The
n_heads listed above are for the first stage. As d_model doubles in subsequent stages (e.g., 2C, 4C, 8C), n_heads also doubles (e.g., 2x n_heads, 4x n_heads, 8x n_heads for the respective stages) to maintain a constant d_head.
- Swin Transformer's architecture is more complex; the overall number of tokens in the feature map reduces across stages, but the attention is always applied within local windows of fixed size (e.g., 7x7).
These tables provide the most common configurations, but researchers and practitioners might fine-tune these parameters for specific tasks or datasets.