CertNote/ Commute Study Notes
Roadmap
KOEN
CLF-C02 · FoundationalCloud Practitioner - Foundational
DVA-C02 · AssociateDeveloper - Associate
SAA-C03 · AssociateSolutions Architect - Associate
SOA-C02 · AssociateCloudOps Engineer - Associate
SAP-C02 · ProfessionalSolutions Architect - Professional
DOP-C02 · ProfessionalDevOps Engineer - Professional
Linux Master 2 · 2급KAIT Linux Master - Level 2
SCS-C03 · SpecialtySecurity - Specialty
Linux Master 1 · 1급KAIT Linux Master - Level 1
MLA-C01 · AssociateMachine Learning Engineer - Associate
AIF-C01 · FoundationalAI Practitioner - Foundational
DEA-C01 · AssociateData Engineer - Associate
MLS-C01 · SpecialtyMachine Learning - Specialty
  • Week 1
    • 1.The ML Lifecycle (from a Specialty Perspective)
    • 2.Data Storage for ML: S3, EFS, FSx for Lustre, and Data Formats
    • 3.Data Ingestion: Kinesis, Glue, Batch vs. Streaming
    • 4.Data Labeling: SageMaker Ground Truth, Active Learning, Label Quality
    • 5.Week 1 Comprehensive Review: ML Overview & Data Engineering 1
  • Week 2
  • Week 3
  • Week 4
  • Week 5
  • Week 6
  • Week 7
    • 1.Neural Network Basics: From the Perceptron to Backpropagation
    • 2.CNN: Convolutional Neural Networks and Computer Vision
    • 3.RNNs and Sequence Models: From LSTM to Transformer
    • 4.Training Techniques and Transfer Learning
    • 5.Week 7 Synthesis: Deep Learning Summary
  • Week 8
  • Week 9
  • Week 10
  • Week 11
  • Week 12
SQLD · 개발자SQL Developer (SQLD)

© CertNote · 주간랩스

  • About
  • Changelog
  • Contact
  • Terms
  • Privacy
← MLS-C01/Week 7/Day 3

Based on the MLS-C01 exam guide·Report an error

MLS-C01· AssociateWeek 7 · Day 3~30 min read

Day 3 - RNNs and Sequence Models: From LSTM to Transformer

📌 Key Takeaways

  • An RNN processes a sequence by accumulating the past in its hidden state, but it is weak at long-term dependencies because of vanishing and exploding gradients.
  • LSTM (gates + cell state) / GRU (fewer gates, lighter and faster) mitigate that weakness.
  • In seq2seq (encoder-decoder), the information bottleneck of the fixed context vector was the problem, and attention resolved it.
  • The Transformer processes sequences with self-attention alone, with no recurrence, so it is strong at parallel training and long-range dependencies (BERT/GPT/T5).
  • AWS mapping: time series = DeepAR/Forecast, text = BlazingText/Comprehend, generation = JumpStart/Bedrock.

Data Where Order Creates Meaning

If images have spatial structure, then text, time series, speech, and logs have sequential structure. For this kind of data, where earlier inputs affect later outputs, we use recurrent neural networks (RNNs) and their successors, and today's mainstream, the Transformer.

DataWhy order mattersIf order is ignored
TextWord order changes meaning"The dog bit the man" and "The man bit the dog" become the same
Time series (demand, sales)Trend and seasonality live on the time axisThe seasonal pattern is lost entirely
SpeechA run of phonemes forms a wordShuffling the frames makes it nonsense
Logs, clickstreamsThe order of actions reveals intentThe flow of the session disappears

MLS-C01 asks about the differences among RNN/LSTM/GRU, the seq2seq structure, the concepts of attention and the Transformer, and algorithm choices for time series and NLP.

The Basic Idea of RNNs

An RNN processes a sequence one time step at a time and accumulates past information in its hidden state.

h_t = f(W_x * x_t + W_h * h_{t-1} + b)
The output y_t is computed from h_t
  • h_{t-1}: the "memory" up to the previous step is passed on to the next computation.
  • The weights are shared across all time steps (weight sharing along the time axis).
  • It naturally handles variable-length sequences.
 x_1        x_2        x_3        x_4
  │          │          │          │
  ▼          ▼          ▼          ▼
[RNN] ──h1─▶[RNN] ──h2─▶[RNN] ──h3─▶[RNN] ──▶ h4
  │          │          │          │
  ▼          ▼          ▼          ▼
 y_1        y_2        y_3        y_4
  └─ Every step shares the same weights W ─┘

Unrolled along the time axis, an RNN is equivalent to a very deep neural network. So during backpropagation (BPTT — backpropagation through time), gradients accumulate through multiplication and vanishing/exploding gradients become severe. As a result, a simple RNN suffers from the long-term dependency problem of being unable to remember information from the distant past. This limitation is the motivation for LSTM and GRU.

Sequence Task Types

The structure differs depending on which side, input or output, is the sequence.

TypeInput → outputExampleTypical structure
one-to-manySingle → sequenceImage captioningCNN encoder + RNN decoder
many-to-oneSequence → singleSentiment classification, anomaly detectionRNN + last hidden state → classification head
many-to-many (synchronous)Sequence → sequence of the same lengthNamed entity recognition, per-frame labelingOutput at every step
many-to-many (asynchronous)Sequence → sequence of a different lengthMachine translation, summarizationseq2seq (encoder-decoder)

⚠️ Trap: If you see a clue that the input and output lengths differ, the answer is an encoder-decoder (seq2seq), not a per-step output structure. Translation and summarization are the typical cases.

LSTM and GRU

These are RNN variants that mitigate the long-term dependency problem with gate mechanisms.

ModelStructureCharacteristic
LSTMInput, forget, and output gates + cell statePreserves long-term memory, many parameters
GRUUpdate and reset gatesSimpler and faster than LSTM, often with similar performance
  • Cell state: the LSTM's "highway" that carries information over long distances. The gates control what to erase (forget), what to add (input), and what to emit (output).
  • GRU reduces the gates so computation is lighter. It is a good LSTM alternative when data and resources are moderate.

Selection guide: use LSTM when long-term dependencies are decisive and resources are ample, and GRU when it must be lighter.

⚠️ Trap: The remedies for gradient vanishing and exploding differ. Vanishing is solved with structure (gates, residuals, ReLU). Exploding is handled with gradient clipping and a smaller learning rate. If the loss slowly stalls, suspect vanishing; if it suddenly jumps to NaN, suspect exploding.

[LSTM cell]
  previous cell state c_{t-1} ──────┬──▶ c_t   (long-range information path)
                                     │
   input x_t ─┬─▶ forget gate → what to erase
   previous h_{t-1}├─▶ input gate → what to write anew
             └─▶ output gate → what to emit ──▶ h_t

RNN · LSTM · GRU · Transformer Comparison

ItemSimple RNNLSTMGRUTransformer
Long-term dependenciesWeakStrong (cell state)Strong (slightly simpler than LSTM)Very strong (direct connections)
Parallelism across time stepsNot possible (sequential)Not possibleNot possiblePossible (processes everything at once)
Parameters / computeSmallestManyFewer than LSTMLarge, sensitive to sequence length
Position informationInherent in the structureInherent in the structureInherent in the structureInjected separately via positional encoding
Typical useShort sequences, teachingTime series, speech, long contextLightweight LSTM alternativeStandard for NLP and generative AI

In a question saying "I added GPUs but RNN training did not get faster," the cause is not the hardware but the sequential processing structure itself. If you want parallelism, you must move to the Transformer family.

seq2seq (Encoder-Decoder)

A structure that maps an input sequence → an output sequence (which may have a different length), as in machine translation.

Input sequence → [Encoder RNN] → Context vector → [Decoder RNN] → Output sequence
"I am happy"                                                (Korean: "I am happy")
  • The encoder compresses the whole input into a fixed-length context vector.
  • The decoder generates tokens one at a time from that vector.
  • Limitation: compressing a long input into a single vector loses information (a bottleneck).

Attention

It appeared to resolve the seq2seq bottleneck. The decoder learns which part of the input to focus on at each output step.

When the decoder generates the Korean word for "happy"
→ it assigns a high attention weight to "happy" in the input
        Input:  I     am    happy
               │     │      │
               ▼     ▼      ▼
   [Encoder hidden states h1, h2, h3]
               │     │      │
        weight 0.1   0.2    0.7      ← similarity to the decoder's current state (query)
               └──┬──┴──────┘
                  ▼
          weighted sum = context for this step
                  ▼
             Decoder → the Korean word for "happy"
  • Instead of a fixed context vector, it dynamically references the entire input.
  • It reduces information loss on long sequences and gives interpretability of input-output relationships.

The core of attention is the "query, key, value" mechanism. Weights are computed from the similarity between the decoder's current state (query) and each encoder position (key), and the values are then summed using those weights. Applying this idea, without any RNN, to every pair of positions inside a single sequence is self-attention, which is the foundation of the Transformer.

Transformer

"Attention Is All You Need" (2017) dropped the RNN and processed sequences with self-attention alone. It is today's standard for NLP and generative AI.

  • Parallel processing: it does not process time steps sequentially like an RNN but handles the whole sequence at once, so GPU training is much faster.
  • Long-range dependencies: distant tokens are connected directly through attention.
  • Positional encoding: order information is injected separately (attention itself does not know position).
  • Representative models: BERT (encoder, understanding), GPT (decoder, generation), T5 (encoder-decoder).

It is at the center of the pretraining-fine-tuning paradigm (large-scale pretraining + downstream fine-tuning), and AWS makes it easy to access through SageMaker JumpStart and Amazon Bedrock.

Model familyStructureGood at
BERTEncoderUnderstanding tasks such as classification and named entity recognition
GPTDecoderText generation and completion
T5Encoder-decoderSequence-to-sequence tasks such as translation and summarization

AWS Mapping for Time Series and NLP

TaskSuitable choice
Demand and sales time-series forecastingDeepAR (SageMaker), Amazon Forecast
Text classification and embeddings (traditional)BlazingText (Word2Vec/classification)
Machine translationSeq2Seq (SageMaker built-in), Amazon Translate if managed
Entity, sentiment, and key phrase extraction (managed)Amazon Comprehend
Speech-to-text (managed)Amazon Transcribe
Generation, summarization, QA (large language models)SageMaker JumpStart, Amazon Bedrock

How to Turn Text into Numbers

To feed text into a sequence model, you first have to convert it into a representation.

RepresentationMethodStrengthLimitation
Bag-of-Words / TF-IDFWord frequencies as a vectorSimple and fast, enough for traditional classificationDiscards word order entirely
Word embeddings (Word2Vec, etc.)Learn words as dense vectorsWords with close meanings get close vectorsCannot distinguish polysemous words by context
Contextual embeddings (Transformer-based)Reflect the context within the sentenceStrong on polysemy and contextHigh compute cost
  • SageMaker BlazingText supports both Word2Vec (embeddings) and text classification.
  • For tasks where word order matters (translation, named entity recognition), the Bag-of-Words family is unsuitable.

Training Your Own vs Managed AI Services

Situation clueChoiceWhy
"We have no ML specialists," "we want to plug it in quickly"Comprehend, Translate, TranscribeAPI calls with no training
"We must recognize our domain terminology"Custom training (BlazingText, Seq2Seq) or fine-tuningA general model is not enough
"Demand for thousands of SKUs at once"DeepAR / ForecastLearns many time series together
"Generation tasks like summarization and question answering"JumpStart / BedrockUses large language models

In time-series forecasting, DeepAR learns many related time series together, letting series with sparse data borrow patterns from other series (a global model). When there are many similar series (demand per store or per SKU), it is stronger than fitting ARIMA separately to each one. "Forecast demand for many products at once" is a DeepAR/Forecast signal.

Reading Time-Series Regression Metrics

MetricWhat it tells youWhen to prioritizeTrap
RMSEAverage error that penalizes large errors heavilyWhen big mistakes are costlySwayed heavily by a single outlier
MAEAverage absolute errorWhen it must be robust to outliersTreats large errors leniently
Convert to classification and use accuracy(not recommended)—Meaningless on sparse series where demand is mostly 0

If you turn sparse demand data into a "sold/not sold" binary classification and evaluate it with accuracy, most values are 0, so predicting nothing at all still produces a high number. Evaluate regression tasks with RMSE and MAE, and imbalanced classification with recall and PR-AUC.

Question Clue → Answer Mapping

If the question says thisIt points to
"It cannot remember information from the distant past"Long-term dependency → LSTM/GRU
"Input and output lengths differ"seq2seq (encoder-decoder)
"Quality drops only on long inputs"Context bottleneck → attention
"We want to cut time by training in parallel"Transformer (self-attention)
"Thousands of time series in one model"DeepAR / Amazon Forecast
"Extract sentiment and entities right away, without training"Amazon Comprehend
"Recorded audio files to text"Amazon Transcribe
"Quickly use a pretrained large model"JumpStart / Bedrock

"It's the latest technology, so always Transformer" is a wrong path. If the question has clues such as "managed and fast" or "no ML staff," the answer is not a model structure but a managed AI service.

Symptom → Cause → Remedy

SymptomCauseFirst remedy
Cannot use information from the start of a long sentenceLong-term dependency failure (vanishing gradients)LSTM/GRU, or Transformer
Loss suddenly explodes during trainingExploding gradientsGradient clipping, smaller learning rate
Translation quality is bad only on long sentencesFixed context vector bottleneckIntroduce attention
Adding GPUs does not speed up trainingThe sequential processing structure of RNNsSwitch to the Transformer family
Building a separate model per time series is unmanageableIndividual-model approachGlobal model with DeepAR/Forecast
Almost no labeled textInsufficient supervised dataFine-tune a pretrained model (JumpStart)

The next post covers how these structures are actually trained — loss, optimizers, learning rate, regularization, and transfer learning.

📖 Glossary

  • Sequence data: data where the order itself carries meaning. Text, time series, speech, and logs qualify.
  • Hidden state: the RNN's memory vector that carries information up to the previous step and passes it to the next.
  • BPTT: applying backpropagation to an RNN unrolled along the time axis. The reason vanishing and exploding gradients get severe.
  • Long-term dependency: the property that information from the distant past must be reflected in the current output. A point where simple RNNs are weak.
  • Cell state / gates: the path the LSTM uses to carry information over long distances / switches learned to control what to erase, add, and emit.
  • seq2seq (encoder-decoder): a structure that compresses an input sequence into a vector and then generates an output sequence of a different length.
  • Attention: a mechanism that references the relevant parts of the input by weighting them at each output step. It resolves the bottleneck.
  • Self-attention: attention applied to every pair of positions inside a single sequence. The foundation of the Transformer.
  • Positional encoding: a device that separately adds the order information attention does not know to the input.
  • DeepAR: a SageMaker built-in time-series forecasting algorithm that learns many related time series together.

📝 Practice Questions

Click a choice to reveal the answer and explanation.

Question 1

What is the fundamental reason a simple RNN cannot make good use of information from the beginning of a long sentence at its end?

Question 2

Which of the following architectures processes sequences with self-attention alone and no RNN, making it strong in parallel training and long-range dependencies?

Question 3

You want to forecast future demand for many products (SKUs) at once. Which built-in algorithm in SageMaker is MOST suitable?

Question 4

What is the main benefit of adding attention to seq2seq?

Question 5

Which statement about the general characteristics of a GRU compared with an LSTM is correct?

PreviousCNN: Convolutional Neural Networks and Computer VisionWeek 7 · Day 2Next Training Techniques and Transfer LearningWeek 7 · Day 4

Official references

  • MLS-C01 official exam page(opens in a new tab)
  • Amazon SageMaker AI documentation(opens in a new tab)
  • Amazon Bedrock documentation(opens in a new tab)

On this page

  • Key Takeaways
  • Data Where Order Creates Meaning
  • The Basic Idea of RNNs
  • Sequence Task Types
  • LSTM and GRU
  • RNN · LSTM · GRU · Transformer Comparison
  • seq2seq (Encoder-Decoder)
  • Attention
  • Transformer
  • AWS Mapping for Time Series and NLP
  • How to Turn Text into Numbers
  • Training Your Own vs Managed AI Services
  • Reading Time-Series Regression Metrics
  • Question Clue → Answer Mapping
  • Symptom → Cause → Remedy
  • Glossary
  • Practice Questions