هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
What the course covers
Why transformers work better than RNNs for text.
- Positional encoding, attention and self-attention in detail.
- How to build an encoder classifier in PyTorch.
- How decoder models such as GPT learn and generate text.
- How BERT is pretrained with masked language modeling and next sentence prediction.
- How an encoder-decoder transformer translates text.
| Module | Main focus |
|---|---|
| Module 1 · about 4 hours | Fundamental Concepts: positional encoding, attention, self-attention, encoder classification, optimization |
| Module 2 · about 6 hours | Advanced Concepts: decoder and GPT models, BERT encoder pretraining, translation |
Module 1: Fundamental Concepts of Transformer Architecture
Fundamental Concepts: positional encoding, attention, self-attention, encoder classification, optimization.
- A transformer uses attention instead of a loop, so it reads all tokens in parallel.
- Attention works with three vectors: query, key and value.
- A score says how well a query matches a key. Softmax turns the scores into weights.
- Scaled dot-product attention divides by √d_k so the numbers stay in a safe range.
- Self-attention takes Q, K and V from the same sequence, so tokens share information.
- Multi-head attention runs many heads in parallel; each head learns a different relation, then the outputs are joined.
- Attention alone has no idea of order, so we add positional encoding.
- Positional encoding can be sinusoidal (fixed) or learned during training.
- A transformer block = attention + feed-forward network + residual links + layer norm.
- The encoder stack reads the whole input, so it looks left and right at the same time.
- For classification, take the pooled (or [CLS]) vector and add a small classifier head.
- Optimization: AdamW, learning-rate warmup then decay, gradient clipping, mixed precision and gradient accumulation.
Module 2: Advanced Concepts of Transformer Architecture
Advanced Concepts: decoder and GPT models, BERT encoder pretraining, translation.
- The decoder uses masked self-attention, so a token cannot see the future.
- Causal language modeling means: predict the next token from the tokens before it.
- GPT is a decoder-only model. It is trained on a lot of text to write new text.
- Decoder training moves the labels one step, then uses cross-entropy on the next token.
- Decoder inference can use greedy search, beam search, temperature, top-k or top-p sampling.
- BERT is encoder-only and reads text in both directions.
- MLM (masked language modeling) hides about 15% of the tokens and the model learns to fill them.
- NSP (next sentence prediction) asks if sentence B comes right after sentence A.
- Special tokens: [CLS], [SEP], [MASK] and [PAD].
- BERT data prep: tokenize sentence pairs, add special tokens, mask tokens, then pad to a fixed length.
- BERT pretraining adds the MLM loss and the NSP loss together.
- In translation, the encoder reads the source and the decoder uses cross-attention on the encoder output.
- Training uses teacher forcing; inference decodes step by step and BLEU scores the result.
Check yourself
Answer each question first, then open it to check the answer.
1Which three vectors does attention use? a) input, hidden and output · b) query, key and value · c) alpha, beta and gamma · d) train, test and valid
b) query, key and value. Attention uses a query, keys and values.
2Why does scaled dot-product attention divide by √d_k? a) to remove the softmax · b) to add position · c) to keep the scores stable · d) to reduce the vocabulary
c) to keep the scores stable. The division keeps the dot products small, so softmax and gradients stay stable.
3In self-attention, where do Q, K and V come from? a) the labels · b) three different documents · c) the optimizer · d) the same input sequence
d) the same input sequence. One sequence gives all three, so every token can mix with the others.
4Why does a transformer need positional encoding? a) attention has no order by itself · b) to store the labels · c) to shrink the model · d) to speed up the GPU
a) attention has no order by itself. Attention sees a bag of tokens, so position must be added by hand.
5What is the main benefit of multi-head attention? a) it removes the feed-forward layer · b) it makes the input shorter · c) heads learn different relations in parallel · d) it needs no training
c) heads learn different relations in parallel. Each head can focus on a different pattern, for example syntax or nearby words.
6What does masked self-attention stop? a) the tokenizer from working · b) a token from seeing future tokens · c) the loss from going down · d) the model from using embeddings
b) a token from seeing future tokens. The mask blocks the right side, so the model only looks back.
7GPT is a ... model. a) encoder-only · b) encoder-decoder · c) rule-based · d) decoder-only
d) decoder-only. GPT generates text, so it has no encoder.
8Which two tasks pretrain BERT? a) BLEU and ROUGE · b) MLM and NSP · c) greedy and beam · d) CBOW and skip-gram
b) MLM and NSP. BERT learns from masked words and from sentence pairs.
9In MLM, how many tokens are usually masked? a) all tokens · b) none · c) about 15% · d) about 90%
c) about 15%. About 15% of the tokens are hidden, so the model learns to fill them.
10What does NSP ask the model? a) to count the tokens · b) to translate a sentence · c) to sort the vocabulary · d) if sentence B follows sentence A
d) if sentence B follows sentence A. The model gets two sentences and answers yes or no.
11Which token gives the classification output in BERT? a) [CLS] · b) [SEP] · c) [MASK] · d) [PAD]
a) [CLS]. The [CLS] vector is a summary of the pair, and the classifier sits on it.
12In a translation transformer, how does the decoder read the source? a) with the NSP loss · b) with cross-attention on the encoder output · c) with a new tokenizer · d) with a bigger batch
b) with cross-attention on the encoder output. Cross-attention lets each decoder step look at the encoder states.
13Which metric scores translation output? a) RMSE · b) R2 · c) accuracy only · d) BLEU
d) BLEU. BLEU compares n-grams of the output and the reference.
Study and exam tips
Say the attention recipe: query meets key, softmax gives weights, weights mix the values.
- Keep the pair: encoder = understand, decoder = generate.
- Remember BERT's two jobs: MLM and NSP.
- For decoder questions, the answer often involves "can only look back".
- Check the small numbers: √d_k scaling and about 15% masked tokens.