National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · 16:00–17:00 · 19 October 2026

From Attention to a Transformer

Attention mixes positions. A complete decoder must also represent order and predict the next token.

Machine Learning in Engineering Science Fall 2026 · Week 07 · Hour 3 Chi-Hua Jonny Yu · NCKU / LAiMM

Token + positionMasked attention + residualTokenwise MLP + residualNext-token logitsA complete causal decoder, not attention alone
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202600:00–02:00 · 01/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

This hour: assemble and test a decoder

Trace the complete computation, then test whether future tokens can affect earlier predictions.

Goal: explain a block and preserve a causal training experiment.

00–22 minPositions + the complete block22–36 minShifted targets + CPU notebook36–50 minBaseline + intervention checks50–60 minGeneration + limits + summary
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202602:00–04:00 · 02/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Position information supplies order

Unmasked attention alone is permutation-equivariant when positions are absent.

Attention(PX) = P Attention(X)

Same tokens, different orderARepresent order using positionsB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202604:00–06:00 · 03/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

There are several position mechanisms

The teaching model adds learned position vectors; the original Transformer used sinusoidal encodings.

zₜ = token_embedding(idₜ) + position_embedding(t)

Token vectorPosition vectorElementwise sumAttention input
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202606:00–08:00 · 04/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Multiple heads learn multiple projections

Each head routes information in its own learned query/key/value subspace.

D = H × dhead

Input width D = 32Head 1dhead = 8Head 2dhead = 8Head 3dhead = 8Head 4dhead = 8Concatenate + output projection
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202608:00–10:00 · 05/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Concatenate heads, then project

Head outputs are joined and mixed by a learned output projection.

MultiHead = Concat(head₁,…,headH) WO

Four head outputsConcatenate to width DOutput projectionMixed representation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202610:00–12:00 · 06/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

The feed-forward network transforms each token

The same nonlinear MLP is applied independently at each position.

FFN(x) = W₂ GELU(W₁x + b₁) + b₂

Width DExpand to 4DGELUReturn to D
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202612:00–14:00 · 07/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Residual paths preserve an identity route

A sublayer adds a learned update to its input representation.

y = x + Sublayer(x)

input xlearned updateAdd inputidentity path
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202614:00–16:00 · 08/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Normalization placement changes the block

Our teaching decoder normalizes before each sublayer.

x ← x + Attention(LN(x))

LayerNormMasked attentionAdd inputidentity pathThen: normalized feed-forward update + residual
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202616:00–18:00 · 09/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

An encoder uses bidirectional context

Encoder self-attention can access all valid input positions.

0.400.200.100.300.200.500.200.100.100.200.400.300.300.100.200.40Key / source position →Rows: queries / receiving positions
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202618:00–20:00 · 10/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

A decoder-only model hides the future

Masked self-attention supports next-token prediction from a prefix.

Key / source position →Rows: queries / receiving positions× = blocked before softmax
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202620:00–22:00 · 11/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

The original model had encoder and decoder

Decoder queries can retrieve from the separate encoder outputs.

Encoder: full self-attention + FFNADecoder: causal self-attention + cross-attention + FFNB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202622:00–24:00 · 12/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Predict the next token at every position

Shift targets one step ahead of inputs.

input: [0,1,2,3] target: [1,2,3,4]

01122334Input token positionsTargets are shifted one token ahead
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202624:00–26:00 · 13/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Cross-entropy scores the true next token

The model should assign high probability to observed next-token targets.

L = −mean log p(yₜ | x≤ₜ)

2.079A0.693B0.223CIllustrative values; see the stated calculation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202626:00–28:00 · 14/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Training is parallel across known positions

The causal mask permits computing many training losses together.

Training: known sequence, masked positionsAGeneration: unknown next token, append and repeatB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202628:00–30:00 · 15/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Open a tiny model you can inspect

One block, width 32, four heads, and eight symbols keep the experiment small.

Context length: 16Model width: 32 / heads: 4Vocabulary size: 8 / CPU only
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202630:00–32:00 · 16/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Define what generalization means here

Separate streams use different noise seeds but share the same synthetic process.

TrainingTraining stream seed 2026ValidationValidation stream seed 2027TestTest stream seed 2028Time increases →
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202632:00–34:00 · 17/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Read the four-dimensional score tensor

Every head produces a query-by-key grid for each example.

scores: (B, H, T, T)

B = examplesH = attention headsT × T = query/key pairs
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202634:00–36:00 · 18/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Trace the model before training

The output has one vocabulary-logit vector per input position.

IDs: (B,T) → logits: (B,T,V)

Token + positionOne decoder blockFinal normalizationVocabulary logits
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202636:00–38:00 · 19/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Fit a training-only bigram baseline

Estimate the next state from the current state using transition counts.

P(next | current) = smoothed count / row total

0.100.600.200.100.200.100.600.100.100.300.100.500.500.200.200.10Key / source position →Rows: queries / receiving positions
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202638:00–40:00 · 20/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Select by validation, evaluate test afterward

Keep the checkpoint with lowest validation negative log-likelihood.

Select by validation, evaluate test afterward — actual CPU notebook output on synthetic teaching data
Actual CPU notebook run · synthetic teaching data
Machine Learning in Engineering Science · 19 Oct 202640:00–42:00 · 21/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Inspect actual learned attention

Rows are receiving positions; columns are source positions.

Inspect actual learned attention — actual CPU notebook output on synthetic teaching data
Actual CPU notebook run · synthetic teaching data
Machine Learning in Engineering Science · 19 Oct 202642:00–44:00 · 22/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

A future intervention must preserve earlier logits

Change tokens after position 7; logits at positions 0–7 should remain identical.

Δearlier = max |old_logits − new_logits|

01234567012378910Original prefix versus modified futureCompare earlier logits: expected Δ = 0
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202644:00–46:00 · 23/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Test the position claim precisely

Permute inputs to unmasked attention without positional information.

Permute input rowsAOutput rows permute correspondinglyB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202646:00–48:00 · 24/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Generate one token, then append it

Autoregressive decoding grows the prefix sequentially.

PrefixNext-token logitsChoose one tokenAppend and repeat
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202648:00–50:00 · 25/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Long context has a measurable cost

Dense attention pair counts grow as T² at fixed head count.

T=16: 256 pairs T=32: 1024 pairs

256T=161024T=324096T=6416384T=128Dense score entries per head
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202650:00–52:00 · 26/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Choose models using task evidence

Streaming state, context length, data size, and compute budget all matter.

LSTM: recurrent state and sequential updatesATransformer: attention context and parallel trainingB
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202652:00–54:00 · 27/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

A token generator is one agent component

An agent also needs tools, state management, permissions, and a stopping rule.

Model proposesController validatesTool executesEvidence + stop rule
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202654:00–56:00 · 28/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Preserve the experiment, not just a score

Record data identity, configuration, baseline, checkpoint, and observed tests.

Data + seedVersions + configBaseline + resultsCausal check + limitation
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202656:00–58:00 · 29/30
National Cheng Kung UniversityLAiMM Laboratory

W07 · H3 · Machine Learning in Engineering Science

Summary: architecture plus evidence

A causal decoder combines attention with position and feature transformations. Its predictions still need evidence.

prefix → masked decoder → next-token logits Check: baseline error + future-token intervention

RepresentToken vectors + position informationTransformMasked attention + tokenwise MLPCheckBaseline comparison + causal testTAKE HOME: a token generator is not a complete agent
Mechanism diagram / illustrative teaching values
Machine Learning in Engineering Science · 19 Oct 202658:00–60:00 · 30/30

← → navigate · N notes · R answer · O overview · F fullscreen