Answers.Body: <p><strong>Masking</strong></p>
  <p>The purpose of masking in the decoder is to prevent the model from having access to future tokens when predicting the current token in the output sequence. During training, the model is given the entire target sequence as input, but it should not be allowed to use information from future tokens to predict the current token, as this would violate the autoregressive nature of the decoding process.</p>
  <p>The decoder should only have access to the leftward (previous) context when generating a translation. If masking were not used, the model would be able to &quot;cheat&quot; by leveraging information from future tokens, which would not be available during inference or when generating translations in a real-world scenario. This would lead to a model that performs well on training data but fails to generalize to new, unseen data.</p>
  <p><strong>Alternative Approaches</strong></p>
  <p>As for models that do not use masking in the decoder, most sequence-to-sequence models rely on some form of autoregressive decoding, either through masking or through other mechanisms. For example, traditional RNN-based encoder-decoder models do not use explicit masking, as they generate the output sequence one token at a time, inherently relying only on the previous context. However, there are non-autoregressive models that predict the entire output sequence in parallel, which do not use masking in the same way as autoregressive models. These non-autoregressive models can be faster but often sacrifice some accuracy compared to their autoregressive counterparts.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-04-05T09:07:28.577Z
Answers.LastActivityDate: 2023-04-05T09:07:28.577Z
Answers.UserDisplayName: Hans-Peter Schrei
Body: <p>I am trying to understand the purpose of masking in the decoder in the <a href="https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf" rel="nofollow noreferrer">&quot;Attention is All you Need&quot;</a> paper. Why wouldn't we want to rely on bidirectional context when translating? What goes wrong if masking isn't used in the decoder? Are there successful models in which the decoder doesn't use masking? Thank you!</p>
  
Tags: <deep-learning><natural-language-processing><transformer><attention><machine-translation>
Title: Machine Translation Transformers: Why Mask in Decoder?
UserDisplayName: gumbelgrumbelgumbel
ThreadText: # Machine Translation Transformers: Why Mask in Decoder?
  
  (Asked by: username_0 on 2023-04-05)
  
  I am trying to understand the purpose of masking in the decoder in the ["Attention is All you Need"](https://proceedings.neurips.cc/paper_files/paper/2017/file/3f5ee243547dee91fbd053c1c4a845aa-Paper.pdf) paper. Why wouldn't we want to rely on bidirectional context when translating? What goes wrong if masking isn't used in the decoder? Are there successful models in which the decoder doesn't use masking? Thank you!
  
  **Tags:** deep-learning, natural-language-processing, transformer, attention, machine-translation
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-04-05. Score: 2
  
  **Masking**
  
  The purpose of masking in the decoder is to prevent the model from having access to future tokens when predicting the current token in the output sequence. During training, the model is given the entire target sequence as input, but it should not be allowed to use information from future tokens to predict the current token, as this would violate the autoregressive nature of the decoding process.
  
  The decoder should only have access to the leftward (previous) context when generating a translation. If masking were not used, the model would be able to "cheat" by leveraging information from future tokens, which would not be available during inference or when generating translations in a real-world scenario. This would lead to a model that performs well on training data but fails to generalize to new, unseen data.
  
  **Alternative Approaches**
  
  As for models that do not use masking in the decoder, most sequence-to-sequence models rely on some form of autoregressive decoding, either through masking or through other mechanisms. For example, traditional RNN-based encoder-decoder models do not use explicit masking, as they generate the output sequence one token at a time, inherently relying only on the previous context. However, there are non-autoregressive models that predict the entire output sequence in parallel, which do not use masking in the same way as autoregressive models. These non-autoregressive models can be faster but often sacrifice some accuracy compared to their autoregressive counterparts.
  
  
  ---
  
  