Natural Language Processing

Module 5: Transformers, Large Language Models, Prompt Engineering & Modern NLP Applications

This module provides an in-depth overview of the Transformer architecture, large language models (LLMs), self-supervised pretraining, prompt engineering, and real-world NLP applications. It describes the mechanisms powering modern generative AI, their capabilities, limitations, and societal impact.

Page 1 – The Transformer Architecture

1.1 Origin of Transformers

The Transformer model was introduced by Vaswani et al. (2017) in “Attention is All You Need.” It eliminated recurrence entirely in favour of:

  • Self-attention
  • Parallel computation
  • Improved long-range dependency modelling

1.2 Encoder–Decoder Design

The Transformer contains two main components:

  • Encoder – maps input sequence to contextual representations.
  • Decoder – autoregressive generator for output sequences.

1.3 Multi-Head Attention

Multi-head attention allows the model to learn multiple relationships simultaneously.

Q = XW_Q
K = XW_K
V = XW_V
Attention = softmax(QKᵀ / √dₖ) V
    

1.4 Positional Encoding

Because Transformers have no inherent recurrence, they inject order using positional encodings:

  • Sinusoidal encodings
  • Learned positional embeddings
  • Rotary and ALiBi encodings in modern models

1.5 Feed-Forward Networks

Each Transformer layer includes a position-wise feed-forward network:

FFN(x) = max(0, xW₁ + b₁)W₂ + b₂

1.6 Layer Normalisation & Residual Connections

Residual connections + layer normalisation stabilise deep architecture training.

1.7 Why Transformers Replaced RNNs

  • Better global context modelling
  • Parallelisation → faster training
  • Higher performance on seq2seq tasks
  • Scalable to billions of parameters

Page 2 – Self-Supervised Pretraining & Autoregressive Models

2.1 The Self-Supervised Paradigm

Instead of requiring labelled data, LLMs are trained on self-supervised objectives:

  • Predict masked tokens (BERT)
  • Predict next token (GPT)
  • Reconstruct original text from corrupted input (BART, T5)

2.2 Masked Language Modelling (MLM)

MLM masks 15% of tokens and asks the model to fill in blanks. Effective for bidirectional understanding.

2.3 Autoregressive Language Modelling

GPT-style models learn:

P(wₜ | w₁, …, wₜ₋₁)

This enables open-ended text generation and reasoning.

2.4 Encoder-Only vs. Decoder-Only vs. Encoder–Decoder

  • Encoder-only (BERT) → classification + extraction tasks
  • Decoder-only (GPT) → generative tasks
  • Encoder–decoder (T5, BART) → translation, summarisation

2.5 Scaling Laws

Research shows predictable improvement as we scale:

  • Model size (parameters)
  • Dataset size
  • Training compute

2.6 Emergent Behaviour

Large-scale models display properties not present in small models:

  • Chain-of-thought reasoning
  • Code generation
  • Tool use
  • In-context learning

Page 3 – Large Language Models (LLMs)

3.1 What Is an LLM?

A Large Language Model is a Transformer-based model with billions of parameters, trained on massive corpora to learn general language representations and reasoning.

3.2 Key Capabilities

  • Natural language understanding
  • Text generation
  • Translation & summarisation
  • Information retrieval
  • Reasoning & chain-of-thought
  • Coding & mathematical problem solving

3.3 Instruction Tuning

Models like ChatGPT, LLaMA-2, PaLM, and Mistral use:

  • Instruction tuning – training on high-quality instruction–response pairs.
  • Supervised fine-tuning (SFT)

3.4 Reinforcement Learning from Human Feedback (RLHF)

RLHF aligns model outputs with human preferences:

  1. Generate model responses.
  2. Rank by human evaluators.
  3. Train a reward model.
  4. Optimise using PPO or similar algorithms.

3.5 Retrieval-Augmented Generation (RAG)

RAG systems enhance LLMs with external knowledge:

  • Vector databases
  • Document retrieval
  • Real-time information access

3.6 Limitations of LLMs

  • Hallucination (fabricated content)
  • Bias & stereotypical associations
  • Lack of interpretability
  • High computational cost
  • Inconsistent reasoning in edge cases

Page 4 – Prompt Engineering

4.1 Why Prompts Matter

LLMs behave differently depending on how the input is phrased. Prompt engineering improves control, reliability, and output quality.

4.2 Types of Prompts

  • Zero-shot prompts – no examples provided.
  • Few-shot prompts – include demonstrations.
  • Chain-of-thought prompts – encourage step-by-step reasoning.
  • Role-based prompts – instruct model to adopt a persona.

4.3 Example of Few-Shot Prompt

Translate to Thai:
Input: "I am hungry"
Output: "ฉันหิว"
    

4.4 Prompt Patterns

  • Instruction → Input → Output
  • Role → Task → Constraints
  • Context → Question

4.5 Avoiding Prompt Pitfalls

  • Avoid ambiguous instructions.
  • Specify output format.
  • Include examples for complex tasks.
  • Use delimiters to separate content.

4.6 Advanced Prompting

  • Self-consistency – majority reasoning.
  • Generated knowledge prompting – ask model to generate background info first.
  • Tool-use prompting – combine with external APIs.

Page 5 – Applications, Risks, Deployment & Future Directions

5.1 Real-World Applications

  • Healthcare NLP (clinical summarisation, coding)
  • Legal text analysis
  • Customer support automation
  • Search engines & conversational interfaces
  • Education (tutoring, feedback generation)
  • Software development assistants
  • Scientific literature analysis

5.2 Risks & Safety Concerns

Deploying LLMs introduces critical risks:

  • Hallucinations – confident but incorrect answers
  • Bias & fairness issues – inherited from training data
  • Privacy leakage – memorisation of sensitive training examples
  • Security vulnerabilities – prompt injection, jailbreaking
  • Misinformation scaling

5.3 Responsible Deployment

  • Model monitoring
  • Human-in-the-loop systems
  • Evaluation on safety benchmarks
  • RAG-based verification
  • Transparent documentation

5.4 Future Directions

  • Multimodal LLMs (text, image, audio, video)
  • Agentic AI – tool use, reasoning with external environments
  • Continual learning and memory-based architectures
  • Neural–symbolic integration for reasoning
  • On-device LLMs for privacy-first applications

5.5 Summary

Module 5 concludes the NLP course by presenting the architectures and methodologies that define modern NLP—Transformers, LLMs, prompt engineering, and real-world deployment considerations. These models dominate today’s AI landscape and continue to reshape the future of language technologies.

Pages: 1 2 3 4 5 6