{"notes":[{"id":"vision-transformers/mlp-mixer","category":"vision-transformers","legacyPath":"vision-transformers/2105-mlp-mixer","legacySlug":"2105-mlp-mixer","legacyPaths":[],"title":"MLP-Mixer","subtitle":"Less Loss is Gain","author":"Z. Yuan","paperDate":"2021-05-04","publishedOn":"2024-07-05","paperTitle":"MLP-Mixer: An all-MLP Architecture for Vision","paperUrl":"https://arxiv.org/abs/2105.01601","bibliography":{"title":"MLP-Mixer: An all-MLP Architecture for Vision","authors":["Ilya Tolstikhin","Neil Houlsby","Alexander Kolesnikov","Lucas Beyer","Xiaohua Zhai","Thomas Unterthiner","Jessica Yung","Andreas Steiner","Daniel Keysers","Jakob Uszkoreit","Mario Lucic","Alexey Dosovitskiy"],"url":"https://arxiv.org/abs/2105.01601","pdfUrl":"https://arxiv.org/pdf/2105.01601","codeUrl":null,"preprint":{"id":"2105.01601","submittedOn":"2021-05-04","url":"https://arxiv.org/abs/2105.01601"},"publication":{"venue":"NeurIPS","year":2021,"kind":"conference","url":"https://papers.nips.cc/paper_files/paper/2021/hash/cba0a4ee5ccd02fda0fe3f9a3e7b89fe-Abstract.html"},"citation":"@misc{visiontransformersmlpmixer2021,\n  title = {{MLP-Mixer: An all-MLP Architecture for Vision}},\n  author = {Ilya Tolstikhin and Neil Houlsby and Alexander Kolesnikov and Lucas Beyer and Xiaohua Zhai and Thomas Unterthiner and Jessica Yung and Andreas Steiner and Daniel Keysers and Jakob Uszkoreit and Mario Lucic and Alexey Dosovitskiy},\n  year = {2021},\n  eprint = {2105.01601},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/2105.01601}\n}\n","citationPath":"/content/papers/citations/vision-transformers/mlp-mixer.81f47c46741e.bib"},"thumbnail":{"src":"/content/papers/assets/9b380c5bd3149092e175b3f465a5061e872e1783fcc2b51ae18dff9d9f274329.jpg","width":1456,"height":772},"locale":"en","path":"/content/papers/en/vision-transformers/mlp-mixer.964cefaab203.md","headings":[{"id":"problem-definition","text":"Problem Definition","depth":2},{"id":"solution","text":"Solution","depth":2},{"id":"model-architecture","text":"Model Architecture","depth":3},{"id":"discussion","text":"Discussion","depth":3},{"id":"performance-on-imagenet","text":"Performance on ImageNet","depth":3},{"id":"visualization-analysis","text":"Visualization Analysis","depth":3},{"id":"conclusion","text":"Conclusion","depth":2}],"anchorAliases":{},"guide":null,"tags":["vision-transformers","image-classification","scaling","google-brain"],"relatedReading":[{"id":"vision-transformers/poolformer","relation":"next","topic":"Separating the backbone from token mixing"}]},{"id":"vision-transformers/poolformer","category":"vision-transformers","legacyPath":"vision-transformers/2111-poolformer","legacySlug":"2111-poolformer","legacyPaths":[],"title":"PoolFormer","subtitle":"You Need a Meta!","author":"Z. Yuan","paperDate":"2021-11-22","publishedOn":"2024-07-10","paperTitle":"MetaFormer Is Actually What You Need for Vision","paperUrl":"https://openaccess.thecvf.com/content/CVPR2022/html/Yu_MetaFormer_Is_Actually_What_You_Need_for_Vision_CVPR_2022_paper.html","bibliography":{"title":"MetaFormer Is Actually What You Need for Vision","authors":["Weihao Yu","Mi Luo","Pan Zhou","Chenyang Si","Yichen Zhou","Xinchao Wang","Jiashi Feng","Shuicheng Yan"],"url":"https://openaccess.thecvf.com/content/CVPR2022/html/Yu_MetaFormer_Is_Actually_What_You_Need_for_Vision_CVPR_2022_paper.html","pdfUrl":"https://openaccess.thecvf.com/content/CVPR2022/papers/Yu_MetaFormer_Is_Actually_What_You_Need_for_Vision_CVPR_2022_paper.pdf","codeUrl":"https://github.com/sail-sg/poolformer","preprint":{"id":"2111.11418","submittedOn":"2021-11-22","url":"https://arxiv.org/abs/2111.11418"},"publication":{"venue":"CVPR","year":2022,"kind":"conference","url":"https://openaccess.thecvf.com/content/CVPR2022/html/Yu_MetaFormer_Is_Actually_What_You_Need_for_Vision_CVPR_2022_paper.html"},"citation":"@misc{visiontransformerspoolformer2021,\n  title = {{MetaFormer Is Actually What You Need for Vision}},\n  author = {Weihao Yu and Mi Luo and Pan Zhou and Chenyang Si and Yichen Zhou and Xinchao Wang and Jiashi Feng and Shuicheng Yan},\n  year = {2021},\n  eprint = {2111.11418},\n  archivePrefix = {arXiv},\n  url = {https://arxiv.org/abs/2111.11418}\n}\n","citationPath":"/content/papers/citations/vision-transformers/poolformer.f2f96629d283.bib"},"thumbnail":{"src":"/content/papers/assets/3cb141e9bb1247410c8214276b21b28ed1d536060d470406f1bfb0c88fe68df2.jpg","width":1498,"height":548},"locale":"en","path":"/content/papers/en/vision-transformers/poolformer.ceadb42af53a.md","headings":[{"id":"problem-definition","text":"Problem Definition","depth":2},{"id":"input-embedding","text":"Input Embedding","depth":3},{"id":"token-mixer","text":"Token-Mixer","depth":3},{"id":"channel-mlp","text":"Channel MLP","depth":3},{"id":"is-token-mixer-really-the-key","text":"Is Token-Mixer Really the Key?","depth":3},{"id":"solution","text":"Solution","depth":2},{"id":"model-architecture","text":"Model Architecture","depth":3},{"id":"model-configuration","text":"Model Configuration","depth":3},{"id":"discussion","text":"Discussion","depth":2},{"id":"imagenet-experiments","text":"ImageNet Experiments","depth":3},{"id":"ablation-experiments","text":"Ablation Experiments","depth":3},{"id":"conclusion","text":"Conclusion","depth":2}],"anchorAliases":{},"guide":null,"tags":["vision-transformers","image-classification","attention-mechanisms","nus"],"relatedReading":[{"id":"vision-transformers/mlp-mixer","relation":"background","topic":"Separating the backbone from token mixing"},{"id":"vision-transformers/caformer","relation":"next","topic":"Separating the backbone from token mixing"}]}],"text":"After Transformers were introduced into the field of computer vision, many studies began exploring ways to make the exchange of information between patches more efficient.\n\nThus, a new term emerged: \"Token-Mixer.\"\n\nAs the name implies, the purpose of a Token-Mixer is to mix information between different patches to enhance the performance of the model.\n\nThe traditional self-attention mechanism is one such implementation of a Token-Mixer.\n\nHowever, due to the high computational complexity of the original attention mechanism, the academic community began searching for alternatives.\n\nMLP-Mixer is one of these alternatives.\n\n\n> [!TIP]\n> \n> “Token Mixer” is a broader term adopted in later papers, not a name coined by this paper. This paper calls its own component a “token-mixing MLP.”\n\n\n## Problem Definition\n\nThe authors believe: the complexity of self-attention is too high, so let's just get rid of it!\n\nBut what should we do after getting rid of it?\n\nHey! Look, there's a simple fully connected layer by the roadside! Let's use it!\n\n~And then the paper ends here.~ (Not really!)\n\n## Solution\n\n### Model Architecture\n\n![MLP-Mixer patch embeddings, token/channel mixing and classifier; original paper Figure 1](/content/papers/assets/9b380c5bd3149092e175b3f465a5061e872e1783fcc2b51ae18dff9d9f274329.jpg \"Tolstikhin et al., MLP-Mixer, Figure 1, arXiv:2105.01601v4\")\n\nIn this paper, the authors propose using two stages of fully connected layers, or MLPs (Multi-Layer Perceptrons), to exchange information.\n\nThe image above might be hard to understand, but don't worry, let's break it down step by step:\n\n1. Input an image of size 3 x 224 x 224 and divide it into 16 x 16 patches, with a dimension assumed to be 512.\n\n   - Dimension change: [B x 3 x 224 x 224] -> [B x 512 x 14 x 14].\n\n2. Flatten the patches into a sequence. This borrows the layout of a Transformer input; the sequence is not passed through a Transformer.\n\n   - Dimension change: [B x 512 x 14 x 14] -> [B x 512 x 196].\n\n3. First stage: Exchange information between patches.\n\n   ```python\n   import torch\n   import torch.nn as nn\n   \n   # Input: [batch, channels, tokens]; an illustrative 512-channel block.\n   x = torch.randn(2, 512, 196)\n   norm_tokens = nn.LayerNorm(512)\n   patch_mixer = nn.Sequential(\n       nn.Linear(196, 256), nn.GELU(), nn.Linear(256, 196)\n   )\n   y = norm_tokens(x.transpose(1, 2)).transpose(1, 2)\n   x = x + patch_mixer(y)\n   ```\n\n4. Second stage: Exchange information between channels.\n\n   ```python\n   # Continue from the token-mixing result above.\n   norm_channels = nn.LayerNorm(512)\n   channel_mixer = nn.Sequential(\n       nn.Linear(512, 2048), nn.GELU(), nn.Linear(2048, 512)\n   )\n   y = norm_channels(x.transpose(1, 2))\n   x = x + channel_mixer(y).transpose(1, 2)\n   assert x.shape == (2, 512, 196)\n   ```\n\nThis completes one operation of the MLP-Mixer.\n\nThe second stage resembles the feed-forward network after attention in a ViT block: it mixes channels within each token, but is not self-attention itself. Each mixer uses two linear layers with GELU, pre-LayerNorm and a residual. The complete model ends with global average pooling and a classifier; this illustrative example omits dropout, patch embedding and the classifier.\n\nThe first stage uses the same learned weights across channels to mix all 196 patches, rather than computing an input-dependent QK attention map. Its fixed-position mixing makes separate position embeddings unnecessary. With the same patch size, a 196→256→196 MLP is tied to that token count; fine-tuning at a higher resolution requires adjusting the mixing weights.\n\n### Discussion\n\n### Performance on ImageNet\n\n![Pretraining cost versus ImageNet accuracy, and data size versus five-shot linear evaluation; original paper Figure 2](/content/papers/assets/1c792799228325f64046698dafc677e35c6e821de8d038e355fdd37db2b2b65d.jpg \"Tolstikhin et al., Figure 2, arXiv:2105.01601v4; cost/accuracy at left, data scale at right\")\n\n![Accuracy and TPU throughput after ImageNet-21k or JFT-300M pretraining; original paper Table 2](/content/papers/assets/0a7f36647a88658083d79c6e28801cc09e2e3631cf7b4b4b8809b7e5e697cb0a.jpg \"Tolstikhin et al., Table 2, arXiv:2105.01601v4; JFT-300M is proprietary\")\n\n- The \"ImNet\" and \"ReaL\" columns represent the results on the original ImageNet validation set and the cleaned ReaL label set, respectively.\n- Avg 5 indicates the average performance across five downstream tasks (ImageNet, CIFAR-10, CIFAR-100, Pets, Flowers).\n\n---\n\nWhen pre-trained on ImageNet-21k with additional regularization and fine-tuned at resolution 448, Mixer-L/16 achieves 84.15% top-1 accuracy on ImageNet. This is strong performance, but slightly below several comparison models in the table. Without regularization, Mixer tends to overfit, consistent with observations for ViT.\n\nWhen training MLP-Mixer from scratch on ImageNet, Mixer-B/16 scores 76.4% at resolution 224, similar to a standard ResNet50 but lower than other state-of-the-art CNN/hybrid models like BoTNet (84.7%) and NFNet (86.5%).\n\nAs the size of the upstream dataset increases, MLP-Mixer's performance improves significantly. The right-hand plot above evaluates the fixed pre-trained representations with a linear 5-shot classifier, not full fine-tuning.\n\nWith JFT-300M pretraining and fine-tuning at 448 resolution, Mixer-H/14 reaches 87.94% top-1. Table values for BiT-R152x4 and ViT-H/14 are 87.54% and 88.55%: differences of +0.40 and −0.61 percentage points. TPU-v3 throughput per core is 40, 26 and 15 img/s respectively, making Mixer about 2.67× ViT and 1.54× BiT in this benchmark, not universally across hardware.\n\nIn terms of precision-computation trade-offs, Mixer is competitive with more traditional neural network architectures. There is a clear correlation between total pre-training cost and downstream accuracy across architecture categories.\n\n### Visualization Analysis\n\n![Hidden-unit weights of the first three token-mixing MLPs; original paper Figure 5](/content/papers/assets/5a7d9729f4d666ac9fd86d08cf4dad53037ec56f0bf28cebb7001537c1f9ab78.jpg \"Tolstikhin et al., Figure 5, arXiv:2105.01601v4; weights reshaped to 14×14, not input activations\")\n\nIt is generally observed that the first layer of CNNs tends to learn Gabor-like detectors, which act on local regions of the image pixels.\n\nIn contrast, MLP-Mixer allows global information exchange between tokens in the MLP, raising the question:\n\n- Does it process information in a similar way?\n\nThe figure visualizes hidden-unit weights of the first three token-mixing MLPs in Mixer-B/16 trained on JFT-300M. Each unit’s 196 weights are reshaped into a 14×14 grid. These are not activation maps produced by a particular input image.\n\nIn the image above, from left to right, are the first, second, and third layers of MLP-Mixer. We can see:\n\n- Some learned features act on the entire image, while others act on smaller regions.\n- Deeper layers seem to have less distinguishable structures.\n- Similar to CNNs, there are many feature detectors with opposite phases.\n\n## Conclusion\n\nLosing less counts as a gain!\n\nReplacing the original self-attention with fixed-width token mixing can save a great deal of computation. Even if its results are a little worse, there is still a market for it!\n","imageSizes":{"/content/papers/assets/9b380c5bd3149092e175b3f465a5061e872e1783fcc2b51ae18dff9d9f274329.jpg":{"width":1456,"height":772},"/content/papers/assets/1c792799228325f64046698dafc677e35c6e821de8d038e355fdd37db2b2b65d.jpg":{"width":1444,"height":560},"/content/papers/assets/0a7f36647a88658083d79c6e28801cc09e2e3631cf7b4b4b8809b7e5e697cb0a.jpg":{"width":1224,"height":696},"/content/papers/assets/5a7d9729f4d666ac9fd86d08cf4dad53037ec56f0bf28cebb7001537c1f9ab78.jpg":{"width":1852,"height":606}}}
