# I don't understand the transformer's decoder

**URL:** <https://community.deeplearning.ai/t/i-dont-understand-the-transformers-decoder/621915>\
**Category:** Generative AI with Large Language Models\
**Tags:** week-module-1\
**Created:** [May 5, 2024, 9:48am UTC](https://community.deeplearning.ai/t/i-dont-understand-the-transformers-decoder/621915 "2024-05-05T09:48:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Goomin](https://avatars.discourse-cdn.com/v4/letter/g/91b2a8/32.png) [@Goomin](https://community.deeplearning.ai/u/Goomin)\
**Post date:** [May 5, 2024, 9:48am UTC](https://community.deeplearning.ai/t/i-dont-understand-the-transformers-decoder/621915/1 "2024-05-05T09:48:42Z")

</div>

When creating a post, please add:

- Week # must be added in the tags option of the post.
- Link to the classroom item you are referring to: [https://www.coursera.org/learn/generative-ai-with-llms/lecture/R0xbD/generating-text-with-transformers](https://www.coursera.org/learn/generative-ai-with-llms/lecture/R0xbD/generating-text-with-transformers)
- Description (include relevant info but please do not post solution code or your entire notebook):

 ![스크린샷 2024-05-05 오후 6.45.31](https://global.discourse-cdn.com/dlai/original/3X/3/e/3e5efa438e5af00ec7bd5268ae4f1dd1435244f8.png)

First of all, the encoder as I understand it is like this.

The sentence “i am a boy” goes into the input of the encoder, tokenization → vector → multihead attention → vector values change → feed forward network → token comes out with well-established context.

The encoder as I understand it is above.

And now the decoder part is the problem.

The decoder initially receives SOS as input. And I don’t quite understand the masked multi-head attention here.

At the beginning of the decoder, there is only one sos token in the masked multihead attention layer. How do mask and predict the next value?

How does masked multi-head attention occur with just one SOS token?

---

<div class="post-metadata">

**Author:** ![gent.spah](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/gent.spah/32/136423_2.png) [@gent.spah](https://community.deeplearning.ai/u/gent.spah)\
**Post date:** [July 22, 2024, 10:23am UTC](https://community.deeplearning.ai/t/i-dont-understand-the-transformers-decoder/621915/2 "2024-07-22T10:23:14Z")

</div>

I think its a good idea to check the NLP specialization, especially course 4 which explains the transformer architecture.

---

<div class="post-metadata">

**Author:** ![Deepti\_Prasad](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/deepti_prasad/32/165831_2.png) [@Deepti\_Prasad](https://community.deeplearning.ai/u/Deepti_Prasad)\
**Post date:** [July 24, 2024, 12:26pm UTC](https://community.deeplearning.ai/t/i-dont-understand-the-transformers-decoder/621915/3 "2024-07-24T12:26:18Z")

</div>

Hi @Goomin

> [@Goomin](#):
>
> At the beginning of the decoder, there is only one sos token in the masked multihead attention layer. How do mask and predict the next value?

The decoder is also composed of a stack of identical layers.  
In addition to the two sub-layers in each encoder layer, the decoder inserts a third sub-layer, which performs multi-head attention over the output of the encoder stack.  
Similar to the encoder, we employ residual connections around each of the sub-layers, followed by layer normalization.  
We also modify the self-attention sub-layer in the decoder stack to prevent positions from attending to subsequent positions.  
This masking, combined with fact that the output embeddings are offset by one position, ensures that the predictions for position i can depend only on the known outputs at positions less than i.

I am sharing the Transformer pdf. kindly go through it which explains the same part I explained you here. in case you still have doubt, feel free to ask doubt!

[Transformer.pdf](https://community.deeplearning.ai/uploads/short-url/nItXg2jZdMlR412J5Umphm4zFrF.pdf) (2.1 MB)

Regards  
DP
