On the Relationship between Self-Attention and Convolutional Layers

Jean-Baptiste Cordonnier, Andreas Loukas, Martin Jaggi
11/8/2019
cs.LGcs.CLcs.CV
transformer-attention

Official code: epfml/attention-cnn (1,123 stars, Python, Apache-2.0, last commit 2023-01-10).

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.

Show full abstract

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 expressive as any convolutional layer. Our numerical experiments then show that self-attention layers attend to pixel-grid patterns similarly to CNN layers, corroborating our analysis. Our code is publicly available.

View on arXivView PDF

Code Implementations(1)

Apache-2.0

Source code for "On the Relationship between Self-Attention and Convolutional Layers"

1,123127Python, ShellJun 25, 20193 years agoApache-2.0

Similar Papers

Cite this paper

@article{cordonnier2019relationship,
  title  = {On the Relationship between Self-Attention and Convolutional Layers},
  author = {Jean-Baptiste Cordonnier and Andreas Loukas and Martin Jaggi},
  year   = {2019},
  eprint = {1911.03584},
  archivePrefix = {arXiv},
  url    = {https://arxiv.org/abs/1911.03584}
}

Add a badge to your README

Shows whether official code is linked for this paper.

Code of Paper badge
[![Code of Paper](https://codeofpaper.com/badge/1911.03584.svg)](https://codeofpaper.com/paper/1911.03584)

Discussion