Answers.Body: <p>You can conceive images as 2D or 3D sequences of pixels. In the same way, text without any preprocessing or feature extraction is just a 1D sequence of words/characters. So there is no difference at all in the way CNN learn features from images or text (or any other type of data). Of course the limitations are also the same. For example in terms of linguistic dependencies, a single convolutional layer can learn relationships between words that fall into the same kernel window. In the example below a kernel of size 3 is used, so only dependencies between words/characters that fall within that window will be captured.</p>
  <p>Regarding the comparison with transformer, there's a recent <a href="https://arxiv.org/pdf/2105.03322.pdf" rel="nofollow noreferrer">paper</a> that compare CNN with depthwise scalable convolutions and transformers, showing that when finetuning pretrained models they both share similar performances, with convolutions being better in term of training speed and sequence length scalability.</p>
  <img src="https://i.sstatic.net/73zj1.jpg" width="512" height="256"> 
  <p>As a last note, keep in mind that transformers have a big design difference compared to CNN, which is the explicit learning of positional encoding of features. A CNN learn kernels that react to specific features regardless of where they are present in an image or in a chunk of text. For example, the 2 images below are only slightly different for a CNN, since they are made of basically the same features. When analyzing text of a language where words position is a heavy grammatical constrain transformer might perform better, whereas for languages with a very relaxed positional grammar, CNN might be a better option.</p>
  <img src="https://i.sstatic.net/1mmkI.png" width="400" height="256">
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-16T08:43:41.933Z
Answers.Text: It seems that you assume that a CNN can also perform a 1d convolution and I think the OP assumes that CNNs perform only 2d convolutions, which are applicable to 2d data (like images), and that's where maybe the doubt lies. So, should probably edit your answer to address this issue of different convolutions (1d, 2d, 3d).
Answers.UserDisplayName: nbro
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-05-16T08:35:28.690Z
Answers.LastActivityDate: 2022-05-16T08:35:28.690Z
Answers.UserDisplayName: Edoardo Guerriero
Body: <p>I have read some articles where people use 1D CNN for NLP tasks like sentiment analysis. My questions are, given that CNNs are largely used for images, how/why does this work on sequences/NLP tasks? And, are there any specific CNN-based architectures that outperform transformers?</p>
  
Tags: <convolutional-neural-networks><natural-language-processing><applications>
Title: Why do Convolution Neural Networks work on NLP/sequential tasks?
UserDisplayName: cookiecutter
ThreadText: # Why do Convolution Neural Networks work on NLP/sequential tasks?
  
  (Asked by: username_0 on 2022-05-16)
  
  I have read some articles where people use 1D CNN for NLP tasks like sentiment analysis. My questions are, given that CNNs are largely used for images, how/why does this work on sequences/NLP tasks? And, are there any specific CNN-based architectures that outperform transformers?
  
  **Tags:** convolutional-neural-networks, natural-language-processing, applications
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-05-16. Score: 2
  
  You can conceive images as 2D or 3D sequences of pixels. In the same way, text without any preprocessing or feature extraction is just a 1D sequence of words/characters. So there is no difference at all in the way CNN learn features from images or text (or any other type of data). Of course the limitations are also the same. For example in terms of linguistic dependencies, a single convolutional layer can learn relationships between words that fall into the same kernel window. In the example below a kernel of size 3 is used, so only dependencies between words/characters that fall within that window will be captured.
  
  Regarding the comparison with transformer, there's a recent [paper](https://arxiv.org/pdf/2105.03322.pdf) that compare CNN with depthwise scalable convolutions and transformers, showing that when finetuning pretrained models they both share similar performances, with convolutions being better in term of training speed and sequence length scalability.
  
  ![](https://i.sstatic.net/73zj1.jpg)
  
  As a last note, keep in mind that transformers have a big design difference compared to CNN, which is the explicit learning of positional encoding of features. A CNN learn kernels that react to specific features regardless of where they are present in an image or in a chunk of text. For example, the 2 images below are only slightly different for a CNN, since they are made of basically the same features. When analyzing text of a language where words position is a heavy grammatical constrain transformer might perform better, whereas for languages with a very relaxed positional grammar, CNN might be a better option.
  
  ![](https://i.sstatic.net/1mmkI.png)
  
  > **Comments:**
  > **username_2**: It seems that you assume that a CNN can also perform a 1d convolution and I think the OP assumes that CNNs perform only 2d convolutions, which are applicable to 2d data (like images), and that's where maybe the doubt lies. So, should probably edit your answer to address this issue of different convolutions (1d, 2d, 3d).  
  
  ---
  
  