epfml/attention-cnn
epfml/attention-cnn has 1,122 GitHub stars and 127 forks. Star changes are not measured yet. Last checked 2026-11-09 09:34 UTC. Last push 2023-01-10.
Source code for "On the Relationship between Self-Attention and Convolutional Layers"
Measured star growth
- in 24h
- Update delayed
- in 7 days
- Update delayed
- in 15 days
- Update delayed
- in 1 month
- Update delayed
Net star changes over overlapping periods, not daily totals. Missing measurements are left blank. Stars measure attention, not software quality.
README.md
OriginalSelf-Attention and Convolution
The code accompanies the paper On the Relationship between Self-Attention and Convolutional Layers by Jean-Baptiste Cordonnier, Andreas Loukas and Martin Jaggi that appeared in ICLR 2020.
Abstract
Recent trends of incorporating attention mechanisms in vision have led researchers to reconsider the supremacy of convolutional layers as a primary building block. Beyond helping CNNs to handle long-range dependencies, Ramachandran et al. (2019) showed that attention can completely replace convolution and achieve state-of-the-art performance on vision tasks. This raises the question: do learned attention layers operate similarly to convolutional layers? This work provides evidence that attention layers can perform convolution and, indeed, they often learn to do so in practice. Specifically, we prove that a multi-head self-attention layer with sufficient number of heads is at least as powerful as any convolutional layer. Our numerical experiments then show that the phenomenon also occurs in practice, corroborating our analysis. Our code is publicly available.
Interact with Attention
Check out our interactive website.
Reproduce
To run our code on a Ubuntu machine with a GPU, install the Python packages in a fresh Anaconda environment:
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
pip install -r requirements.txt
All experiments presented in the paper are reproducible by running the scripts in runs/, for example:
bash runs/quadratic/run.sh
Reference
If you use this code, please cite the following paper:
@inproceedings{
Cordonnier2020On,
title={On the Relationship between Self-Attention and Convolutional Layers},
author={Jean-Baptiste Cordonnier and Andreas Loukas and Martin Jaggi},
booktitle={International Conference on Learning Representations},
year={2020},
url={https://openreview.net/forum?id=HJlnC1rKPB}
}
Explore related projects
codecrafters-io/build-your-own-x
Master programming by recreating your favorite technologies from scratch.
561,607 starspublic-apis/public-apis
A collective list of free APIs
497,930 starsmorluto/rea
Reverse engineer anything with agents, from app behavior down to native binaries.
449,872 starsopenclaw/openclaw
The AI that really does things. Any OS. Any Platform. The lobster way. 🦞
394,297 stars