UNIT - II
Generative Models For Text: Language Models Basics,
Building blocks of Language models,
Transformer Architecture,
Encoder and Decoder,
Attention mechanisms,
Generation of Text,
Models like BERT and GPT models,
Generation of Text,
Autoencoding,
Regression Models,
Exploring ChatGPT,
Prompt Engineering: Designing Prompts,
Revising Prompts using Reinforcement Learning from Human Feedback (RLHF),
Retrieval Augmented Generation,
Multimodal LLM,
Issues of LLM like hallucination.
Classified as Microsoft Confidential
LANGUAGE MODELS BASICS
Example:
Classified as Microsoft Confidential
COMPONENTS OF A LANGUAGE MODEL
Breaks text into smaller units called tokens.
Example:
Classified as Microsoft Confidential
3.Vocabulary
4.Neural Network
5.Probability Prediction
Classified as Microsoft Confidential
�������TYPES OF LANGUAGE MODELS�
Classified as Microsoft Confidential
TYPES OF STATISTICAL LANGUAGE MODELS
Classified as Microsoft Confidential
Classified as Microsoft Confidential
Classified as Microsoft Confidential
EXAMPLE:CALCULATING WORD PROBABILITY
Classified as Microsoft Confidential
Classified as Microsoft Confidential
EXAMPLE 2
Classified as Microsoft Confidential
Classified as Microsoft Confidential
�����EXAMPLE 1�
Classified as Microsoft Confidential
Classified as Microsoft Confidential
EXAMPLE
Classified as Microsoft Confidential
2.Neural Language Models
A Neural Language Model (NLM) is a language model that uses Artificial Neural Networks (ANNs) to learn the probability distribution of words and predict the next word in a sentence. Unlike Statistical Language Models, Neural Language Models learn semantic meaning and context from data using word embeddings and neural networks.
Classified as Microsoft Confidential
3.Transformer-Based Language Models
Classified as Microsoft Confidential
������APPLICATIONS�
Classified as Microsoft Confidential
BUILDING BLOCKS OF LANGUAGE MODELS
Classified as Microsoft Confidential
�������1 .TEXT CORPUS (TRAINING DATA)�
Examples:
Classified as Microsoft Confidential
�2. TOKENIZATION�
Tokenization is the process of dividing text into smaller units called tokens.
Types of Tokens:
Example:
Sentence:
Artificial Intelligence is powerful.
Tokens:
Classified as Microsoft Confidential
�3. VOCABULARY�
Vocabulary is the collection of all unique tokens known by the model.
Vocabulary
------------------
Artificial
Intelligence
Machine
Learning
Data
Science
AI
A larger vocabulary improves language understanding but increases computational cost.
Classified as Microsoft Confidential
��4. WORD EMBEDDINGS�
Embeddings convert words into numerical vectors that capture semantic meaning.
Word | Vector Representation |
Cat | [0.12, 0.45, -0.31] |
Dog | [0.15, 0.42, -0.29] |
Car | [-0.72, 0.08, 0.91] |
Classified as Microsoft Confidential
5. POSITIONAL ENCODING�
Transformers process all words simultaneously, so they require positional information.
Example:
Sentence:
Token | Position |
I | 1 |
Love | 2 |
AI | 3 |
Classified as Microsoft Confidential
6. NEURAL NETWORK ARCHITECTURE�
Modern language models use Transformer architecture.
Main Components:
Classified as Microsoft Confidential
7. ATTENTION MECHANISM�
Attention allows the model to focus on the most relevant words while predicting the next token.
Example:
Sentence:
‘”The student submitted the assignment because he completed it”.
Benefits:
Classified as Microsoft Confidential
8. CONTEXT WINDOW�
The context window is the maximum number of previous tokens the model can consider.
Example:
↓
Larger context windows improve:
Classified as Microsoft Confidential
9. PROBABILITY DISTRIBUTION�
Language models predict the probability of the next token.
Example:
Input:
Word | Probability |
AI | 0.55 |
Programming | 0.20 |
Cricket | 0.10 |
Music | 0.08 |
Food | 0.07 |
Classified as Microsoft Confidential
�10. TRAINING PROCESS
The model learns by minimizing prediction errors using optimization algorithms.
Steps:
Classified as Microsoft Confidential
�����11. DECODING STRATEGIES�
Classified as Microsoft Confidential
���TRANSFORMER ARCHITECTURE�
Classified as Microsoft Confidential
Classified as Microsoft Confidential
���TRANSFORMERS WORK�
1. Input Representation
The first step in processing input data involves converting raw text into a format that the transformer model can understand. This involves tokenization and embedding.
Classified as Microsoft Confidential
2. ENCODER PROCESS IN TRANSFORMERS�
Query, Key and Value
The attention calculation is:
Where:
Classified as Microsoft Confidential
3.Feed-Forward Network: The output from the self-attention mechanism is passed through a position-wise feed-forward network.
4.Layer Normalization and Residual Connections: Layer normalization and residual connections are applied.
Classified as Microsoft Confidential
Multi-Head Attention Mechanism
Classified as Microsoft Confidential
��3. DECODER PROCESS�
Example;
Sentence :
I love AI today
Classified as Microsoft Confidential
3.Encoder-Decoder Attention Mechanism: The decoder attends to the encoder's output allowing it to focus on relevant parts of the input sequence.
4.Feed-Forward Network: Similar to the encoder the output from the attention mechanisms is passed through a position-wise feed-forward network.
5.Layer Normalization and Residual Connections: Similar to the encoder Layer normalization and residual connections are applied.
Classified as Microsoft Confidential
Classified as Microsoft Confidential
4. TRAINING AND INFERENCE�
Classified as Microsoft Confidential
ENCODER AND DECODER
Example:
Input → Encoder → Latent Representation → Decoder → Output
English:�I love AI
↓ Encoder
Context representation
↓ Decoder
Telugu:�నేను AIని ప్రేమిస్తున్నాను
Classified as Microsoft Confidential
Classified as Microsoft Confidential
WORKING ENCODER AND DECODER
Classified as Microsoft Confidential
Step 1: Tokenizing the Input Sentence
Step 2: Encoding the Input
Classified as Microsoft Confidential
Step 3: Passing the Context to the Decoder
Step 4: Decoder Generates Output Step-by-Step
Classified as Microsoft Confidential
Step 5: Attention Mechanism
Step 6: Producing the Final Output
Classified as Microsoft Confidential
ATTENTION MECHANISM
Example
Classified as Microsoft Confidential
QUERY, KEY AND VALUE
Classified as Microsoft Confidential
Classified as Microsoft Confidential
SELF-ATTENTION:
Classified as Microsoft Confidential
MULTI-HEAD ATTENTION:
Multi-Head Attention performs attention using multiple attention heads.
Different heads can learn different relationships, such as:
Classified as Microsoft Confidential
�EXAMPLE
Attention Head | What it may learn |
Head 1 | “he” → “student” (who does he refer to?) |
Head 2 | “completed” → “assignment” (what was completed?) |
Head 3 | “submitted” → “assignment” (what was submitted?) |
Consider the sentence:
“The student submitted the assignment because he completed it.”
Suppose a Transformer uses 3 attention heads.
Each head focuses on different relationships between the words.
Classified as Microsoft Confidential
���GENERATION OF TEXT�
Basic Process
Input Prompt → Tokenization → Embedding → Transformer → Next-Token Prediction → Token Selection → Generated Text
Classified as Microsoft Confidential
STEPS IN TEXT GENERATION�
Step 1: Prompt/Input�The user provides text such as:
“Explain Machine Learning.”
Step 2: Tokenization�The input is divided into tokens.
Step 3: Embedding�Tokens are converted into numerical vectors that the model can process.
Step 4: Transformer Processing�The Transformer analyzes the context using Multi-Head Self-Attention and Feed-Forward Networks.
Step 5: Next-Token Prediction�The model calculates probabilities for possible next tokens.
Classified as Microsoft Confidential
Step 6: Token Selection�A token is selected using techniques such as greedy decoding, temperature sampling, Top-k, or Top-p sampling.
Step 7: Repetition�The selected token is added to the sequence, and the model predicts the next token.
Step 8: Stopping�Generation stops when an end token is produced or the maximum generation length is reached.
Classified as Microsoft Confidential
���DIFFERENT APPROACHES TO TEXT GENERATION�
Classified as Microsoft Confidential
Some of the most powerful models for text generation include:
Classified as Microsoft Confidential
MODELS LIKE BERT AND GPT MODELS,
Classified as Microsoft Confidential
Classified as Microsoft Confidential
1. BERT (BIDIRECTIONAL ENCODER REPRESENTATIONS FROM TRANSFORMERS)
BERT (Bidirectional Encoder Representations from Transformers) is an encoder-only Transformer model developed by Google.
Classified as Microsoft Confidential
2. GPT MODELS
GPT (Generative Pre-trained Transformer) models are primarily decoder-only Transformer models designed for text generation.
Classified as Microsoft Confidential
DIFFERENCE BETWEEN BERT AND GPT�
Feature | BERT | GPT |
Architecture Type | Encoder only Transformer | Decoder only Transformer |
Attention Type | Multi head Attention | Masked Multi head Attention |
Context Handling | Considers both left and right context simultaneously | Considers only left context |
Primary Purpose | Understanding and extracting meaning from text | Generating coherent and context relevant text |
Training Objective | Masked Language Modeling (MLM) predicts masked words using full context | Causal Language Modeling predicts the next word based on past words |
Typical Output | Classifications, embeddings, extracted answers | Generated sentences, paragraphs or code |
Best Suited For | Sentiment analysis, question answering, classification | Story writing, chatbots, code generation, creative tasks |
Classified as Microsoft Confidential
AUTOENCODING
Classified as Microsoft Confidential
Classified as Microsoft Confidential
ENCODER�
Where:
Example
Classified as Microsoft Confidential
LATENT SPACE�
For example, for an image of a handwritten digit, the latent representation may capture:
Classified as Microsoft Confidential
5. DECODER�
Where:
The objective is:
x^≈x
Classified as Microsoft Confidential
RECONSTRUCTION LOSS�
Classified as Microsoft Confidential
The main steps are:
Classified as Microsoft Confidential
REGRESSION MODELS
Regression is a supervised learning technique used to predict continuous numerical values by learning relationships between input variables (features) and an output variable (target).
Examples:
Classified as Microsoft Confidential
Classified as Microsoft Confidential
Where:
Classified as Microsoft Confidential
TYPES OF REGRESSION�
1. Simple Linear Regression
Classified as Microsoft Confidential
2. Multiple Linear Regression
Uses multiple input variables.
Example: Predicting house price using area, bedrooms, and location.
Classified as Microsoft Confidential
3. Polynomial Regression
Example: Predicting population growth.
4. Ridge Regression
Classified as Microsoft Confidential
5.Lasso Regression
6.Logistic Regression
7.Support Vector Regression (SVR)
8.Decision Tree Regression
Classified as Microsoft Confidential
EXAMPLE
Input Data → Generative Model → Generated Data → Regression Model → Numerical Prediction
Classified as Microsoft Confidential
EXPLORING CHATGPT
ChatGPT is a Generative AI-based conversational system developed by OpenAI. It uses large language models (LLMs) to understand user instructions and generate human-like responses in natural language. It can be used for education, programming, research, content creation, and many other applications.
Classified as Microsoft Confidential
1. WORKING OF CHATGPT�
The basic working process is:
Classified as Microsoft Confidential
APPLICATIONS OF CHATGPT�
Classified as Microsoft Confidential
PROMPT ENGINEERING: DESIGNING PROMPTS
Prompt engineering is the process of creating clear and effective prompts that guide AI models to generate accurate responses such as ChatGPT, Gemini, and Claude to obtain accurate, relevant, and useful outputs.
Example
Poor Prompt:
“Explain AI”
Good Prompt:
“Explain Artificial Intelligence in simple language for undergraduate students. Give its definition, three applications, advantages, and limitations in 200 words”
The second prompt provides clear instructions, context, and output requirements, so the AI can generate a better response.
Classified as Microsoft Confidential
�
Classified as Microsoft Confidential
PROMPT DESIGN FRAMEWORK�
Classified as Microsoft Confidential
DATABASES AND INFORMATION SOURCES�
Classified as Microsoft Confidential
WORKFLOWS�
Classified as Microsoft Confidential
PROMPT LIBRARIES�
Prompt Libraries are collections of ready made prompts that help guide AI models to generate useful and consistent responses. They act as reusable templates for different tasks, saving time and improving output quality.
Classified as Microsoft Confidential
GENERATIVE AI�
Classified as Microsoft Confidential
IMPORTANT PROMPT DESIGN TECHNIQUES�
Classified as Microsoft Confidential