Discover: a dataset, written as a set
Open almost any machine-learning paper and within a page or two you'll find its data described with set notation. Here is how the LoRA paper, the source of one of today's most widely used ways to fine-tune large language models, introduces the data for a task:
Spotted in the wild
The words around it say this is “a training dataset of context-target pairs”, where each is a prompt and each the text the model should produce. Read it aloud: “calligraphic Z is the set of pairs x i, y i, for i from 1 to N”. Three conventions are packed into that one line. The curly braces make a set. The round brackets make an ordered pair, context first and target second. The subscript range says there's one pair for every index from 1 to .
A few lines later, the paper describes its method as training “a much smaller-sized set of parameters with ”, where holds the weights of the original model. You met in Chamber 1. The vertical bars are new.
LoRA trains “a much smaller-sized set of parameters with ”, where holds the original model's parameters. Take a guess: what do the vertical bars mean here?
Learn: sets and membership
A set is a collection of distinct objects, called its elements. The only thing a set records is what's in it. Order doesn't matter and neither does repetition, so these are all the same set:
Listing elements between curly braces is called roster notation. The key verb is , “is an element of”: is true, and says 4 is not an element. There's exactly one set with nothing in it, the empty set . The size of a finite set, written , is how many elements it has. Mathematicians call it the cardinality.
Machine learning is full of sets. A language model has a vocabulary of tokens, and is its vocabulary size. An image classifier picks from a set of labels such as . The LoRA sentence above says the set of trained parameters has far fewer elements than the model's full set .
Sets can sit inside other sets. , “A is a subset of B”, means every element of is also in . A proper subset, , adds that has something extra. Two sets are equal exactly when each is a subset of the other, and that simple fact is the strategy behind every proof in this chamber.
- “is an element of”Membership: says the object belongs to the set . Also read “is in” or “belongs to”.
- “is not an element of”says does not belong to .
- “the set containing a, b and c”Curly braces list a set's elements (roster notation). Order and repeats don't matter: .
- “the empty set”The set with no elements at all, . Also printed .
- “the size of A”The number of elements of a finite set , called its cardinality. Around a number the same bars mean absolute value, so check what's inside.
- “is a subset of”: every element of is also an element of . is allowed to equal .
- “is a proper subset of”: and , so has at least one element that lacks.
- “is a subset of (strict or not, check)”Ambiguous: some authors mean , others . ML papers mostly use it loosely for . Check the paper's convention.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “is an element of” | Membership: says the object belongs to the set . Also read “is in” or “belongs to”. | ||
| “is not an element of” | says does not belong to . | ||
| “the set containing a, b and c” | Curly braces list a set's elements (roster notation). Order and repeats don't matter: . | ||
| “the empty set” | The set with no elements at all, . Also printed . | ||
| “the size of A” | The number of elements of a finite set , called its cardinality. Around a number the same bars mean absolute value, so check what's inside. | ||
| “is a subset of” | : every element of is also an element of . is allowed to equal . | ||
| “is a proper subset of” | : and , so has at least one element that lacks. | ||
| “is a subset of (strict or not, check)” | Ambiguous: some authors mean , others . ML papers mostly use it loosely for . Check the paper's convention. |
Let . Which statement is true?
Learn: set-builder notation
Rosters are fine for three elements. They're hopeless for a million, or for infinitely many. Set-builder notation describes a set by a rule instead:
Read it “the set of in such that of ”. Before the colon is where the candidates come from. After it is the test each candidate must pass. Some authors use a vertical bar instead of the colon, as in , and read it the same way. A third form puts a formula on the left to build the elements: is every integer doubled, which is the set of even numbers.
Here's one from machine learning. After a classifier makes predictions for examples , the set of its mistakes is
and its accuracy is . If you know Python, you've already written this. The set comprehension {i for i in range(n) if y_hat[i] != y[i]} is set-builder notation, almost symbol for symbol. (Python counts from 0, the paper from 1.)
- “the set of x in S such that P of x”Go through and keep each that passes the test . The colon reads “such that”.
- “the set of all x such that P of x”The same idea with a vertical bar instead of a colon: . Where comes from should be clear from context.
- “the set of f of x for x in S”A formula on the left builds the elements: apply to every in and collect the results.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “the set of x in S such that P of x” | Go through and keep each that passes the test . The colon reads “such that”. | ||
| “the set of all x such that P of x” | The same idea with a vertical bar instead of a colon: . Where comes from should be clear from context. | ||
| “the set of f of x for x in S” | A formula on the left builds the elements: apply to every in and collect the results. |
Learn: the number systems
Every paper assumes a few standard sets of numbers and names them in blackboard bold (Chamber 1). They fit inside each other, and each one fills a gap in the one before:
The natural numbers count things, but has no answer there, so we extend to the integers . The integers can't do , so we extend to the rationals , the fractions. Even fractions leave holes: the length isn't a fraction (Chamber 3 proves it), so we fill the number line completely and get the reals . Weights, losses, learning rates and probabilities all live in . Computers store floating-point approximations of real numbers, but papers write .
Subscripts trim these sets down. is the non-negative reals, home of distances and variances. is either the positive or the non-negative reals, depending on the author. Intervals are the pieces of between two endpoints, and the bracket shape says whether each endpoint is included. A probability satisfies , with both ends allowed. The sigmoid function (Chamber 4) outputs values in : it gets arbitrarily close to 0 and 1 but never reaches them.
- “the natural numbers”The counting numbers. Authors disagree about whether , so check. or removes the doubt.
- “the integers”Whole numbers, positive, negative and zero: .
- “the rational numbers”Fractions with and .
- “the real numbers”Every point on the number line, including and . The home of weights, losses and learning rates.
- “the non-negative reals”Real numbers that are : distances, variances, ReLU outputs.
- “R plus”The positive reals, or the non-negative reals: authors differ, so check. Learning rates live here.
- “the closed interval from a to b”All reals with : both ends included.
- “the open interval from a to b”All reals with : both ends excluded. The same brackets also write an ordered pair, so read the context.
- “the half-open interval from a to b”All reals with . A square bracket includes its end, a round one excludes it.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “the natural numbers” | The counting numbers. Authors disagree about whether , so check. or removes the doubt. | ||
| “the integers” | Whole numbers, positive, negative and zero: . | ||
| “the rational numbers” | Fractions with and . | ||
| “the real numbers” | Every point on the number line, including and . The home of weights, losses and learning rates. | ||
| “the non-negative reals” | Real numbers that are : distances, variances, ReLU outputs. | ||
| “R plus” | The positive reals, or the non-negative reals: authors differ, so check. Learning rates live here. | ||
| “the closed interval from a to b” | All reals with : both ends included. | ||
| “the open interval from a to b” | All reals with : both ends excluded. The same brackets also write an ordered pair, so read the context. | ||
| “the half-open interval from a to b” | All reals with . A square bracket includes its end, a round one excludes it. |
How many elements does have?
Learn: union, intersection, difference and complement
Four operations make new sets from old ones. Each has a one-line definition in set-builder notation, and each is secretly a logic word:
The complement needs a universe , the set of everything under discussion. Without one, “everything not in ” would include every number, every word and every cat. Two sets with nothing in common, , are called disjoint. A training set and a test set should be disjoint, and Problem 2 below shows how to check.
- “union”: everything in or in (or in both).
- “intersection”: everything in both and .
- “set minus”: everything in that is not in . Some authors write .
- “the complement of A”Everything in the universe that is not in : .
- “A bar, the complement”Another way to write the complement. In statistics the same bar means an average (, Chamber 1), so read the context.
- “the universal set”Everything under discussion. Complements are taken inside it. Also written or .
- “A and B are disjoint”The two sets share no elements, like a training set and a test set should.
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “union” | : everything in or in (or in both). | ||
| “intersection” | : everything in both and . | ||
| “set minus” | : everything in that is not in . Some authors write . | ||
| “the complement of A” | Everything in the universe that is not in : . | ||
| “A bar, the complement” | Another way to write the complement. In statistics the same bar means an average (, Chamber 1), so read the context. | ||
| “the universal set” | Everything under discussion. Complements are taken inside it. Also written or . | ||
| “A and B are disjoint” | The two sets share no elements, like a training set and a test set should. |
The best way to get a feel for these operations is to play with them. In the lab, three sets cut the universe into eight regions, and every expression you build lights up some of them. Watch for the moment two different expressions light up the same regions.
Interactive lab
Venn diagram lab
Tap a region to see which sets contain it.
Build an expression from the buttons.
Regions shaded
0/8
Targets hit
0/6
Targets
1. in both A and B
2. in neither A nor B
3. in A, but in neither B nor C
4. in exactly one of the three
5. in all three
6. in both A and B, or in C
Learn: proving De Morgan's law
In the lab, and lit up the same regions. Is that always true, for any sets at all, finite or infinite? Pictures can't settle that. A proof can.
Claim (De Morgan's law). For any sets and in a universe , .
The strategy is element chasing. Two sets are equal when each is a subset of the other, and to show you take an arbitrary element of (one you know nothing about beyond ) and show it must be in .
Proof. First we show . Let . By the definition of complement, . If were in it would be in , so . By the same argument, . So and , which means .
Now the other direction, . Let . Then and . To be in an element must be in or in , and is in neither, so . That is, .
Each side is a subset of the other, so the two sets are equal.
Look at what made it work. We never drew a picture or picked an example. We unpacked each symbol into its definition, reasoned about a single arbitrary , and repacked the result. Because was arbitrary, the argument covers every element of every pair of sets. The second law, , has the same proof with “and” and “or” swapped, and you'll put it together yourself at the end of this chamber.
Learn: counting with inclusion–exclusion
How big is a union? Adding is tempting, but it counts every element of twice. The fix is called inclusion–exclusion, and here is its derivation in full.
- 1
Cut the union into disjoint pieces
Every element of is in exactly one of three pieces: only in , in both, or only in . The pieces are disjoint, so their sizes simply add:
- 2
Cut A the same way
splits into the disjoint pieces and , so . Rearranged:
- 3
And B
Likewise, .
- 4
Substitute and simplify
Put the last two lines into the first:
Include both sets, then exclude the part you counted twice. For example, if 700 of 1000 training sentences mention a date, 400 mention a place and 250 mention both, then mention at least one, and mention neither. With three sets the pattern continues: add the three sizes, subtract the three pairwise overlaps, then add back the triple overlap, which the subtraction removed once too often. Problem 1 puts that to work.
Learn: tuples, products and the sets of machine learning
Sets forget order, but data often can't. An ordered pair remembers which entry comes first: , whereas . An ordered list of entries, , is an -tuple, and in a tuple repeats count too.
The Cartesian product , read “A cross B”, is the set of all ordered pairs with the first entry from and the second from :
For example, . Laid out as a grid, with one row per element of and one column per element of , it has rows and columns, so .
Now the sets you'll see in every paper come into focus:
- is the plane: every point is a pair of real numbers.
- is the set of all -tuples of reals. A greyscale image, like those in the MNIST digits dataset, is a list of 784 pixel values, so .
- is the set of all real grids with rows and columns. “” is a complete sentence: is a real matrix with 512 rows and 64 columns. Chamber 6 reads these shapes in depth.
- is the usual label set of a classifier with classes.
Put together, a labelled dataset with and is simply a subset of a product: .
- “the ordered pair a, b”Two objects in a fixed order: , unlike the set .
- “the tuple x one to x n”An ordered list of entries, called an -tuple. Order and repeats both count.
- “A cross B”The Cartesian product: every ordered pair with and . It has elements.
- “R n”All -tuples of real numbers, with factors. A vector is one element.
- “R m by n”All grids of real numbers with rows and columns: real matrices (Chamber 6).
- “the set one to K”The integers from 1 to : the usual label set for a classifier with classes. Some papers write .
- “the set of pairs x i, y i for i from 1 to n”A dataset: one (input, label) pair for each index from 1 to .
| Symbol | Say it | Meaning | LaTeX |
|---|---|---|---|
| “the ordered pair a, b” | Two objects in a fixed order: , unlike the set . | ||
| “the tuple x one to x n” | An ordered list of entries, called an -tuple. Order and repeats both count. | ||
| “A cross B” | The Cartesian product: every ordered pair with and . It has elements. | ||
| “R n” | All -tuples of real numbers, with factors. A vector is one element. | ||
| “R m by n” | All grids of real numbers with rows and columns: real matrices (Chamber 6). | ||
| “the set one to K” | The integers from 1 to : the usual label set for a classifier with classes. Some papers write . | ||
| “the set of pairs x i, y i for i from 1 to n” | A dataset: one (input, label) pair for each index from 1 to . |
Which statement is true?
Read beyond the course
Sets are the first chapter of nearly every book on proofs and discrete mathematics. These are free, and each goes a step past this chamber.
Book · free online · ~60 min
Book of ProofRichard Hammack · Chapter 1: Sets (§1.1–1.9)
A gentle, careful textbook, free as a PDF. Chapter 1 covers everything in this chamber and adds power sets and indexed sets, with plenty of exercises. Try a dozen of them to make the notation automatic. Chapter 2 is the logic behind Chamber 3.
Book · free online · ~15 min
Mathematics for Machine LearningMarc Peter Deisenroth, A. Aldo Faisal & Cheng Soon Ong · Table of Symbols (front matter) and §2.2, Definition 2.1
The book's own table of symbols separates a tuple from a set , just as this chamber does. Definition 2.1 then says in so many words that is the set of all real-valued -matrices. You can read both pages now.
Lecture notes · free online · ~25 min
Mathematics for Computer ScienceEric Lehman, F. Thomson Leighton & Albert R. Meyer · §4.1: Sets
MIT's discrete mathematics text, written for computer scientists. Its section on sets moves briskly through the same operations, with examples drawn from computing. It's also the companion to the lecture in Chamber 3.
Article · free online · ~10 min
Set-builder notationWikipedia
The colon and bar variants, formulas on the left, and a section on how programming languages borrowed the idea as comprehensions. It also explains why a domain like matters: without one, Russell's paradox lets you “define” a set that can't exist.
Papers and lectures
Set notation is how papers say what type of thing each symbol is, and you can now read those sentences in any paper. Two short passages are worth reading today.
In Attention Is All You Need, go to Section 3.2.2, Multi-Head Attention. It's half a page. Skim the displayed equation: and are named functions, and Chamber 9 reads attention in full. Then read slowly from “Where the projections are parameter matrices”. That sentence is four membership statements in a row, and it tells you the shape of every learned weight in the layer. The value comes from the first paragraph of Section 3.1.
In the LoRA paper, reread Section 2, the source of this chamber's opening, and then the first paragraph of Section 4.1. There you'll find , and : more membership statements that fix shapes. Skip the experiments for now. Chamber 6 and the Linear Algebra course come back to what the shapes mean.
Attention Is All You NeedAshish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin · NIPS, 2017The paper that introduced the Transformer. Its notation is compact and conventional, so every piece of set notation it uses is exactly what you learned here.
LoRA: Low-Rank Adaptation of Large Language ModelsEdward J. Hu, Yelong Shen, Phillip Wallis, Zeyuan Allen-Zhu, Yuanzhi Li, Shean Wang, Lu Wang, Weizhu Chen · ICLR 2022, 2021Fine-tunes a huge model by training a small set of extra parameters. Section 2 writes its data as a set of pairs and compares parameter counts with .
Decode the paper · Section 3.2.2, Multi-Head Attention
Attention Is All You NeedAshish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin · NIPS, 2017
The sentence that introduces the learned weights of multi-head attention. It is four statements of set membership in a row, and each one tells you a matrix's shape. Match each symbol to its meaning.
Options
Watch
A quick tour of notation for discrete mathematics, worked through small examples. The set theory part reviews this chamber, and the parts on quantifiers and on sums and products preview Chambers 3 and 5. Pause whenever a symbol appears and say it aloud before she does.
Your turn
First match symbols to words and to Python, which has sets built in. Then assemble one proof and write another, and finish with three problems in code.
Match · Symbols ↔ Read aloud as
Say it aloud
Match each expression to the way a researcher would read it aloud.
Options
Match · Maths ↔ Python
From sets to Python
Python has sets built in, and its operators mirror the maths closely. Match each expression to the Python that computes it, assuming A, B and S are Python sets.
Options
Proofs
The puzzle is De Morgan's second law: the same element chase as the proof above, with “and” and “or” traded. The proof you write yourself removes a union from a set. You can check your answer in the Venn lab first: it's one of the targets.
Proof puzzle
De Morgan's other law
Claim
For any sets and inside a universe :
Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.
Your proof
- Pick the first line below.
Available lines
Prove it yourself
Removing a union
Claim
For any sets , and :
Your typeset proof appears here.
Code it up
Python's sets make these direct: {... for ... if ...} builds a set, |, & and - combine sets, and len measures one. The last problem is too big for brute force, so you'll need to split a set cleverly.
Problem 4·Warm-up
Three sets of multiples
Let and define three subsets with set-builder notation:
Here means that 3 divides exactly. What is , the number of integers from 1 to 10 000 (inclusive) that are multiples of 3, 5 or 7?
Problem 5·Standard
A leaky test set
LoRA (Hu et al., 2021) writes a task's training data as a set of context–target pairs, . Set notation has a consequence that's easy to forget: a set never contains the same element twice, and two pairs are equal exactly when both of their entries match.
Here is a toy dataset. For each index , let
The training set uses the indices 1 to 800 and the test set the indices 801 to 1000 (all inclusive):
Different indices can produce the same pair, so some test examples secretly appear in the training set. How many elements does have?
Problem 6·Challenge
No two ones in a row
A binary feature vector of length is an element of the Cartesian product ( factors): an -tuple with every . There are of them.
Now keep only the vectors with no two adjacent ones:
For example, but , and . What is ?
Checking all (about a billion) tuples one at a time is far too slow in Python, so look for structure.
Key takeaways
- A set records membership only: . says an element belongs, compares two sets, and counts elements.
- Set-builder notation reads “the in such that ”: where elements come from, then the test they pass.
- The number systems nest: . Square brackets include an interval's endpoint and round ones exclude it.
- , , and are “or”, “and”, “but not” and “not”. De Morgan's laws, and , are proved by chasing an arbitrary element.
- Inclusion–exclusion: .
- Tuples keep order, sets don't. Products build the spaces of ML: , , and .
Checkpoint
Prove it to the labyrinth
Answer every question to clear this chamber. First-try answers earn the most XP.
Let and . What is ?
Which set is equal to for all sets and ?
A dataset has 200 images. 120 contain a person, 90 contain a car, and 50 contain both. How many images contain neither a person nor a car?
Which number is in but not in ?
How many elements does have?
A paper writes with and . What is ?
Which statement is true for every set ?
In the Transformer paper, each query projection satisfies with and . How many real numbers does one matrix hold?
End of the chamber
Clear this chamber
- Questions in this chamber (0/12 solved)Next unsolved
- Bonus: Venn virtuoso (+40 XP)
- Bonus: Problem 4: Three sets of multiples (+20 XP)
- Bonus: Problem 5: A leaky test set (+35 XP)
- Bonus: Problem 6: No two ones in a row (+50 XP)
- Bonus: Proof: De Morgan's other law (+25 XP)
- Bonus: Proof: Removing a union (+40 XP)
- Bonus: Decode the paper (+30 XP)
- Bonus: Match: Say it aloud (+25 XP)
- Bonus: Match: From sets to Python (+25 XP)