Back to Data Science Notes
Topic #84

Large Language Models (LLMs)

Understand how Transformer-based Large Language Models (LLMs) process text using self-attention mechanisms to predict the next token in a sequence.

What it is

A Large Language Model (LLM) based on the Transformer architecture is a neural network designed to understand and generate human language. Unlike older recurrent models, Transformers process entire sequences of tokens simultaneously. The core innovation is self-attention, which allows the model to weigh the importance of different words in a sentence relative to each other when predicting the next word. Key components include embeddings (converting words to vectors), positional encodings (adding order information), and multi-head attention layers.

Why it matters

  • Contextual Understanding: Captures long-range dependencies in text, enabling coherent paragraph-level generation.
  • Parallelization: Processes all tokens at once during training, significantly speeding up computation compared to sequential RNNs.
  • Scalability: Performance improves predictably with more data and larger model sizes (scaling laws).
  • Versatility: One architecture powers translation, summarization, code generation, and question answering.

Syntax or steps

The basic inference loop for an LLM involves: 1. Tokenize input text into integer IDs. 2. Embed IDs into vector representations. 3. Pass through Transformer layers (Attention + Feed Forward). 4. Project final hidden state to vocabulary size via a linear layer. 5. Apply softmax to get probabilities for the next token. 6. Select the highest probability token (or sample) and append to input.

Example

This minimal PyTorch example demonstrates the core logic of a single Transformer block processing a sequence. Note that real LLMs have many such blocks stacked together.

import torch
import torch.nn as nn

class SimpleTransformerBlock(nn.Module):
    def __init__(self, d_model=512, nhead=8):
        super().__init__()
        # Self-attention mechanism
        self.self_attn = nn.MultiheadAttention(d_model, nhead, batch_first=True)
        # Layer normalization stabilizes training
        self.norm1 = nn.LayerNorm(d_model)
        # Position-wise feed-forward network
        self.ffn = nn.Sequential(
            nn.Linear(d_model, 2048),
            nn.ReLU(),
            nn.Linear(2048, d_model)
        )
        self.norm2 = nn.LayerNorm(d_model)

    def forward(self, x):
        # Attention sub-layer with residual connection
        attn_output, _ = self.self_attn(x, x, x)
        x = self.norm1(x + attn_output)
        
        # FFN sub-layer with residual connection
        ffn_output = self.ffn(x)
        x = self.norm2(x + ffn_output)
        return x

# Simulate input: Batch size 1, Sequence length 10, Embedding dim 512
input_tensor = torch.randn(1, 10, 512)
block = SimpleTransformerBlock()
output_tensor = block(input_tensor)

print(f"Input shape: {input_tensor.shape}")
print(f"Output shape: {output_tensor.shape}")

Explanation: The MultiheadAttention layer computes relationships between all positions in the sequence. Residual connections (x + attn_output) help gradients flow during training. Layer Norm ensures stable activations. The output maintains the same shape as the input but contains contextually enriched representations.

Common mistakes

  • Ignoring Positional Encoding: Without adding position information, the model treats "dog bites man" and "man bites dog" identically. Always inject positional signals before attention.
  • Causal Masking Errors: In autoregressive generation, you must mask future tokens so the model doesn't "cheat" by seeing what comes next during training. Use is_causal=True in attention calls.
  • Overlooking Tokenizer Mismatch: Using the wrong tokenizer for a pre-trained model leads to gibberish. Ensure the embedding matrix matches the specific vocabulary of the checkpoint.
  • Forgetting Normalization: Skipping LayerNorm often results in unstable training or divergence due to exploding activations in deep stacks.

When to use it

ScenarioUse Transformer LLMUse Alternative (e.g., RNN/CNN)
Long-context understanding Yes (Self-attention handles global dependencies) No (RNNs struggle with very long sequences)
Low-latency edge device Maybe (requires quantization/distillation) Yes (Smaller CNNs/RNNs may be faster)
Massive parallel training Yes (Highly optimized for GPUs/TPUs) No (Sequential nature limits speed)

Practice

Guided Exercise: Modify the code above to add a simple causal mask to the attention call. Hint: Create a triangular boolean matrix where True indicates positions that should not be attended to.

Challenge: Implement a function that takes the output tensor from the block, projects it to a vocabulary size of 1000, applies softmax, and returns the index of the most likely next token. Expected output: An integer between 0 and 999.

Quick check

Q: Why do we need positional encodings in Transformers if they process all tokens in parallel?

A: Because self-attention is permutation-invariant; without positional info, the model cannot distinguish word order, making sentences like "I love you" and "You love I" indistinguishable.

Summary

Transformers revolutionized NLP by replacing recurrence with self-attention, allowing models to capture complex contextual relationships efficiently. Mastering the interaction between embeddings, attention heads, and residual connections is key to building or fine-tuning effective LLMs.

Want to go beyond the notes?

Join Coding Now Tech Institute's Data Science course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Large Language Models (LLMs) – FAQs

Quick answers about learning Large Language Models (LLMs) in Data Science.

This free note from Coding Now Tech Institute explains Large Language Models (LLMs) in Data Science — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Data Science topic on Coding Now Tech Institute, including Large Language Models (LLMs), is 100% free with no signup required.
With focused practice, most students grasp Large Language Models (LLMs) in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now