Deep Learning Specializations

Sequence Models

Lesson 4: Sequence Models — Language & Time Series | Beginner Guide
Deep Learning · Lesson 4

Sequence Models
Language Processing & Time Series

A beginner-friendly guide to how machines understand text, audio, and time-ordered data: from sequential data, through RNN, LSTM, and GRU, to Attention, Transformers, large language models (BERT, GPT, Llama), and everyday apps — with plain-language analogies.

✍ Abdurrahman Al-Rifai
9 sections
RNN → LLM
Simple examples
01 Section 1

Sequential Data

One simple idea first: some data has order. Change the order, and the meaning changes. That is sequential data.

Analogy: a picture story

Imagine five photos: open door → see gift → open gift → smile → thank mom. Shuffle them and the story breaks. Text, audio, and stock prices work the same way: order is part of the meaning.

Text

“The cat ate the mouse” is not “The mouse ate the cat.” Same words, different order, different meaning. Models must track what came before and after.

Everyday example

“I did not like the movie” ≠ “I liked the movie.” The word “not” flips the meaning of what follows.

Audio

Sound is a wave over time. The computer cuts it into tiny consecutive frames. The word “hello” is a sequence of sounds, not a single point.

Time Series

Numbers measured through time: gold price each day, temperature each hour. The goal is often: predict tomorrow from the past.

DNA

DNA is a sequence of letters A, T, C, G. Order decides what a gene does — same “order matters” idea, in biology.

TypeUnitExample question
Textchar / word / sentenceIs this review positive?
Audioframe / phonemeWhat did the speaker say?
Time seriesvalue at time tWhat is tomorrow’s price?
DNAbase (A/T/C/G)Is this region important?
02 Section 2

Recurrent Neural Networks

A normal feedforward net sees inputs once and stops. Text is long: word after word. We need a net that reads step by step and remembers. That is the RNN idea.

RNN — the core idea

At each time step t, the network takes the current input and a summary of past steps (hidden state), then produces a new summary for the next step — like reading a book and updating margin notes.

Hidden State

The hidden state is a vector of numbers meaning “what I have understood so far.”

Analogy: lecture notes

You do not remember every word. You keep a compressed summary and update it. The hidden state is that notebook inside the network.

Sequence Learning

Many → one

Full sentence → sentiment class.

One → many

Image → multi-word caption.

Many → many

Translate sentence word by word.

Next-step prediction

“Once upon a…” → “time”.

hₜ = tanh(Wₓ · xₜ + Wₕ · hₜ₋₁ + b)
Strength: handles variable lengths.
Weakness: forgets early context in long sequences — hence LSTM.
03 Section 3

LSTM — Long Short-Term Memory

Vanilla RNNs forget. Name a person early, ask 50 words later “who did that?” — the name may be gone. LSTM adds gates that control what to keep and what to drop.

Analogy: warehouse manager

A warehouse (cell) gets new deliveries. The manager decides: what old stock to throw away, what new stock to store, what to show customers today. Those decisions are the gates.

Gates

Forget Gate

What to erase from memory.

Input Gate

What new info deserves to enter.

Output Gate

What memory to expose as the output now.

Cell State

Think of a smooth conveyor belt carrying memory across steps. Gates gently add or remove information so important facts last longer.

Long-Term Memory

Names, tense, and links from sentence start to end stay available — which made LSTM dominant for years in translation, sentiment, and forecasting.

Story example

“Ahmed went to the market, bought apples and oranges, then went home because it started raining. Who bought the fruit?” LSTM keeps “Ahmed” available until the question.

04 Section 4

GRU — Gated Recurrent Unit

GRU is close to LSTM but simpler and often faster: two gates instead of three, one state instead of two.

Reset Gate

How much past to ignore when computing the new candidate. Helpful when the topic suddenly changes.

Update Gate

Mixes old and new memory in one knob — from “keep everything” to “replace with new.”

LSTM: sometimes better on very long chains; more flexible.
GRU: fewer parameters; similar quality often.

Today, most language tasks move to Transformers. LSTM/GRU remain essential foundations.

05 Section 5

NLP Basics

Tokenization

Split text into tokens: words, subwords, or characters. Subwords help rare words by reusing known pieces.

text = "I love deep learning"
tokens = text.split()
print(tokens) # ['I', 'love', 'deep', 'learning']

Embeddings

Each token becomes a vector of numbers. Similar meanings live near each other in that space.

Analogy: a map of meanings

“King,” “queen,” and “prince” cluster together. “Banana” and “apple” sit in fruit territory. Distance reflects relatedness.

Word2Vec · GloVe · FastText

king − man + woman ≈ queen
MethodIdeaWhen useful
Word2VecLearn from local contextSimilarity search, NLP starters
GloVeGlobal co-occurrence statsReady static vectors
FastTextSubword piecesRare words, typos, rich morphology

Modern Transformers learn contextual embeddings: “bank” in “river bank” differs from “bank account.”

06 Section 6

Attention

When translating a long sentence, you do not weight every word equally. You focus on what matters now. Attention gives the network that spotlight.

Analogy: stage spotlight

The stage is dimly lit, but the spotlight follows the actor. Translating “ate,” the light focuses more on “apple” than on “yesterday.”

Self Attention

Every word looks at every other word in the same sentence and asks: “who helps me most?”

Example

“The blue bird sang on the branch because it was happy.” The word “it” should link to “bird,” not “branch.” Self-attention learns stronger weights between “it” and “bird.”

Query / Key / Value: like asking a library question (Query), matching book titles (Keys), then taking useful content (Values).

Multi Head Attention

Several heads in parallel learn different relation types: subject–verb, adjective–noun, pronoun–referent — then merge their views.

07 Section 7

Transformers

“Attention Is All You Need” (2017) changed the game: process sequences with attention instead of only step-by-step recurrence. Faster GPU training, stronger long-range links.

Tokenization Embeddings + positions Encoder / Decoder Outputs

Encoder

Reads the input and builds a rich contextual representation for each token via stacked Self-Attention + feed-forward layers.

Decoder

Generates the output step by step, looking at its own past tokens and at the encoder’s understanding of the source.

Positional Encoding

Attention alone does not know who is first or second. We add position signals so order is preserved.

Analogy: seat numbers

Students have faces (meaning) plus seat numbers (position). Without seat numbers, seating order is lost. Positional encoding is the seat ticket for words.

08 Section 8

Large Language Models

An LLM is a huge Transformer trained on vast text — often by predicting the next token millions of times until language patterns become fluent.

BERT

Bidirectional encoder. Great for classification, NER, and reading comprehension. Classic pretraining: fill in [MASK]ed words.

GPT

Generative decoder-style models. Basis of many chat assistants: writing, dialogue, summarization, coding (after instruction tuning).

T5

Casts every task as text-to-text: “translate: …”, “summarize: …”, “sentiment: …”.

Llama · Qwen · DeepSeek

Llama

Meta’s open family — popular for research and local customization.

Qwen

Strong multilingual family from Alibaba.

DeepSeek

Modern models noted for training efficiency and strong reasoning/coding.

ModelStyleCommon use
BERTUnderstand (Encoder)Classify, search, NER
GPTGenerate (Decoder)Chat, write, code
T5Text→textTranslate, summarize
Llama / Qwen / DeepSeekModern LLMsAssistants, apps, fine-tuning
09 Section 9

Applications

Translation

Source → Encoder understanding → Decoder target text. Modern systems are Transformer-based.

Chatbots

Read your messages as a sequence, keep dialogue context within a token limit, generate a reply token by token.

Summarization

Long article → short summary. Extractive (pick sentences) or abstractive (rewrite).

Sentiment Analysis

Product review → positive / negative / neutral. A small model or fine-tuned BERT often suffices.

Speech Recognition

Audio waveform → time features → sequence model → text. Your voice keyboard uses these ideas.

14-day beginner plan
  1. Days 1–2: Hand-tokenize short sentences; understand sequences.
  2. Days 3–4: Tiny RNN for next-character prediction.
  3. Days 5–6: LSTM/GRU sentiment on short texts.
  4. Days 7–8: Study Attention; visualize weights if possible.
  5. Days 9–11: Hugging Face pipeline (translate or classify).
  6. Days 12–14: Small project + GitHub README.
FAQ Questions

FAQ on Sequence Models

Are RNNs still useful after Transformers?
Yes for learning, small projects, some time series, and legacy systems. For modern NLP, Transformer/LLM is the default.
LSTM or GRU?
Start with GRU for speed, or try both. For large language tasks, jump to Transformers.
Static vs contextual embeddings?
Word2Vec gives one vector per word forever. BERT/GPT change the vector with surrounding context.
Why do chatbots sound confident when wrong?
They sample likely next tokens, not a guaranteed fact database. Verify critical claims.
Do I need a GPU?
Small RNNs: CPU is fine. Training Transformers or running local LLMs: GPU or cloud (e.g. Colab).
What next after this lesson?
Revisit Lesson 3 CNN if needed, then Part 3. Ship a small sentiment or summarization project this week.