DEEP LEARNING LESSON 13 TRANSFORMERS

Transformers and Modern AI

Transformers changed modern AI by providing an architecture that can efficiently understand relationships between tokens and process large amounts of sequence data.

1. AI Before Transformers

Before Transformers became popular, sequence problems were commonly handled using architectures such as RNNs and LSTMs.

Text
 ↓
RNN
 ↓
RNN
 ↓
RNN
 ↓
Output

The model processed sequence information step by step.

This created difficulties when sequences became long. Information from earlier tokens could become harder to preserve.

2. What Did Transformers Change?

Transformers introduced attention as the central mechanism for processing relationships between tokens.

RNN:

Token 1
  ↓
Token 2
  ↓
Token 3
  ↓
Token 4


Transformer:

Token 1 ─┐
Token 2 ─┤
Token 3 ─┼──→ Self-Attention
Token 4 ─┘

Instead of relying only on a sequential hidden state, self-attention allows the model to consider relationships between tokens.

3. Why Transformers Matter

Transformers became important for several reasons.

Transformers
     ↓
Self-Attention
     ↓
Better relationship modeling
     ↓
Parallel processing during training
     ↓
Large-scale training
     ↓
Powerful AI models

The important idea is that Transformers work especially well when combined with large datasets, large models, and large amounts of computing.

4. Transformers and Language Models

Transformers became extremely important in natural language processing.

A language model learns patterns in text and can use those learned patterns to predict tokens.

The sky is

→ blue

The model receives the context and predicts what token is likely to come next.

The sky is
     ↓
Transformer
     ↓
Probability distribution
     ↓
blue

5. Next-Token Prediction

One important training approach for modern language models is next-token prediction.

Consider:

I am learning

The model attempts to predict the next token.

I am learning
       ↓
Python

After adding the predicted token, the model can continue:

I am learning Python
          ↓
because

Then:

I am learning Python because
              ↓
...

This process can be repeated to generate a sequence.

6. How Chat AI Uses Transformers

A simplified view of a conversational AI system is:

User Input
    ↓
Tokenization
    ↓
Token Embeddings
    ↓
Transformer Layers
    ↓
Output Probabilities
    ↓
Next Token
    ↓
Next Token
    ↓
Next Token
    ↓
Generated Response

The actual systems are much more complicated than this simplified diagram.

7. Example — Asking an AI a Question

Suppose the user asks:

What is Python?

The text is converted into tokens.

["What", "is", "Python", "?"]

Those tokens are converted into numerical representations and processed by Transformer layers.

Tokens
   ↓
Embeddings
   ↓
Transformer
   ↓
Attention
   ↓
Contextual representation
   ↓
Output probabilities
   ↓
Generated answer

8. Understanding Context

One of the major strengths of attention is its ability to model relationships between tokens in context.

Consider:

I went to the bank to deposit money.

Here, the surrounding words provide clues about what "bank" means.

Compare it with:

I sat beside the river bank.

The surrounding context is different, so the relevant meaning is different.

Attention helps the model use surrounding context when constructing token representations.

9. Scaling Transformers

Transformers became especially powerful when researchers discovered that increasing model and training scale could produce substantial improvements.

More Data
   +
Larger Model
   +
More Compute
   ↓
Potentially More Capable Model

This is one reason modern AI systems can contain very large numbers of parameters and be trained on enormous datasets.

However, simply making a model larger does not guarantee that it will be useful, reliable, or accurate.

10. What Are Parameters?

Parameters are learned numerical values inside a neural network.

Training Data
     ↓
Model
     ↓
Adjust Parameters
     ↓
Lower Error
     ↓
Learned Model

A simplified example:

weight = 0.42

During training, values like these are adjusted using optimization algorithms.

A modern Transformer can contain a very large number of learned parameters.

11. Pretraining

A language model is commonly pretrained on a large collection of text.

Large Text Dataset
       ↓
Tokenization
       ↓
Transformer
       ↓
Prediction
       ↓
Calculate Loss
       ↓
Update Parameters
       ↓
Repeat
       ↓
Pretrained Model

During training, the model repeatedly makes predictions, calculates errors, and updates its parameters.

12. Fine-Tuning

A pretrained model can be further trained for a specific task or behavior.

Pretrained Model
       ↓
Task-Specific Data
       ↓
Additional Training
       ↓
Fine-Tuned Model

For example, a general language model could be adapted for a particular domain or task.

13. Instruction Tuning

Another important idea in modern AI is training models to better follow instructions.

User:
Explain photosynthesis simply.

       ↓

Model:
Produces an explanation
that follows the instruction.

This is different from simply learning to predict text. Additional training can teach a model to respond in ways that better match desired instructions.

14. Transformers Are Not Only for Text

Transformers are now used in many areas of AI.

Transformers
     │
     ├── Text
     │
     ├── Images
     │
     ├── Audio
     │
     ├── Video
     │
     └── Multimodal AI

The basic idea remains similar: represent information as tokens or token-like elements and learn relationships between them.

15. Vision Transformers

Transformers can also process images.

Instead of treating an image as one giant object, an image can be divided into smaller patches.

Image
  ↓
Image Patches
  ↓
Patch Embeddings
  ↓
Transformer
  ↓
Image Representation
  ↓
Prediction

This led to architectures known as Vision Transformers, often abbreviated as ViT.

16. Multimodal AI

Modern AI systems can work with multiple types of information.

Text ─────┐
          │
Image ────┼──→ AI Model
          │
Audio ────┘
          ↓
       Response

A multimodal system can combine information from different modalities instead of working only with text.

17. Modern AI Pipeline

                DATA
                  ↓
             Tokenization
                  ↓
             Embeddings
                  ↓
          Positional Information
                  ↓
       ┌─────────────────────┐
       │ Transformer Layers  │
       │                     │
       │ Self-Attention      │
       │        ↓            │
       │ Feed-Forward        │
       │        ↓            │
       │ Normalization       │
       └──────────┬──────────┘
                  ↓
             Output Layer
                  ↓
             Predictions

18. Simple Transformer Model With Python

You normally do not implement the complete architecture manually. Frameworks such as TensorFlow/Keras provide reusable Transformer components.

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers


class TransformerBlock(layers.Layer):

    def __init__(
        self,
        embedding_dim,
        num_heads,
        feed_forward_dim
    ):
        super().__init__()

        self.attention = layers.MultiHeadAttention(
            num_heads=num_heads,
            key_dim=embedding_dim
        )

        self.feed_forward = keras.Sequential([
            layers.Dense(
                feed_forward_dim,
                activation="relu"
            ),

            layers.Dense(
                embedding_dim
            )
        ])

        self.norm1 = layers.LayerNormalization(
            epsilon=1e-6
        )

        self.norm2 = layers.LayerNormalization(
            epsilon=1e-6
        )


    def call(self, inputs):

        attention_output = self.attention(
            inputs,
            inputs
        )

        x = self.norm1(
            inputs + attention_output
        )

        feed_forward_output = self.feed_forward(x)

        return self.norm2(
            x + feed_forward_output
        )


# Create a Transformer block

transformer = TransformerBlock(
    embedding_dim=32,
    num_heads=4,
    feed_forward_dim=64
)


# Example input

x = tf.random.normal(
    (2, 10, 32)
)


# Process input

output = transformer(x)


print("Input shape:", x.shape)
print("Output shape:", output.shape)

19. Understand the Python Code

Multi-Head Attention

self.attention = layers.MultiHeadAttention(
    num_heads=4,
    key_dim=embedding_dim
)

This creates the attention mechanism.

Here we use four attention heads.

Feed-Forward Network

self.feed_forward = keras.Sequential([
    layers.Dense(64, activation="relu"),
    layers.Dense(32)
])

The network expands the representation from 32 features to 64 and then projects it back to 32.

Self-Attention

attention_output = self.attention(
    inputs,
    inputs
)

The same input is used to create the query, key, and value inputs to the attention layer.

Residual Connection

x = self.norm1(
    inputs + attention_output
)

The original information is added back to the attention result before normalization.

Feed-Forward Processing

feed_forward_output = self.feed_forward(x)

The attention result is passed through the feed-forward network.

Final Output

return self.norm2(
    x + feed_forward_output
)

Another residual connection and normalization produce the final Transformer block output.

20. Example — AI Coding Assistant

Suppose you ask:

Write a Python function that adds two numbers.

A simplified view is:

User Prompt
     ↓
Tokens
     ↓
Embeddings
     ↓
Transformer Layers
     ↓
Contextual Representation
     ↓
Next-token probabilities
     ↓
Generated code

The model generates the response token by token.

def
 ↓
add
 ↓
(
 ↓
a
 ↓
,
 ↓
b
 ↓
)
 ↓
:
 ↓
return
 ↓
a
 ↓
+
 ↓
b

21. Transformers Are Not Magic

This is important.

A Transformer does not automatically understand truth, facts, or the real world simply because it is large.

Large Transformer
       ≠
Perfect Knowledge

Large Transformer
       ≠
Always Correct

Large Transformer
       ≠
Human Understanding

The model learns statistical patterns from its training process and can still produce incorrect or fabricated information.

22. Transformer vs RNN

RNN

Token 1
   ↓
Token 2
   ↓
Token 3
   ↓
Token 4


Transformer

Token 1 ─┐
Token 2 ─┤
Token 3 ─┼──→ Attention
Token 4 ─┘

RNNs process sequences recurrently, while Transformers use attention to model relationships between tokens.

During training, Transformers can process many tokens in parallel, which is a major advantage for large-scale training.

23. Modern AI Stack

Large Dataset
      ↓
Tokenization
      ↓
Embeddings
      ↓
Transformer Architecture
      ↓
Pretraining
      ↓
Fine-Tuning / Alignment
      ↓
Inference
      ↓
AI Application

The Transformer is an important part of the stack, but it is not the entire AI system.

24. Two Simple Examples

Example 1 — Text Generation

Input:

"Python is a"

Model predicts:

"programming"

Then:

"Python is a programming"

Model predicts another token.

Result:

"Python is a programming language."

Example 2 — Image Understanding

Image
 ↓
Image Patches
 ↓
Patch Embeddings
 ↓
Transformer
 ↓
Image Representation
 ↓
Prediction

This shows why Transformer ideas are useful beyond traditional text processing.

25. The Big Picture

                TRANSFORMER
                     │
        ┌────────────┼────────────┐
        │            │            │
      Text         Image        Audio
        │            │            │
        └────────────┼────────────┘
                     ↓
              Modern AI Models
                     ↓
        ┌────────────┼────────────┐
        │            │            │
   Generation   Understanding  Multimodal
        │            │            │
        └────────────┼────────────┘
                     ↓
                AI Applications

26. What You Should Remember

1. Transformers use attention.

2. Self-attention helps model relationships
   between tokens.

3. Transformers can process sequence information
   efficiently during training.

4. Transformer models can be scaled to large sizes.

5. They are used for text, images, audio,
   video, and multimodal AI.

6. Modern AI systems use much more than
   just the Transformer architecture.

7. Transformers are powerful, but they
   are not automatically correct.
QUICK CHECK

Check Your Understanding

1. Why are Transformers important?
They provide an effective architecture for modeling relationships in sequence data and can be trained efficiently at large scale.

2. What does self-attention do?
It allows each token to use information from other relevant tokens in the sequence.

3. How can a language model generate text?
It repeatedly predicts the next token and uses the generated context to continue the sequence.

4. Are Transformers only used for text?
No. They are also used for images, audio, video, and multimodal systems.

5. Is every modern Transformer an encoder-decoder model?
No. Modern systems can use encoder-only, decoder-only, or encoder-decoder architectures.

6. Does a large Transformer always give correct answers?
No. Transformer-based models can still make mistakes and generate incorrect information.