Answers.Body: <p>It seems that in the reference you provided they use attention to compute weights for the encoder representations based on the context provided by the decoder (Fig. 1). As far as I can tell, attention is applied <em>after</em> convolution (in fact, after the GLU step), so it does not affect the feature maps directly. Rather, attention is used to select the target words in the decoder. In other papers (e.g., <a href="http://www.aclweb.org/anthology/Q16-1019" rel="nofollow noreferrer">this one</a>), attention is applied directly to the feature maps in a way that is more similar to what you described.</p>
  
  <p>Regarding your second question, the paper you reference actually provides a <a href="https://github.com/facebookresearch/fairseq" rel="nofollow noreferrer">link</a> to the source code they used. It is written in Lua (using Torch) instead of PyTorch, probably because PyTorch development was just starting when the paper was published. At any rate, you should be able to follow the Lua code and translate it into PyTorch.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-28T23:15:25.607Z
Answers.LastActivityDate: 2019-01-28T23:15:25.607Z
Answers.UserDisplayName: cantordust
Body: <p>Attention has been used widely in recurrent networks to weight feature representations learned by the model. This is not a trivial task since recurrent networks have a <em>hidden state</em> that captures sequence information. The hidden state can be fed into a small MLP that produces a context vector summarizing the salient features of the hidden state.</p>
  <p>In the context of NLP, convolutional networks are not as straightforward. They have the notion of <em>channels</em> that are different feature representations of the input, but are channels the equivalent to hidden states? Particularly, this raises two questions for me:</p>
  <ul>
  <li><p>Why use attention in convolutional networks at all? Convolutions have shown to be adept feature detectors––for example, it is known that higher layers learn small features such as edges while lower layers learn more abstract representations. Would attention be used to sort through and weigh these features?</p>
  </li>
  <li><p>In practice, how would attention be applied to convolutional networks? The output of these networks is usually <code>(batch, channels, input_size)</code> (at least in PyTorch), so how would the attention operations in recurrent networks be applied to the output of convolutional networks?</p>
  </li>
  </ul>
  <hr />
  <p><strong>References</strong></p>
  <p><a href="https://arxiv.org/pdf/1705.03122.pdf" rel="nofollow noreferrer"><em>Convolutional Sequence to Sequence Learning</em></a>, Jonas Gehring, Michael Auli, David Grangier, Denis Yarats, Yann N. Dauphin, 2017</p>
  
Tags: <deep-learning><convolutional-neural-networks><recurrent-neural-networks><attention>
Title: Why would we use attention in convolutional neural networks, and how would we apply it?
UserDisplayName: Shrey
ThreadText: # Why would we use attention in convolutional neural networks, and how would we apply it?
  
  (Asked by: username_0 on 2019-01-17)
  
  Attention has been used widely in recurrent networks to weight feature representations learned by the model. This is not a trivial task since recurrent networks have a *hidden state* that captures sequence information. The hidden state can be fed into a small MLP that produces a context vector summarizing the salient features of the hidden state.
  
  In the context of NLP, convolutional networks are not as straightforward. They have the notion of *channels* that are different feature representations of the input, but are channels the equivalent to hidden states? Particularly, this raises two questions for me:
  
  * Why use attention in convolutional networks at all? Convolutions have shown to be adept feature detectors––for example, it is known that higher layers learn small features such as edges while lower layers learn more abstract representations. Would attention be used to sort through and weigh these features?
  * In practice, how would attention be applied to convolutional networks? The output of these networks is usually `(batch, channels, input_size)` (at least in PyTorch), so how would the attention operations in recurrent networks be applied to the output of convolutional networks?
  
  ---
  
  **References**
  
  [*Convolutional Sequence to Sequence Learning*](https://arxiv.org/pdf/1705.03122.pdf), Jonas Gehring, Michael Auli, David Grangier, Denis Yarats, Yann N. Dauphin, 2017
  
  **Tags:** deep-learning, convolutional-neural-networks, recurrent-neural-networks, attention
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-28. Score: 2
  
  It seems that in the reference you provided they use attention to compute weights for the encoder representations based on the context provided by the decoder (Fig. 1). As far as I can tell, attention is applied *after* convolution (in fact, after the GLU step), so it does not affect the feature maps directly. Rather, attention is used to select the target words in the decoder. In other papers (e.g., [this one](http://www.aclweb.org/anthology/Q16-1019)), attention is applied directly to the feature maps in a way that is more similar to what you described.
  
  Regarding your second question, the paper you reference actually provides a [link](https://github.com/facebookresearch/fairseq) to the source code they used. It is written in Lua (using Torch) instead of PyTorch, probably because PyTorch development was just starting when the paper was published. At any rate, you should be able to follow the Lua code and translate it into PyTorch.
  
  
  ---
  
  