Monash University · FACULTY OF COMPUTER SCIENCE

ECE5179 Chap.11 Tokenisation, Positional Encoding and BERT

- one subject, every graph, every model, every mark
8 Chapters6-page Bible
Our own words - no uploaded lecturer files
Updated for this semester
Chapter 11 of 12 · ECE5179

Tokenisation, Positional Encoding and BERT

Week 11 builds the pieces that turn the transformer into a language model. Tokenisation cuts text into units, with subword schemes such as byte-pair encoding balancing vocabulary size against sequence length. Positional encodings write word order into the input, because attention alone cannot see it.

BERT then shows how an encoder is pre-trained by masked language modelling and fine-tuned with small heads for classification, entity tagging and question answering.

In this chapter

What this chapter covers

  • 01

    Character, word and subword tokens

  • 02

    Byte-pair encoding merge by merge

  • 03

    Permutation equivariance and the need for position

  • 04

    Properties of an ideal positional encoding

  • 05

    Sinusoidal, learned and rotary encodings

  • 06

    BERT pre-training by masked language modelling

  • 07

    Fine-tuning heads for classification, tagging and question answering

  • 08

    Encoder models versus left-to-right language models

Worked example · free

Sinusoidal positional codes for two positions

Q [4 marks]. Using the sinusoidal encoding PE(pos, 2i) = sin(pos/10000^(2i/d)) and PE(pos, 2i+1) = cos(pos/10000^(2i/d)) with d = 4, compute the codes for positions 1 and 2 and explain which dimensions separate nearby and distant positions. This mark allocation is our revision weighting and is not the University's assessment scheme.
Sinusoidal codes for three dimensions of a 16-feature model.Sinusoidal codes for three dimensions of a 16-feature model. The horizontal axis is token position; low dimensions oscillate quickly and higher dimensions slowly, so the combination of values is different at every position.dimension 0dimension 2dimension 4position
Sinusoidal codes for three dimensions of a 16-feature model. The horizontal axis is token position; low dimensions oscillate quickly and higher dimensions slowly, so the combination of values is different at every position.
  • 1With d = 4 there are two frequencies: the divisor is 10000^0 = 1 for dimensions 0 and 1, and 10000^(2/4) = 100 for dimensions 2 and 3.
  • 1Position 1: (sin 1, cos 1, sin 0.01, cos 0.01) = (0.8415, 0.5403, 0.0100, 1.0000).
  • 1Position 2: (sin 2, cos 2, sin 0.02, cos 0.02) = (0.9093, -0.4161, 0.0200, 0.9998).
  • 1The fast pair (dimensions 0 and 1) changes a lot between neighbours, while the slow pair barely moves and only separates positions that are far apart.
Position 1 encodes as (0.8415, 0.5403, 0.0100, 1.0000) and position 2 as (0.9093, −0.4161, 0.0200, 0.9998). Every entry stays within [−1, 1], and mixing fast and slow frequencies gives each position its own code.
Sia tip — Set the calculator to radians, and pair each sine with the cosine of the same angle; the two share one frequency.
Glossary

Key terms

Byte-pair encoding
Byte-pair encoding builds a subword vocabulary by repeatedly merging the most frequent adjacent pair of symbols.
Positional encoding
A positional encoding is a vector added to each token embedding to record the token's position in the sequence.
Masked language modelling
Masked language modelling trains an encoder to recover tokens that have been hidden or replaced in its input.
Fine-tuning
Fine-tuning continues training a pre-trained model, with a small task head, on a specific labelled task.
FAQ

Tokenisation, Positional Encoding and BERT FAQ

Why do transformers need positional encodings?

Dot-product attention is permutation equivariant: shuffling the input tokens simply shuffles the outputs in the same way. Two sentences with the same words in a different order would therefore look identical. Adding a position vector to each token makes order part of the input, so attention can use it.

What makes a good positional encoding?

The lectures list three properties: a unique code for every position, values that stay bounded, and the ability to generalise to sequences longer than those seen in training. Sinusoids of geometrically spaced frequencies meet all three, while plain integers grow without bound and fractions of the sequence length are not unique.

How does BERT differ from a left-to-right language model?

BERT is an encoder trained to fill in masked tokens using words on both sides, which suits understanding tasks such as classification and extraction. A left-to-right model predicts each token from the earlier ones only, which makes it natural for generating text but gives it less context for each prediction.

Why can a learned positional encoding not handle longer sequences?

A learned encoding stores one trainable vector per position up to the longest training length. Beyond that length there is simply no learned vector to use. A fixed formula such as the sinusoidal encoding defines a code for every position, and rotary encodings build relative position into the attention score itself.

How large are BERT-base and BERT-large?

BERT-base has 12 layers, a width of 768 and 12 attention heads, about 110 million parameters. BERT-large has 24 layers, a width of 1024 and 16 heads, about 340 million parameters. Both were pre-trained on BooksCorpus and English Wikipedia with a vocabulary of roughly 30,000 tokens.

Study strategy

Exam move

Run byte-pair encoding by hand on a tiny corpus of your own, recounting pairs after every merge, then tokenise an unseen word by replaying the merges in order. Compute sinusoidal encodings for two positions with a small model width and check them against the three properties.

For BERT, describe the masking recipe exactly as the lecture slides give it and match each downstream task to its head, stating which output vectors it reads and which loss it uses. When you meet a new tokeniser in practice, predict its splits for a long word, a rare word and a line of code before running it, then compare; the mismatches show which merges it learned from its training data.

Working through Tokenisation, Positional Encoding and BERT in ECE5179? Sia is AskSia’s AI Computer Science tutor — ask any ECE5179 Tokenisation, Positional Encoding and BERT question and get a clear, step-by-step explanation grounded in how ECE5179 is taught and assessed. Read this chapter free, then take your hardest questions to Sia.

A+Everything unlocked
Unlocks this Bible + all 161 of your Monash University subjects - and 1,000+ Bibles across every Australian university.
Sia - your ECE5179 tutor, unlimited, worked the way the exam marks it
The full 6-page Bible + practice bank with worked solutions
Chrome extension - sync your LMS so Sia knows your deadlines
Bilingual EN / Chinese on every Bible and every Sia answer
$0.99 Trial
30-day money-back · cancel in one tap · how it works
ECE5179 · Neural networks and deep learning - independent study guide on the AskSia Library. More Monash University subjects · Microeconomics across all universities