Back to Maths

Sets, relations, and functions

I'll first try to explain the short intro about this topic :

Before we talk about neural networks, gradients, probability or how machines learn there is a more basic question worth asking :

How do we describe the objects we are working with the relationships between them and the way one object becomes another ?

Imagine a machine learning model that looks at an image and predicts whether it contains a cat or a dog. We can think about this problem without knowing anything about the model's architecture right and we have a collection of possible images. we have a collection of possible labels and we have a rule that takes an image and produces a prediction.

first : lets discuss the :

1. Sets :

Suppose we have an apple a mango and a banana and If we care only about which kinds of fruit are available rather than where they are placed or how many of each we have, we can group them into one collection and give it a name.

A={Apple,Mango,Banana}A=\{\text{Apple},\text{Mango},\text{Banana} \}

Here now AA is the name of the collection

A set is a well defined collection of distinct objects now well defined means that membership has a precise meaning in the context of the problem for ex. “ +Ve even integers smaller than 10 ” gives us a clear rule right “ nice numbers ” does not, unless we explain what nice means.

There are two important consequences of describing a collection as a set.

1st thing is order does not matter here so the sets {1,2,3}\{1,2,3\} and {3,1,2}\{3,1,2\} are equal bcz they contain exactly the same elements and second is repetitions do not matter : {1,1,2,3,3}={1,2,3}\{1,1,2,3,3\}=\{1,2,3\}. A set describes membership, not position or frequency.

This is similar to the distinction between a list and a set in programming. A list can preserve duplicates and order whereas an ordinary mathematical set does neither.

so now

Membership : does an object belong ?

Let us choose a simple set : ex

A={2,4,6,8}A=\{2,4,6,8\}

select an even number and then an odd number. The original set stays the same only the membership question changes. like we can also describe a set using a membership rule rather than listing every element. the same set can be written as :

A={x∈N∣x>0, x<10, x is even}A=\{x\in\mathbb{N}\mid x>0,\ x<10,\ x\text{ is even}\}

so here, xx is a variable representing a candidate element, N\mathbb{N} denotes the natural numbers ( we use positive natural numbers here ).

I'm writing this bcz this is called *set builder notation It is particularly useful when a set is too large or infinite to list.

now lets move on the

Cardinality : how many distinct elements ?

Sometimes we care about the size of a collection rather than its contents the number of distinct elements of a finite set is called its cardinality

∣A∣=4|A|=4

For infinite sets cardinality describes their size in a more general sense we will not need that theory here.

So A set can also contain no elements this is called the empty set, written ∅\varnothing. Its cardinality is zero but notice the difference between ∅\varnothing and {∅}\{\varnothing\} : the 1st contains nothing and the 2nd contains exactly one element which happens to be an empty set like think of an empty box and a box containing an empty box.

now my fav question :

Why this matters in machine learning

If a classifier predicts one of three animal classes so its label space may be written as Y={Cat,Dog,Horse}Y=\{\text{Cat},\text{Dog},\text{Horse}\} and the letter YY names the collection of possible labels ∣Y∣=3|Y|=3 tells us there are three distinct classes.

a real training dataset is not necessarily a mathematical set. Two identical observations may occur more than once, and their frequency may matter during training. We can model examples using unique record identifiers, a sequence or a multiset when necessary. This distinction will matter when we talk about learning from data.

2. Subsets

Suppose our original collection is A={1,2,3}A=\{1,2,3\} and we create a smaller collection B={1,2}B=\{1,2\} by choosing some of its elements every element of BB also belongs to AA

We call BB a subset of AA and write:

B⊆AB\subseteq A

The symbol ⊆\subseteq is read as “ is a subset of ” The key condition is that no element of BB is outside AA. Formally :

B⊆A  ⟺  ∀x∈B, x∈AB\subseteq A\iff \forall x\in B,\ x\in A

The symbol   ⟺  \iff means “if and only if ” and ∀\forall means “ for every ”

every set is a subset of itself since every element it contains obviously belongs to it the empty set is also a subset of every set. Why ? To disprove containment we would need to find an element of the empty set that is missing from the other set no such element exists this is an example of vacuous truth

Proper subsets : containment without equality

Now suppose C={1,2,3}C=\{1,2,3\} both BB and CC are subsets of AA but there is a difference : C=AC=A, while BB leaves something out.

A proper subset is a subset that is not equal to its containing set

B⊊A  ⟺  B⊆A and B≠AB\subsetneq A\iff B\subseteq A\text{ and }B\ne A

The symbol ⊊\subsetneq means proper subset. select the equal set the empty set and the set containing 4. Which ones satisfy containment ? Which are proper subsets ?

One subtle but important distinction is between 2∈A2\in A and {2}⊆A\{2\}\subseteq A. The first is about a number belonging to a collection the second is about a collection containing that number being contained in another collection membership and subset are different relationships.

In machine learning if DD is a collection of uniquely identified examples and DtrainD_{\text{train}} is the collection chosen for training then Dtrain⊆DD_{\text{train}}\subseteq D. If some examples are held out, the training collection is a proper subset of DD.

3. Universal sets :

What does it mean for a number to be “ not even ” ? If we restrict our attention to integers we might answer with odd integers but what happens if we also consider fractions and real numbers ?

before we talk about what lies outside a set we need to specify what objects we are considering in the first place and that collection is called the universal set conventionally written UU.

For ex : suppose U={1,2,3,4,5}U=\{1,2,3,4,5\} and A={2,4}A=\{2,4\}. The numbers in our universe that are not in A are {1,3,5}\{1,3,5\}. If we expand U to include 6, 7 and 8, the answer changes, even though A stays exactly the same.

Try it: change the universe while keeping A fixed. Observe how the collection outside A grows.

A universal set is not a set of absolutely everything. It is the universe relevant to a particular problem. For a fashion search engine, it could be the entire available product catalog. For a probability problem, it could be the collection of possible outcomes of an experiment.

4. Power sets :

Imagine we want to predict house prices using three features : area, number of bedrooms and location we do not yet know which combination is useful. We might use only area, use area and location together, use every feature or even use no features to construct a baseline.

Instead of asking whether a particular subset exists, we now ask : what are all the subsets we could possibly choose ?

The collection of all subsets of a set A is called its power set, written P(A)\mathcal{P}(A).

If A={a,b,c}A=\{a,b,c\}, its power set contains the following eight sets :

P(A)={∅,{a},{b},{c},{a,b},{a,c},{b,c},{a,b,c}}\begin{aligned} \mathcal{P}(A)=\{&\varnothing,\{a\},\{b\},\{c\},\\ &\{a,b\},\{a,c\},\{b,c\},\{a,b,c\}\} \end{aligned}

The elements of a power set are themselves sets both the empty set and A itself must appear in it.

5. Set operations :

So far, we have described collections and their containment. Now we want to perform operations on them. We will use the same sets for the next four ideas :

U={1,2,3,4,5,6,7,8}A={1,2,3,4}B={3,4,5,6}\begin{aligned} U&=\{1,2,3,4,5,6,7,8\}\\ A&=\{1,2,3,4\}\\ B&=\{3,4,5,6\} \end{aligned}

A useful way to picture these collections is a Venn diagram. The outer rectangle represents the universe and two overlapping circles represent A and B each region describes a different membership condition

keep this explorer in mind as you read the next four operations switch between them and observe which region and elements are highlighted.

Union :

Imagine two teams collect training examples team A collects examples 1 through 4 and Team B collects examples 3 through 6. We want the combined collection of distinct examples, without counting shared records twice.

That is the union, denoted by ∪\cup :

A∪B={1,2,3,4,5,6}A\cup B=\{1,2,3,4,5,6\}

Its definition is :

A∪B={x∣x∈A∨x∈B}A\cup B=\{x\mid x\in A\lor x\in B\}

The symbol ∨\lor means logical OR. This is inclusive OR : an element belongs to the union if it belongs to A, B or both. Union is useful when combining vocabularies or merging distinct candidates from different retrieval systems.

Intersection :

Now imagine we want to find the examples collected by both teams. We are interested only in the overlap.

This is the intersection, denoted by ∩\cap:

A∩B={3,4}A\cap B=\{3,4\}
A∩B={x∣x∈A∧x∈B}A\cap B=\{x\mid x\in A\land x\in B\}

The symbol ∧\land means logical AND : both membership conditions must be true when two sets have an empty intersection we call them disjoint.

An important application is detecting overlap between training and testing data. For a conventional held out evaluation we usually require Dtrain∩Dtest=∅D_{\text{train}}\cap D_{\text{test}}=\varnothing for sample identifiers. This prevents shared records but it is not sufficient to prevent every form of data leakage : duplicate content related examples information from future data and preprocessing can also cause leakage

Difference :

Suppose Team A wants to identify the examples that Team B has not collected. We begin with A and remove everything also found in B.

This is the set difference, denoted by ∖\setminus

A∖B={1,2}A\setminus B=\{1,2\}
A∖B={x∣x∈A∧x∉B}A\setminus B=\{x\mid x\in A\land x\notin B\}

Unlike union and intersection difference generally depends on order here, B∖A={5,6}B\setminus A=\{5,6\}, which is not the same as A∖BA\setminus B.

If DD contains all collected data and CC contains the corrupted records we can describe the remaining collection as Dclean=D∖CD_{\text{clean}}=D\setminus C.

Complement :

Suppose A describes all in-stock products and U is the entire product catalog. We want the products that are not in A, but still belong to U. That is the complement of A, written AcA^c (the superscript c stands for complement).

Ac=U∖A={5,6,7,8}A^c=U\setminus A=\{5,6,7,8\}
Ac={x∈U∣x∉A}A^c=\{x\in U\mid x\notin A\}

Complement is a special case of difference : we start from the universe rather than an arbitrary other set. This is why the universe matters.

The same idea appears in probability. If E is the event that a classifier predicts correctly and its probability is P(E)=0.9P(E)=0.9, then P(Ec)=1−P(E)=0.1P(E^c)=1-P(E)=0.1. here P denotes the probability of an event and E and its complement cover all outcomes without overlap.

Putting the operations together

OperationMembership ruleResult for our example
A∪BA \cup BIn A or B, possibly both{1,2,3,4,5,6}\{1,2,3,4,5,6\}
A∩BA \cap BIn both A and B{3,4}\{3,4\}
A∖BA \setminus BIn A but not B{1,2}\{1,2\}
B∖AB \setminus AIn B but not A{5,6}\{5,6\}
AcA^cIn U but not A{5,6,7,8}\{5,6,7,8\}

One additional operation is the symmetric difference, written A△BA\triangle B. It contains the elements in exactly one of the two sets : (A∖B)∪(B∖A)={1,2,5,6}(A\setminus B)\cup(B\setminus A)=\{1,2,5,6\}. You can explore this option in the Venn diagram too.

Why some set identities work

When adding the sizes of A and B, the two common elements are counted twice. Subtracting the size of their intersection corrects this double counting :

∣A∪B∣=∣A∣+∣B∣−∣A∩B∣=4+4−2=6|A\cup B|=|A|+|B|-|A\cap B|=4+4-2=6

This is the two set inclusion exclusion principle for finite sets

Some other useful laws follow directly from the membership conditions. Union and intersection are commutative (swapping the sets does not change the result), and taking a complement twice returns the original set, provided A is contained in U.

Two especially useful identities are De Morgan's laws

(A∪B)c=Ac∩Bc(A\cup B)^c=A^c\cap B^c
(A∩B)c=Ac∪Bc(A\cap B)^c=A^c\cup B^c

Why ? To be outside the union, an element must be outside A and outside B. To be outside the intersection, it is enough to be outside A or outside B. Both identities assume the same universe for all complements.

You can already see the link to Boolean logic and search filters. If K is the collection of black products, L blue products, S shirts, P products priced below ₹2,000 and I in-stock products, then the result of the query “black or blue shirts under ₹2,000 that are in stock” can be expressed as:

R=(K∪L)∩S∩P∩IR=(K\cup L)\cap S\cap P\cap I

R is the collection satisfying the whole query. This is a mathematical description of filtering, independent of whatever search or AI model is used to rank the results.

6. Cartesian products :

Now we are moving from object to pairs

So far we know how to describe collections but what if we need to connect objects from two different collections ?

suppose a shop sells two colors, A={Black,Blue}A=\{\text{Black},\text{Blue}\}, in three sizes, B={S,M,L}B=\{\text{S},\text{M},\text{L}\}. What are all the possible color size combinations ?

We need to pair each color with each size. A pair such as ( Black, M ) has two positions : the first describes the color, and the second describes the size.

Unlike a set, an ordered pair preserves these positions. In general, (a,b)(a,b) and (b,a)(b,a) are different if a and b are different.

The Cartesian product of A and B is the set of all ordered pairs whose first element is from A and whose second element is from B:

A×B={(a,b)∣a∈A∧b∈B}A\times B=\{(a,b)\mid a\in A\land b\in B\}

here aa and bb represent elements of the first and second sets. The symbol ×\times between two sets denotes their Cartesian product, rather than ordinary number multiplication.

For a smaller numerical example :

A={1,2},B={a,b,c}A×B={(1,a),(1,b),(1,c),(2,a),(2,b),(2,c)}\begin{aligned} A&=\{1,2\},\quad B=\{a,b,c\}\\ A\times B&=\{(1,a),(1,b),(1,c),\\ &\qquad (2,a),(2,b),(2,c)\} \end{aligned}

For finite sets each element of A can pair with every element of B so ∣A×B∣=∣A∣ ∣B∣|A\times B|=|A|\,|B|. Juxtaposing the two cardinalities means multiplication. In our example, there are 2×3=62\times3=6 pairs.

Try it : increase the size of either set. Each new row or column introduces all the combinations with the other set.

This is the beginning of a mathematical description of a feature space. A house with a numerical area and a number of bedrooms can be represented by an ordered pair. More generally, an ordered tuple with d real-valued coordinates lives in Rd\mathbb{R}^d, where R\mathbb{R} denotes the real numbers and d is the number of coordinates. This is the ambient feature space; actual valid houses may occupy only a subset of it. We will return to vectors when we study linear algebra.

7. Relations :

A Cartesian product gives us every possible pair. But in real life, not every possible connection is valid. A shop might sell a black shirt in size M but not in size S. Or a recommendation system might know that one user likes two products while another user likes only one.

Suppose A={1,2}A=\{1,2\} and B={a,b,c}B=\{a,b,c\}. Instead of using all six possible pairs we select just three :

R={(1,a),(1,b),(2,c)}R=\{(1,a),(1,b),(2,c)\}

This selection is called a relation from A to B. More precisely a binary relation R from A to B is any subset of their cartesian product :

R⊆A×BR\subseteq A\times B

We can write 1 R a1\,R\,a to mean that (1,a)∈R(1,a)\in R. The relation itself does not impose a requirement that every element has a connection or that it has only one both the empty relation and the entire Cartesian product are valid relations.

Try it : add or remove pairs. One input can connect to several outputs, or to none at all. Every selection remains a valid relation.

There is a direct connection to power sets: if finite A has m elements and finite B has n elements, there are mnmn possible ordered pairs, and therefore 2mn2^{mn} possible relations from A to B. A relation is one element of the power set of the Cartesian product.

Properties of relations :

When both sides of a relation are the same set, we can ask questions about its internal structure. Consider a relation R on a set A, meaning R⊆A×AR\subseteq A\times A.

PropertyIntuitionExample on the integers
ReflexiveEvery element relates to itselfx≤xx \leq x
SymmetricIf xx relates to yy, then yy relates to xxEqual parity
AntisymmetricIf two elements relate in both directions, they must be equalLess than or equal to (≤\leq)
TransitiveIf xx relates to yy and yy relates to zz, then xx relates to zzLess than or equal to (≤\leq)

In the table, x, y and z are arbitrary elements, and ≤\le means less than or equal to. Each property is a condition that must hold for all relevant elements, not just for a single example.

Antisymmetry is not the opposite of symmetry. Equality, for example, is both symmetric and antisymmetric. Conversely, “is a friend of” (under the usual mutual interpretation) can be symmetric but need not be transitive: my friend's friend need not be my friend.

Try it : compare equality, less than or equal to, and the distance-at-most-one relation. The last one is reflexive and symmetric but fails transitivity: 1 is close to 2, and 2 is close to 3, but 1 is not close to 3 under this rule.

A relation that is reflexive, symmetric and transitive is an equivalence relation. For instance, “has the same parity as” groups integers into two disjoint classes: even and odd. More generally, an equivalence relation partitions its set into nonempty, disjoint equivalence classes whose union is the original set.

A relation that is reflexive, antisymmetric and transitive is a partial order. For example, subset containment ⊆\subseteq on a collection of sets is a partial order: not every two sets need be comparable, but containment has these three properties. Less than or equal to on real numbers is a total order, a partial order in which every pair is comparable.

In AI/ML, relations appear in graphs, knowledge bases and recommendation systems. For example, an observed user-product interaction is a pair in a relation between users and products. A single user can have many such interactions, so this relation need not be a function from users to products.

8. Functions :

Imagine an image classifier that must assign a single class label to every valid image. If we give it one image, we expect a prediction. If it has no prediction for some valid input, or produces two distinct class labels as the single output, it does not satisfy this particular mathematical specification.

We need a special kind of relation in which each input is connected to exactly one output. That is a function.

We write :

f:A→Bf:A\to B

here f is the name of the function, A is the input set, B is the declared output set, and the arrow →\to indicates a mapping from A to B. For any input x∈Ax\in A, its output is written f(x)∈Bf(x)\in B.

In set-theoretic terms, a function is a relation F⊆A×BF\subseteq A\times B satisfying the following condition :

∀x∈A, ∃! y∈B: (x,y)∈F\forall x\in A,\ \exists!\,y\in B:\ (x,y)\in F

The symbol ∃!\exists! means “there exists exactly one,” and the colon introduces the condition that must hold. In plain language: for every input x in A, there is exactly one output y in B connected to it. This set F of input-output pairs is also called the graph of f.

For example, if f(x)=2xf(x)=2x, input 3 produces output 6, and input 5 produces output 10. Here, the function rule is multiplication by 2. But functions do not have to be arithmetic formulas; a dictionary lookup, deterministic algorithm or trained neural network can also define a function.

Domain, codomain and range

Consider the mapping f:A→Bf:A\to B where A={1,2,3}A=\{1,2,3\}, B={a,b,c,d}B=\{a,b,c,d\}, and f(1)=af(1)=a, f(2)=af(2)=a, f(3)=cf(3)=c.

TermMeaningExample
DomainAll permitted inputs{1,2,3}\{1,2,3\}
CodomainThe declared set of permitted outputs{a,b,c,d}\{a,b,c,d\}
Range (image)Outputs actually reached by the function{a,c}\{a,c\}

We can write the range as f(A)={f(x)∣x∈A}f(A)=\{f(x)\mid x\in A\}. Notice that it is a subset of B. Two different inputs may produce the same output, and some codomain elements may never be used. Neither situation violates the definition of a function.

Injective, surjective and bijective functions

These words sound technical, but each asks a simple question about the arrows in a mapping.

Injective (one-to-one) : Can two different inputs produce the same output? If the answer is no, the function is injective. Formally, f(x1)=f(x2)⇒x1=x2f(x_1)=f(x_2)\Rightarrow x_1=x_2. Here, x1x_1 and x2x_2 are any two domain elements, and ⇒\Rightarrow means “implies.” No output has more than one input.

Surjective (onto) : Does every element of the codomain actually occur as an output? If yes, the function is surjective. Equivalently, f(A)=Bf(A)=B. Every codomain element has at least one input.

Bijective : A function is bijective when it is both injective and surjective. Each output has exactly one input, as well as each input having exactly one output. The mapping establishes a one-to-one correspondence between A and B.

Try it : first select Missing output and Two outputs. These are not functions. Then compare Injective only, Surjective only, Bijective and Neither. All four are valid functions, even though their arrows have different properties.

A useful subtlety: the codomain is part of the specification of a function. The rule f(x)=x2f(x)=x^2 from R\mathbb{R} to R\mathbb{R} is not surjective because negative real numbers never occur as outputs. If its codomain is changed to the nonnegative real numbers, it becomes surjective. It is still not injective on all real inputs because, for example, 2 and −2 produce the same output 4.

Composition

connection 1 fun to another that's set !!!

Suppose the output of one function becomes the input of another. This is function composition If f:A→Bf:A\to B and g:B→Cg:B\to C, then their composition is a function from A to C :

(g∘f)(x)=g(f(x))(g\circ f)(x)=g(f(x))

The symbol ∘\circ denotes composition, g is the second function and C is the final codomain. Read the expression from the inside outward: apply f to x first, and then apply g to the result. The codomain of f matching the domain of g guarantees that this composition is defined; more generally, every output of f must lie in the domain of g

For example, take f(x)=2xf(x)=2x and g(x)=x+3g(x)=x+3, both from the real numbers to the real numbers. For input 2, applying f first gives 4, then applying g gives 7. Reversing the order gives 5 first, then 10. In general, g∘fg\circ f need not equal f∘gf\circ g.

Try it: select a value for x, then reverse the order. The intermediate and final outputs update together.

Inverse function

simple question can we recover the original input ?

Suppose we know the output of a function and want to recover its input. An inverse function reverses the original mapping. A function f:A→Bf:A\to B has a two-sided inverse f−1:B→Af^{-1}:B\to A exactly when it is bijective.

For instance f(x)=2xf(x)=2x is a bijection from the real numbers to the real numbers, and its inverse is f−1(y)=y/2f^{-1}(y)=y/2. The superscript −1 denotes the inverse function, not the reciprocal of the output. Applying the function and its inverse in succession recovers the original input : f−1(f(x))=xf^{-1}(f(x))=x.

By contrast, f(x)=x2f(x)=x^2 on all real numbers loses information about the sign of its input. Given 4, we cannot determine whether the original input was 2 or −2. Restricting the domain and codomain to the nonnegative real numbers makes the square function bijective, with the square root as its inverse.

9. Suppose A machine learning model as a function

Return to the image classifier from the beginning. Let X be the set of valid input images and Y the set of possible class labels. A training example consists of an image x and its label y, represented by the ordered pair (x,y)∈X×Y(x,y)\in X\times Y.

A dataset of uniquely identified examples provides observations of such pairs. A model tries to learn a useful mapping from images to labels. For a trained deterministic classifier, we can write:

fθ:X→Yf_{\theta}:X\to Y
y^=fθ(x)\hat{y}=f_{\theta}(x)

The symbol θ\theta (theta) denotes the model's learned parameters, and y^\hat{y} (y-hat) denotes its predicted label, rather than the known target y. For fixed parameters and deterministic prediction rules, every valid input has exactly one predicted label.

There is an important nuance here: many classifiers first produce a vector of scores or probabilities, rather than a label. For three classes, a probability-output model can be written as :

pθ:X→Δ2p_{\theta}:X\to\Delta^2

here Δ2\Delta^2 is the set of ordered triples of nonnegative real numbers whose coordinates sum to one (the two-dimensional probability simplex). For example, (0.7,0.2,0.1)(0.7,0.2,0.1) might represent the probabilities assigned to Cat, Dog and Horse respectively. A separate decision rule then selects a label, typically the class with the largest score, with a defined tie-breaking rule. The prediction function is the composition of those two mappings.

A neural network itself is also built through function composition. If three layers are represented by functions f1f_1, f2f_2 and f3f_3, the network can be represented as :

F(x)=f3(f2(f1(x)))F(x)=f_3(f_2(f_1(x)))

The subscripts identify the layer order, and F names the entire network. Each layer transforms the output of the previous layer, provided their input and output spaces are compatible. Real architectures can have branches, residual connections and other operations, but their deterministic computation still defines a mapping. A stochastic model can be described as a function returning a distribution or as a function that also takes its random input explicitly.

I have not yet explained how those parameters are learne that will require linear algebra, calculus, probability and optimization. but we now have the language needed to define the learning problem itself ...

Keep learning : )