Skip to content
AriadneTechnology

The Outer Ring · Chamber 2 of 9

Sets: The Nouns of Mathematics

Membership, set-builder notation, unions, intersections and the number systems every paper quietly assumes.

35 min 50 XP + 12 questions + 1 challengeNotationVideoPapersProofsCodeLab

In this chamber you will

  • Read ∈, ⊆, ∪, ∩, ∖ and set-builder notation
  • Name the number systems ℕ, ℤ, ℚ, ℝ and read ℝⁿ and ℝᵐˣⁿ
  • Write a dataset as a set of pairs
  • Prove a De Morgan law and count with inclusion–exclusion
DiscoverLearnRead beyondPapers & lecturesYour turn

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

Z={(xi,yi)}i=1,..,N\mathcal{Z} = \{(x_i, y_i)\}_{i=1,..,N}
Hu et al. (2021), “LoRA: Low-Rank Adaptation of Large Language Models”, Section 2

The words around it say this is “a training dataset of context-target pairs”, where each xix_i is a prompt and each yiy_i 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 NN.

A few lines later, the paper describes its method as training “a much smaller-sized set of parameters Θ\Theta with ∣Θ∣≪∣Φ0∣|\Theta| \ll |\Phi_0|”, where Φ0\Phi_0 holds the weights of the original model. You met ≪\ll in Chamber 1. The vertical bars are new.

Quick check +20 XP

LoRA trains “a much smaller-sized set of parameters Θ\Theta with ∣Θ∣≪∣Φ0∣|\Theta| \ll |\Phi_0|”, where Φ0\Phi_0 holds the original model's parameters. Take a guess: what do the vertical bars mean here?

DiscoverLearnRead beyondPapers & lecturesYour turn

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:

{1,2,3}={3,1,2}={1,1,2,3}.\{1, 2, 3\} = \{3, 1, 2\} = \{1, 1, 2, 3\}.

Listing elements between curly braces is called roster notation. The key verb is ∈\in, “is an element of”: 2∈{1,2,3}2 \in \{1, 2, 3\} is true, and 4∉{1,2,3}4 \notin \{1, 2, 3\} says 4 is not an element. There's exactly one set with nothing in it, the empty set ∅\varnothing. The size of a finite set, written ∣A∣|A|, is how many elements it has. Mathematicians call it the cardinality.

Machine learning is full of sets. A language model has a vocabulary V\mathcal{V} of tokens, and ∣V∣|\mathcal{V}| is its vocabulary size. An image classifier picks from a set of labels such as {cat,dog,bird}\{\text{cat}, \text{dog}, \text{bird}\}. The LoRA sentence above says the set of trained parameters Θ\Theta has far fewer elements than the model's full set Φ0\Phi_0.

Sets can sit inside other sets. A⊆BA \subseteq B, “A is a subset of B”, means every element of AA is also in BB. A proper subset, A⊊BA \subsetneq B, adds that BB 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.

Membership and subsets
  • ∈\in“is an element of”
    Membership: x∈Ax \in A says the object xx belongs to the set AA. Also read “is in” or “belongs to”.
    3∈{1,2,3}3 \in \{1, 2, 3\}
  • ∉\notin“is not an element of”
    x∉Ax \notin A says xx does not belong to AA.
    4∉{1,2,3}4 \notin \{1, 2, 3\}
  • {a,b,c}\{a, b, c\}“the set containing a, b and c”
    Curly braces list a set's elements (roster notation). Order and repeats don't matter: {1,2}={2,1,1}\{1, 2\} = \{2, 1, 1\}.
    {cat,dog,bird}\{\text{cat}, \text{dog}, \text{bird}\}
  • ∅\varnothing“the empty set”
    The set with no elements at all, {}\{\}. Also printed ∅\emptyset.
    ∣∅∣=0|\varnothing| = 0
  • ∣A∣|A|“the size of A”
    The number of elements of a finite set AA, called its cardinality. Around a number the same bars mean absolute value, so check what's inside.
    ∣{2,4,6}∣=3|\{2, 4, 6\}| = 3
  • ⊆\subseteq“is a subset of”
    A⊆BA \subseteq B: every element of AA is also an element of BB. AA is allowed to equal BB.
    {1,2}⊆{1,2,3}\{1, 2\} \subseteq \{1, 2, 3\}
  • ⊊\subsetneq“is a proper subset of”
    A⊊BA \subsetneq B: A⊆BA \subseteq B and A≠BA \ne B, so BB has at least one element that AA lacks.
    N⊊Z\mathbb{N} \subsetneq \mathbb{Z}
  • ⊂\subset“is a subset of (strict or not, check)”
    Ambiguous: some authors mean ⊆\subseteq, others ⊊\subsetneq. ML papers mostly use it loosely for ⊆\subseteq. Check the paper's convention.
    S⊂RS \subset \mathbb{R}
Quick check +20 XP

Let A={1,2,3}A = \{1, 2, 3\}. 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:

{x∈S:P(x)}\{x \in S : P(x)\}

Read it “the set of xx in SS such that PP of xx”. 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 {x∣P(x)}\{x \mid P(x)\}, and read it the same way. A third form puts a formula on the left to build the elements: {2k:k∈Z}\{2k : k \in \mathbb{Z}\} is every integer doubled, which is the set of even numbers.

Here's one from machine learning. After a classifier makes predictions y^i\hat{y}_i for examples i=1,…,ni = 1, \ldots, n, the set of its mistakes is

M={ i∈{1,…,n}:y^i≠yi },M = \{\, i \in \{1, \ldots, n\} : \hat{y}_i \ne y_i \,\},

and its accuracy is 1−∣M∣/n1 - |M|/n. 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.)

Set-builder notation
  • {x∈S:P(x)}\{x \in S : P(x)\}“the set of x in S such that P of x”
    Go through SS and keep each xx that passes the test PP. The colon reads “such that”.
    {x∈Z:x>0}\{x \in \mathbb{Z} : x > 0\}
  • {x∣P(x)}\{x \mid P(x)\}“the set of all x such that P of x”
    The same idea with a vertical bar instead of a colon: {x∣x2=4}={−2,2}\{x \mid x^2 = 4\} = \{-2, 2\}. Where xx comes from should be clear from context.
    {x∣x2=4}\{x \mid x^2 = 4\}
  • {f(x):x∈S}\{f(x) : x \in S\}“the set of f of x for x in S”
    A formula on the left builds the elements: apply ff to every xx in SS and collect the results.
    {2k:k∈Z}\{2k : k \in \mathbb{Z}\}

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:

N⊊Z⊊Q⊊R.\mathbb{N} \subsetneq \mathbb{Z} \subsetneq \mathbb{Q} \subsetneq \mathbb{R}.

The natural numbers N\mathbb{N} count things, but 3−53 - 5 has no answer there, so we extend to the integers Z\mathbb{Z}. The integers can't do 1÷21 \div 2, so we extend to the rationals Q\mathbb{Q}, the fractions. Even fractions leave holes: the length 2\sqrt{2} isn't a fraction (Chamber 3 proves it), so we fill the number line completely and get the reals R\mathbb{R}. Weights, losses, learning rates and probabilities all live in R\mathbb{R}. Computers store floating-point approximations of real numbers, but papers write R\mathbb{R}.

Subscripts trim these sets down. R≥0\mathbb{R}_{\ge 0} is the non-negative reals, home of distances and variances. R+\mathbb{R}_{+} is either the positive or the non-negative reals, depending on the author. Intervals are the pieces of R\mathbb{R} between two endpoints, and the bracket shape says whether each endpoint is included. A probability satisfies p∈[0,1]p \in [0, 1], with both ends allowed. The sigmoid function (Chamber 4) outputs values in (0,1)(0, 1): it gets arbitrarily close to 0 and 1 but never reaches them.

Number systems and intervals
  • N\mathbb{N}“the natural numbers”
    The counting numbers. Authors disagree about whether 0∈N0 \in \mathbb{N}, so check. N0\mathbb{N}_0 or Z≥0\mathbb{Z}_{\ge 0} removes the doubt.
    n∈Nn \in \mathbb{N}
  • Z\mathbb{Z}“the integers”
    Whole numbers, positive, negative and zero: {…,−2,−1,0,1,2,…}\{\ldots, -2, -1, 0, 1, 2, \ldots\}.
    −3∈Z-3 \in \mathbb{Z}
  • Q\mathbb{Q}“the rational numbers”
    Fractions p/qp/q with p,q∈Zp, q \in \mathbb{Z} and q≠0q \ne 0.
    34∈Q\tfrac{3}{4} \in \mathbb{Q}
  • R\mathbb{R}“the real numbers”
    Every point on the number line, including 2\sqrt{2} and π\pi. The home of weights, losses and learning rates.
    η∈R\eta \in \mathbb{R}
  • R≥0\mathbb{R}_{\ge 0}“the non-negative reals”
    Real numbers that are ≥0\ge 0: distances, variances, ReLU outputs.
    σ2∈R≥0\sigma^2 \in \mathbb{R}_{\ge 0}
  • R+\mathbb{R}_{+}“R plus”
    The positive reals, or the non-negative reals: authors differ, so check. Learning rates live here.
    η∈R+\eta \in \mathbb{R}_{+}
  • [a,b][a, b]“the closed interval from a to b”
    All reals xx with a≤x≤ba \le x \le b: both ends included.
    p∈[0,1]p \in [0, 1]
  • (a,b)(a, b)“the open interval from a to b”
    All reals xx with a<x<ba < x < b: both ends excluded. The same brackets also write an ordered pair, so read the context.
    σ(z)∈(0,1)\sigma(z) \in (0, 1)
  • [a,b)[a, b)“the half-open interval from a to b”
    All reals xx with a≤x<ba \le x < b. A square bracket includes its end, a round one excludes it.
    u∈[0,1)u \in [0, 1)
Quick check +20 XP

How many elements does {n∈Z:n2<10}\{n \in \mathbb{Z} : n^2 < 10\} 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:

A∪B={x:x∈A or x∈B}union: in either (or both)A∩B={x:x∈A and x∈B}intersection: in bothA∖B={x:x∈A and x∉B}difference: in A but not BAc={x∈U:x∉A}complement: not in A\begin{aligned} A \cup B &= \{x : x \in A \text{ or } x \in B\} && \text{union: in either (or both)} \\ A \cap B &= \{x : x \in A \text{ and } x \in B\} && \text{intersection: in both} \\ A \setminus B &= \{x : x \in A \text{ and } x \notin B\} && \text{difference: in } A \text{ but not } B \\ A^c &= \{x \in U : x \notin A\} && \text{complement: not in } A \end{aligned}

The complement needs a universe UU, the set of everything under discussion. Without one, “everything not in AA” would include every number, every word and every cat. Two sets with nothing in common, A∩B=∅A \cap B = \varnothing, are called disjoint. A training set and a test set should be disjoint, and Problem 2 below shows how to check.

Operations on sets
  • ∪\cup“union”
    A∪BA \cup B: everything in AA or in BB (or in both).
    {1}∪{2}={1,2}\{1\} \cup \{2\} = \{1, 2\}
  • ∩\cap“intersection”
    A∩BA \cap B: everything in both AA and BB.
    {1,2}∩{2,3}={2}\{1, 2\} \cap \{2, 3\} = \{2\}
  • ∖\setminus“set minus”
    A∖BA \setminus B: everything in AA that is not in BB. Some authors write A−BA - B.
    {1,2}∖{2,3}={1}\{1, 2\} \setminus \{2, 3\} = \{1\}
  • AcA^{c}“the complement of A”
    Everything in the universe UU that is not in AA: Ac=U∖AA^c = U \setminus A.
    (Ac)c=A(A^{c})^{c} = A
  • A‾\overline{A}“A bar, the complement”
    Another way to write the complement. In statistics the same bar means an average (xˉ\bar{x}, Chamber 1), so read the context.
    A∪B‾=A‾∩B‾\overline{A \cup B} = \overline{A} \cap \overline{B}
  • UU“the universal set”
    Everything under discussion. Complements are taken inside it. Also written Ω\Omega or U\mathcal{U}.
    A⊆UA \subseteq U
  • A∩B=∅A \cap B = \varnothing“A and B are disjoint”
    The two sets share no elements, like a training set and a test set should.
    Dtrain∩Dtest=∅\mathcal{D}_{\text{train}} \cap \mathcal{D}_{\text{test}} = \varnothing

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

Three sets, A, B and C, live inside a universe U, and together they cut it into 8 regions. Build an expression with the buttons and the regions it describes light up. Tap any region to see its membership. Then hit the six targets below: each one is described in words, and any expression that shades exactly the right regions counts.
ABCU

Tap a region to see which sets contain it.

A\phantom{A}

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

Challenge: Venn virtuosoShade all six target regions using set operations.+40 XP

Learn: proving De Morgan's law

In the lab, (A∪B)c(A \cup B)^c and Ac∩BcA^c \cap B^c 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 AA and BB in a universe UU, (A∪B)c=Ac∩Bc(A \cup B)^c = A^c \cap B^c.

The strategy is element chasing. Two sets are equal when each is a subset of the other, and to show X⊆YX \subseteq Y you take an arbitrary element of XX (one you know nothing about beyond x∈Xx \in X) and show it must be in YY.

Proof. First we show (A∪B)c⊆Ac∩Bc(A \cup B)^c \subseteq A^c \cap B^c. Let x∈(A∪B)cx \in (A \cup B)^c. By the definition of complement, x∉A∪Bx \notin A \cup B. If xx were in AA it would be in A∪BA \cup B, so x∉Ax \notin A. By the same argument, x∉Bx \notin B. So x∈Acx \in A^c and x∈Bcx \in B^c, which means x∈Ac∩Bcx \in A^c \cap B^c.

Now the other direction, Ac∩Bc⊆(A∪B)cA^c \cap B^c \subseteq (A \cup B)^c. Let x∈Ac∩Bcx \in A^c \cap B^c. Then x∉Ax \notin A and x∉Bx \notin B. To be in A∪BA \cup B an element must be in AA or in BB, and xx is in neither, so x∉A∪Bx \notin A \cup B. That is, x∈(A∪B)cx \in (A \cup B)^c.

Each side is a subset of the other, so the two sets are equal. ■\blacksquare

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 xx, and repacked the result. Because xx was arbitrary, the argument covers every element of every pair of sets. The second law, (A∩B)c=Ac∪Bc(A \cap B)^c = A^c \cup B^c, 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 ∣A∣+∣B∣|A| + |B| is tempting, but it counts every element of A∩BA \cap B twice. The fix is called inclusion–exclusion, and here is its derivation in full.

  1. 1

    Cut the union into disjoint pieces

    Every element of A∪BA \cup B is in exactly one of three pieces: only in AA, in both, or only in BB. The pieces are disjoint, so their sizes simply add:

    ∣A∪B∣=∣A∖B∣+∣A∩B∣+∣B∖A∣.|A \cup B| = |A \setminus B| + |A \cap B| + |B \setminus A|.
  2. 2

    Cut A the same way

    AA splits into the disjoint pieces A∖BA \setminus B and A∩BA \cap B, so ∣A∣=∣A∖B∣+∣A∩B∣|A| = |A \setminus B| + |A \cap B|. Rearranged:

    ∣A∖B∣=∣A∣−∣A∩B∣.|A \setminus B| = |A| - |A \cap B|.
  3. 3

    And B

    Likewise, ∣B∖A∣=∣B∣−∣A∩B∣|B \setminus A| = |B| - |A \cap B|.

  4. 4

    Substitute and simplify

    Put the last two lines into the first:

    ∣A∪B∣=(∣A∣−∣A∩B∣)+∣A∩B∣+(∣B∣−∣A∩B∣)=∣A∣+∣B∣−∣A∩B∣.|A \cup B| = \big(|A| - |A \cap B|\big) + |A \cap B| + \big(|B| - |A \cap B|\big) = |A| + |B| - |A \cap B|.

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 700+400−250=850700 + 400 - 250 = 850 mention at least one, and 1000−850=1501000 - 850 = 150 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 (a,b)(a, b) remembers which entry comes first: (1,2)≠(2,1)(1, 2) \ne (2, 1), whereas {1,2}={2,1}\{1, 2\} = \{2, 1\}. An ordered list of nn entries, (x1,…,xn)(x_1, \ldots, x_n), is an nn-tuple, and in a tuple repeats count too.

The Cartesian product A×BA \times B, read “A cross B”, is the set of all ordered pairs with the first entry from AA and the second from BB:

A×B={(a,b):a∈A, b∈B}.A \times B = \{(a, b) : a \in A,\ b \in B\}.

For example, {0,1}×{x,y}={(0,x),(0,y),(1,x),(1,y)}\{0, 1\} \times \{\text{x}, \text{y}\} = \{(0, \text{x}), (0, \text{y}), (1, \text{x}), (1, \text{y})\}. Laid out as a grid, with one row per element of AA and one column per element of BB, it has ∣A∣|A| rows and ∣B∣|B| columns, so ∣A×B∣=∣A∣⋅∣B∣|A \times B| = |A| \cdot |B|.

Now the sets you'll see in every paper come into focus:

  • R2=R×R\mathbb{R}^2 = \mathbb{R} \times \mathbb{R} is the plane: every point is a pair of real numbers.
  • Rn\mathbb{R}^n is the set of all nn-tuples of reals. A 28×2828 \times 28 greyscale image, like those in the MNIST digits dataset, is a list of 784 pixel values, so x∈R784\mathbf{x} \in \mathbb{R}^{784}.
  • Rm×n\mathbb{R}^{m \times n} is the set of all real grids with mm rows and nn columns. “W∈R512×64W \in \mathbb{R}^{512 \times 64}” is a complete sentence: WW is a real matrix with 512 rows and 64 columns. Chamber 6 reads these shapes in depth.
  • {1,…,K}\{1, \ldots, K\} is the usual label set of a classifier with KK classes.

Put together, a labelled dataset D={(xi,yi)}i=1n\mathcal{D} = \{(\mathbf{x}_i, y_i)\}_{i=1}^{n} with xi∈Rd\mathbf{x}_i \in \mathbb{R}^d and yi∈{1,…,K}y_i \in \{1, \ldots, K\} is simply a subset of a product: D⊆Rd×{1,…,K}\mathcal{D} \subseteq \mathbb{R}^d \times \{1, \ldots, K\}.

Tuples, products and the sets of ML
  • (a,b)(a, b)“the ordered pair a, b”
    Two objects in a fixed order: (1,2)≠(2,1)(1, 2) \ne (2, 1), unlike the set {1,2}={2,1}\{1, 2\} = \{2, 1\}.
    (xi,yi)(x_i, y_i)
  • (x1,…,xn)(x_1, \ldots, x_n)“the tuple x one to x n”
    An ordered list of nn entries, called an nn-tuple. Order and repeats both count.
    (3,1,3)≠(1,3)(3, 1, 3) \ne (1, 3)
  • A×BA \times B“A cross B”
    The Cartesian product: every ordered pair (a,b)(a, b) with a∈Aa \in A and b∈Bb \in B. It has ∣A∣⋅∣B∣|A| \cdot |B| elements.
    {0,1}×{a,b}\{0, 1\} \times \{a, b\}
  • Rn\mathbb{R}^n“R n”
    All nn-tuples of real numbers, R×⋯×R\mathbb{R} \times \cdots \times \mathbb{R} with nn factors. A vector x∈Rn\mathbf{x} \in \mathbb{R}^n is one element.
    x∈R784\mathbf{x} \in \mathbb{R}^{784}
  • Rm×n\mathbb{R}^{m \times n}“R m by n”
    All grids of real numbers with mm rows and nn columns: real matrices (Chamber 6).
    W∈R512×64W \in \mathbb{R}^{512 \times 64}
  • {1,…,K}\{1, \ldots, K\}“the set one to K”
    The integers from 1 to KK: the usual label set for a classifier with KK classes. Some papers write [K][K].
    y∈{1,…,K}y \in \{1, \ldots, K\}
  • {(xi,yi)}i=1n\{(x_i, y_i)\}_{i=1}^{n}“the set of pairs x i, y i for i from 1 to n”
    A dataset: one (input, label) pair for each index ii from 1 to nn.
    D={(xi,yi)}i=1n\mathcal{D} = \{(\mathbf{x}_i, y_i)\}_{i=1}^{n}
Quick check +20 XP

Which statement is true?

DiscoverLearnRead beyondPapers & lecturesYour turn

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 Proof

Richard 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 Learning

Marc 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 (b1,b2,b3)(b_1, b_2, b_3) from a set {b1,b2,b3}\{b_1, b_2, b_3\}, just as this chamber does. Definition 2.1 then says in so many words that Rm×n\mathbb{R}^{m \times n} is the set of all real-valued (m,n)(m, n)-matrices. You can read both pages now.

Lecture notes · free online · ~25 min

Mathematics for Computer Science

Eric 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 notation

Wikipedia

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 x∈Sx \in S matters: without one, Russell's paradox lets you “define” a set that can't exist.

DiscoverLearnRead beyondPapers & lecturesYour turn

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 MultiHead\mathrm{MultiHead} equation: Concat\mathrm{Concat} and Attention\mathrm{Attention} 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 dmodel=512d_{\text{model}} = 512 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 W0∈Rd×kW_0 \in \mathbb{R}^{d \times k}, B∈Rd×rB \in \mathbb{R}^{d \times r} and A∈Rr×kA \in \mathbb{R}^{r \times k}: 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, 2017

The 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, 2021

Fine-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 ∣⋅∣|\cdot|.

Decode the paper · Section 3.2.2, Multi-Head Attention

Attention Is All You Need

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Łukasz Kaiser, Illia Polosukhin · NIPS, 2017

+30 XP
WiQ∈Rdmodel×dk,  WiK∈Rdmodel×dk,  WiV∈Rdmodel×dv and WO∈Rhdv×dmodelW^Q_i \in \mathbb{R}^{d_{\text{model}} \times d_k},\; W^K_i \in \mathbb{R}^{d_{\text{model}} \times d_k},\; W^V_i \in \mathbb{R}^{d_{\text{model}} \times d_v} \text{ and } W^O \in \mathbb{R}^{hd_v \times d_{\text{model}}}

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.

∈\in
R\mathbb{R}
Rdmodel×dk\mathbb{R}^{d_{\text{model}} \times d_k}
WiQW^Q_i
ii
hh
hdvhd_v

Options

Watch

Mathematical Notation - The Language of MathematicsSarada Herke · 9 min

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.

DiscoverLearnRead beyondPapers & lecturesYour turn

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

+25 XP

Match each expression to the way a researcher would read it aloud.

x∈Ax \in A
A⊆BA \subseteq B
A∩BA \cap B
A∖BA \setminus B
∣A∣|A|
{x∈R:x>0}\{x \in \mathbb{R} : x > 0\}
Rm×n\mathbb{R}^{m \times n}

Options

Match · Maths ↔ Python

From sets to Python

+25 XP

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.

x∈Ax \in A
A∪BA \cup B
A∩BA \cap B
A∖BA \setminus B
∣A∣|A|
A⊆BA \subseteq B
{x2:x∈S, x>0}\{x^2 : x \in S,\ x > 0\}

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

+25 XP

Claim

For any sets AA and BB inside a universe UU: (A∩B)c=Ac∪Bc.(A \cap B)^c = A^c \cup B^c.

Tap lines in the order they should appear. Not every line belongs. Tap a line in your proof to send it back.

Your proof

  1. Pick the first line below.

Available lines

Prove it yourself

Removing a union

+40 XP

Claim

For any sets AA, BB and CC: A∖(B∪C)=(A∖B)∩(A∖C).A \setminus (B \cup C) = (A \setminus B) \cap (A \setminus C).

Preview

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

+20 XP

Let U={1,2,…,10 000}U = \{1, 2, \ldots, 10\,000\} and define three subsets with set-builder notation:

A={n∈U:n mod 3=0},B={n∈U:n mod 5=0},C={n∈U:n mod 7=0}.A = \{n \in U : n \bmod 3 = 0\}, \quad B = \{n \in U : n \bmod 5 = 0\}, \quad C = \{n \in U : n \bmod 7 = 0\}.

Here n mod 3=0n \bmod 3 = 0 means that 3 divides nn exactly. What is ∣A∪B∪C∣|A \cup B \cup C|, the number of integers from 1 to 10 000 (inclusive) that are multiples of 3, 5 or 7?

An exact integer (or a fraction like 7/12)

Problem 5·Standard

A leaky test set

+35 XP

LoRA (Hu et al., 2021) writes a task's training data as a set of context–target pairs, Z={(xi,yi)}i=1,..,N\mathcal{Z} = \{(x_i, y_i)\}_{i=1,..,N}. 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 ii, let

xi=i2 mod 1009,yi=i mod 3.x_i = i^2 \bmod 1009, \qquad y_i = i \bmod 3.

The training set uses the indices 1 to 800 and the test set the indices 801 to 1000 (all inclusive):

Ztrain={(xi,yi):1≤i≤800},Ztest={(xi,yi):801≤i≤1000}.\mathcal{Z}_{\text{train}} = \{(x_i, y_i) : 1 \le i \le 800\}, \qquad \mathcal{Z}_{\text{test}} = \{(x_i, y_i) : 801 \le i \le 1000\}.

Different indices can produce the same pair, so some test examples secretly appear in the training set. How many elements does Ztest∩Ztrain\mathcal{Z}_{\text{test}} \cap \mathcal{Z}_{\text{train}} have?

An exact integer (or a fraction like 7/12)

Problem 6·Challenge

No two ones in a row

+50 XP

A binary feature vector of length nn is an element of the Cartesian product {0,1}n={0,1}×⋯×{0,1}\{0, 1\}^n = \{0, 1\} \times \cdots \times \{0, 1\} (nn factors): an nn-tuple (x1,…,xn)(x_1, \ldots, x_n) with every xj∈{0,1}x_j \in \{0, 1\}. There are ∣{0,1}n∣=2n|\{0, 1\}^n| = 2^n of them.

Now keep only the vectors with no two adjacent ones:

Sn={x∈{0,1}n:xj xj+1=0 for every j∈{1,…,n−1}}.S_n = \{\mathbf{x} \in \{0, 1\}^n : x_j\, x_{j+1} = 0 \text{ for every } j \in \{1, \ldots, n - 1\}\}.

For example, (1,0,1)∈S3(1, 0, 1) \in S_3 but (1,1,0)∉S3(1, 1, 0) \notin S_3, and ∣S3∣=5|S_3| = 5. What is ∣S30∣|S_{30}|?

Checking all 2302^{30} (about a billion) tuples one at a time is far too slow in Python, so look for structure.

An exact integer (or a fraction like 7/12)

Key takeaways

  • A set records membership only: {1,2}={2,1,1}\{1, 2\} = \{2, 1, 1\}. x∈Ax \in A says an element belongs, A⊆BA \subseteq B compares two sets, and ∣A∣|A| counts elements.
  • Set-builder notation {x∈S:P(x)}\{x \in S : P(x)\} reads “the xx in SS such that P(x)P(x)”: where elements come from, then the test they pass.
  • The number systems nest: N⊊Z⊊Q⊊R\mathbb{N} \subsetneq \mathbb{Z} \subsetneq \mathbb{Q} \subsetneq \mathbb{R}. Square brackets include an interval's endpoint and round ones exclude it.
  • ∪\cup, ∩\cap, ∖\setminus and c^c are “or”, “and”, “but not” and “not”. De Morgan's laws, (A∪B)c=Ac∩Bc(A \cup B)^c = A^c \cap B^c and (A∩B)c=Ac∪Bc(A \cap B)^c = A^c \cup B^c, are proved by chasing an arbitrary element.
  • Inclusion–exclusion: ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣|A \cup B| = |A| + |B| - |A \cap B|.
  • Tuples keep order, sets don't. Products build the spaces of ML: x∈Rn\mathbf{x} \in \mathbb{R}^n, W∈Rm×nW \in \mathbb{R}^{m \times n}, and D⊆Rd×{1,…,K}\mathcal{D} \subseteq \mathbb{R}^d \times \{1, \ldots, K\}.

Checkpoint

Prove it to the labyrinth

Answer every question to clear this chamber. First-try answers earn the most XP.

0/8
Question 1 of 8 +20 XP

Let A={1,2,3,4}A = \{1, 2, 3, 4\} and B={3,4,5}B = \{3, 4, 5\}. What is A∖BA \setminus B?

Question 2 of 8 +20 XP

Which set is equal to (A∩B)c(A \cap B)^c for all sets AA and BB?

Question 3 of 8 +20 XP

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?

Question 4 of 8 +20 XP

Which number is in [0,1)[0, 1) but not in (0,1)(0, 1)?

Question 5 of 8 +20 XP

How many elements does {0,1}3={0,1}×{0,1}×{0,1}\{0, 1\}^3 = \{0, 1\} \times \{0, 1\} \times \{0, 1\} have?

Question 6 of 8 +20 XP

A paper writes D={(xi,yi)}i=1n\mathcal{D} = \{(\mathbf{x}_i, y_i)\}_{i=1}^{n} with xi∈Rd\mathbf{x}_i \in \mathbb{R}^{d} and yi∈{1,…,K}y_i \in \{1, \ldots, K\}. What is D\mathcal{D}?

Question 7 of 8 +20 XP

Which statement is true for every set AA?

Question 8 of 8 +20 XP

In the Transformer paper, each query projection satisfies WiQ∈Rdmodel×dkW_i^Q \in \mathbb{R}^{d_{\text{model}} \times d_k} with dmodel=512d_{\text{model}} = 512 and dk=64d_k = 64. How many real numbers does one matrix WiQW_i^Q hold?

End of the chamber

Clear this chamber

+50 XPSetSet-Builder NotationThe Number SystemsCartesian Product