Foundations
Sets, relations, and functions
The vocabulary every other idea is built from
The quiet vocabulary underneath everything else in this course: collections of things, the pairings between them, and the special pairings we call functions.
Three words carry an astonishing amount of weight in computer science and machine learning. A is a collection of distinct things. A pairs elements of one set with elements of another. A is a relation with one strict rule: every input gets exactly one output. When someone later says a neural network "learns a function," this is precisely the object they mean.
Three ideas, one sentence each
- Set: a bag of distinct items, order does not matter, .
- Relation: any collection of pairs, like "is-friends-with" linking people to people.
- Function: a relation that never gives one input two answers, is unambiguous.
Sets, and how we write them
A set is written with curly braces. Membership is the most basic question we can ask: is a thing in the set or not? We write ("3 is an element of ") and ("7 is not"). Elements are distinct, a set has no duplicates, and unordered.
Listing every element gets tedious, so we use : describe the elements by a rule. Read the colon as "such that".
The rule on the right of the colon is what makes sets scale to infinite collections you could never list by hand.
Four ways to combine sets
Given two sets and living inside some universe (the set of everything under discussion), four operations build new sets. Each has a set-builder definition, and each is just a plain-English condition on membership: union is in or ; intersection is in both; difference is in but not ; and complement is everything outside . Pick an operation below and watch which elements light up.
result = {1, 2, 3, 4, 5, 6}
Relations, and the functions among them
To pair things up, we first need a set of all possible pairs. The is every ordered pair with a first coordinate from and a second from . A relation is then just any subset of that product, any selection of pairs you care about.
A function is a relation obeying one extra law: every element of the input set is paired with exactly one element of the output set . No input left out, no input answered twice. Here is the domain (all legal inputs), is the codomain (the declared target set), and the image is the subset of that actually gets hit.
The one rule that makes it a function
Three shapes of function matter enough to have names. An injective (one-to-one) function never collapses two inputs onto the same output: . A surjective (onto) function reaches every corner of the codomain, so its image equals its codomain. A bijective function is both, a perfect one-to-one pairing, which is exactly what makes it reversible. And functions compose: feed the output of one into another, , the seed of how deep networks stack layers. Build a mapping below and read the verdict.
Draw some arrows to test the mapping.
Why this is the whole game
A function is just a lookup with the one-output rule.
# A function maps each input to exactly one output
f = {"cat": 0, "dog": 1, "bird": 2} # domain -> codomain
# A machine-learning model IS a function f: X -> Y
def model(x): # X = images, Y = labels
return classify(x) # returns exactly one label per imageCheck yourself
Which rule turns an ordinary relation into a function?
The property 'no two different inputs ever share the same output' is called:
What is the difference between a function's codomain and its image?
One is declared, the other is achieved. Try to state it, then check.
Lock it in
- A set is distinct, unordered items; a relation is any set of pairs.
- A function is a relation where every input has exactly one output.
- Injective avoids collisions, surjective covers Y, bijective does both.
- A trained model is literally a function f from X to Y.
Primary source
Where this reappears