Answers.Body: <p>The encoder-decoder architecture is typically employed to work with two different sequences: the input sequence and the output sequence. The presence of both depends on the task; for machine translation, the transformer encoder processes the text in the source language, and the decoder generates the corresponding text in the destination language. A generative model, such as GPT, could also consist of only the decoder component.</p>
  <p>I will focus on encoder-decoder architectures.</p>
  <p>In the original implementation, once the encoder obtains the contextual representations <code>[C1, C2, C3, C4]</code>, these are considered fixed inputs for the decoder. In the simplest case, the decoder should produce a sequence of indices corresponding to words in the vocabulary as its output. Therefore, the target sequence should be a sequence of indices/words, not word embeddings.</p>
  <p>The decoder obtains two inputs at each step: the encoder output (fixed) and the partially generated sequence. I will use <code>E(x)</code> to refer to the word embedding of the word <code>x</code>. The step-by-step generation (during inference) will be:</p>
  <p>Step 1:</p>
  <pre><code>Inputs:
  [C1, C2, C3, C4] (encoder output)
  [E(&lt;start&gt;)] (partial decoder output)
  
  Outputs:
  [&quot;the&quot;]
  </code></pre>
  <p>Step 2:</p>
  <pre><code>Inputs:
  [C1, C2, C3, C4] (encoder output)
  [E(&lt;start&gt;), E(the)] (partial decoder output)
  
  Outputs:
  [*, &quot;cat&quot;]
  </code></pre>
  <p>Step 3:</p>
  <pre><code>Inputs:
  [C1, C2, C3, C4] (encoder output)
  [E(&lt;start&gt;), E(the), E(cat)] (partial decoder output)
  
  Outputs:
  [*, *, &quot;eats&quot;]
  </code></pre>
  <p>Step 4:</p>
  <pre><code>Inputs:
  [C1, C2, C3, C4] (encoder output)
  [E(&lt;start&gt;), E(the), E(cat), E(eats)] (partial decoder output)
  
  Outputs:
  [*, *, *, &quot;&lt;end&gt;&quot;]
  </code></pre>
  <p>At each step, the decoder can perform masked self-attention (to attend to the previous tokens of its partial output) and cross-attention over the entire encoder output sequence.</p>
  <p>The output of a single step isn't actually a word; instead, it is a probability distribution over the word vocabulary. This distribution can be used to sample a word according to the probabilities, or we can simply choose the word that corresponds to the argmax of the distribution. Furthermore, more complex techniques such as beam search can be used to generate better sequences. Once the word is chosen, it is appended to the partial decoder output and used in the subsequent step. The asterisks correspond to the outputs of the decoder for previous timesteps, which are no longer interesting.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-04T16:32:12.167Z
Answers.Text: "The encoder obtains two inputs at each step:" It should be "The decoder obtains two inputs at each step:"
Answers.UserDisplayName: Steven
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-05T22:46:33.540Z
Answers.Text: @NicolaFanelli Nice answer. Voted! Anche se potrei rispondere in italiano perchè siamo entrambi del sud
Answers.UserDisplayName: Zollikofen4
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-06T15:05:22.970Z
Answers.Text: Grazie! @Frimand
Answers.UserDisplayName: Nicola Fanelli
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-12-04T15:54:57.307Z
Answers.LastActivityDate: 2023-12-05T11:51:59.780Z
Answers.LastEditDate: 2023-12-05T11:51:59.780Z
Answers.UserDisplayName: Nicola Fanelli
Body: <p><strong>Encoder:</strong></p>
  <pre><code>Input: [A, B, C, D] (word embeddings)
  Output: [C1, C2, C3, C4] (contextual representations)
  The encoder processes the input sequence [A, B, C, D] and generates contextual representations [C1, C2, C3, C4]. The specific calculations involved in the encoder, such as self-attention and feed-forward layers, are not shown in this example.
  </code></pre>
  <p><strong>Decoder:</strong></p>
  <pre><code>Input: [C1, C2, C3, C4] (contextual representations)
  Output: [A', B', C', D'] (word embeddings)
  The decoder takes the contextual representations [C1, C2, C3, C4] as input and generates word embeddings [A', B', C', D'] for the output sequence. The decoder generates one word embedding at a time, conditioned on the previously generated embeddings and the contextual representations.
  </code></pre>
  <p>Let's illustrate the decoding process step by step:</p>
  <pre><code>Step 1:
  
  Input: [C1] (contextual representation of the &lt;start&gt; token)
  Output: [A'] (word embedding for the first output word)
  Step 2:
  
  Input: [CA', C2] (contextual representations and word embedding generated so far)
  Output: [B'] (word embedding for the second output word)
  Step 3:
  
  Input: [A', B',C3] (contextual representations and word embeddings generated so far)
  Output: [C'] (word embedding for the third output word)
  Step 4:
  
  Input: [A', B', C',C4] (contextual representations and word embeddings generated so far)
  Output: [D'] (word embedding for the fourth output word)
  </code></pre>
  
Tags: <transformer><decoder>
Title: Does a decoder in transformer model generate output embeddings like the following?
UserDisplayName: Steven
ThreadText: # Does a decoder in transformer model generate output embeddings like the following?
  
  (Asked by: username_0 on 2023-12-04)
  
  Encoder:
  Input: [A, B, C, D] (word embeddings)
  Output: [C1, C2, C3, C4] (contextual representations)
  The encoder processes the input sequence [A, B, C, D] and generates contextual representations [C1, C2, C3, C4]. The specific calculations involved in the encoder, such as self-attention and feed-forward layers, are not shown in this example.
  
  Decoder:
  Input: [C1, C2, C3, C4] (contextual representations)
  Output: [A', B', C', D'] (word embeddings)
  The decoder takes the contextual representations [C1, C2, C3, C4] as input and generates word embeddings [A', B', C', D'] for the output sequence. The decoder generates one word embedding at a time, conditioned on the previously generated embeddings and the contextual representations.
  
  Let's illustrate the decoding process step by step:
  Step 1:
  
  Input: [C1] (contextual representation of the  token)
  Output: [A'] (word embedding for the first output word)
  Step 2:
  
  Input: [CA', C2] (contextual representations and word embedding generated so far)
  Output: [B'] (word embedding for the second output word)
  Step 3:
  
  Input: [A', B',C3] (contextual representations and word embeddings generated so far)
  Output: [C'] (word embedding for the third output word)
  Step 4:
  
  Input: [A', B', C',C4] (contextual representations and word embeddings generated so far)
  Output: [D'] (word embedding for the fourth output word)
  
  **Tags:** transformer, decoder
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2023-12-04. Score: 3
  
  The encoder-decoder architecture is typically employed to work with two different sequences: the input sequence and the output sequence. The presence of both depends on the task; for machine translation, the transformer encoder processes the text in the source language, and the decoder generates the corresponding text in the destination language. A generative model, such as GPT, could also consist of only the decoder component.
  I will focus on encoder-decoder architectures.
  In the original implementation, once the encoder obtains the contextual representations [C1, C2, C3, C4], these are considered fixed inputs for the decoder. In the simplest case, the decoder should produce a sequence of indices corresponding to words in the vocabulary as its output. Therefore, the target sequence should be a sequence of indices/words, not word embeddings.
  The decoder obtains two inputs at each step: the encoder output (fixed) and the partially generated sequence. I will use E(x) to refer to the word embedding of the word x. The step-by-step generation (during inference) will be:
  Step 1:
  Inputs:
  [C1, C2, C3, C4] (encoder output)
  [E()] (partial decoder output)
  
  Outputs:
  ["the"]
  
  Step 2:
  Inputs:
  [C1, C2, C3, C4] (encoder output)
  [E(), E(the)] (partial decoder output)
  
  Outputs:
  [*, "cat"]
  
  Step 3:
  Inputs:
  [C1, C2, C3, C4] (encoder output)
  [E(), E(the), E(cat)] (partial decoder output)
  
  Outputs:
  [*, *, "eats"]
  
  Step 4:
  Inputs:
  [C1, C2, C3, C4] (encoder output)
  [E(), E(the), E(cat), E(eats)] (partial decoder output)
  
  Outputs:
  [*, *, *, ""]
  
  At each step, the decoder can perform masked self-attention (to attend to the previous tokens of its partial output) and cross-attention over the entire encoder output sequence.
  The output of a single step isn't actually a word; instead, it is a probability distribution over the word vocabulary. This distribution can be used to sample a word according to the probabilities, or we can simply choose the word that corresponds to the argmax of the distribution. Furthermore, more complex techniques such as beam search can be used to generate better sequences. Once the word is chosen, it is appended to the partial decoder output and used in the subsequent step. The asterisks correspond to the outputs of the decoder for previous timesteps, which are no longer interesting.
  
  > **Comments:**
  > **username_0**: "The encoder obtains two inputs at each step:" It should be "The decoder obtains two inputs at each step:"  
  > **username_2**: @NicolaFanelli Nice answer. Voted! Anche se potrei rispondere in italiano perchè siamo entrambi del sud  
  > **username_1**: Grazie! @Frimand  
  
  ---
  
  