Mathematics for Artificial Intelligence
Every AI model is numbers moving through equations. This lesson builds that foundation from zero — so formulas in machine learning papers read like sentences, not walls of symbols.
Why Mathematics Matters in AI
An AI system does not "understand" a cat photo, a sentence, or a song. It receives numbers, transforms those numbers using equations, and produces new numbers that we then interpret as a prediction. Mathematics is not a prerequisite you tolerate before "the real work" — it is the real work. The model architecture, the training process, and the evaluation of results are all mathematical operations wearing an engineering costume.
The four pillars an AI engineer leans on
| Branch | What it gives AI |
|---|---|
| Algebra & functions | The language of models — every network is a function mapping input to output |
| Linear algebra | Vectors and matrices — how data, images, and words are actually stored and processed |
| Calculus | Derivatives — how a model learns from its mistakes (gradient descent) |
| Probability & statistics | How a model expresses uncertainty and makes decisions from patterns in data |
This lesson builds the ground floor beneath all four: how numbers behave, how to read and write equations, and how to think in functions and graphs. Nothing here is "too basic" — these are the exact tools used, at full speed, inside every neural network.
You cannot understand a loss function without logarithms, cannot understand a weight update without equations, and cannot understand a neuron's activation without functions and graphs. This lesson is sequenced so each topic unlocks the next.
Types of Data
Before any math happens, raw information has to become numbers. In AI, we sort data into a few basic families, because each family gets processed differently.
Turning categories into numbers
A model cannot multiply the word "red." So categorical data is converted — for example, with one-hot encoding: each category becomes a position in a list of 0s and a single 1.
Colors = {Red, Green, Blue}
Red \(\rightarrow (1,0,0)\) Green \(\rightarrow (0,1,0)\) Blue \(\rightarrow (0,0,1)\)
Structured vs. unstructured
Structured data fits neatly into rows and columns, like a spreadsheet (a customer's age, income, purchase count). Unstructured data — images, audio, free text — has no fixed grid of columns, so it needs extra preprocessing before it becomes clean numeric input.
A neural network's very first job is always this conversion. An image becomes a grid of numbers between 0 and 1 (pixel brightness); a word becomes a vector of numbers called an embedding. From that point on, everything is arithmetic.
Numbers
Numbers come in nested families — every family you learn contains the ones before it, plus something new.
| Set | Symbol | Contains | Example |
|---|---|---|---|
| Natural numbers | \(\mathbb{N}\) | Counting numbers | 1, 2, 3, 4... |
| Whole numbers | — | Naturals + zero | 0, 1, 2, 3... |
| Integers | \(\mathbb{Z}\) | Whole numbers + negatives | ..., -2, -1, 0, 1, 2... |
| Rational numbers | \(\mathbb{Q}\) | Any fraction of integers | \( \tfrac12, -\tfrac34, 5\) |
| Irrational numbers | — | Cannot be written as a fraction | \(\pi,\ \sqrt2\) |
| Real numbers | \(\mathbb{R}\) | Rationals + irrationals | everything on the number line |
The "weights" inside a neural network are real numbers, usually stored as decimals like 0.3821. Pixel values are integers from 0 to 255. Probabilities output by a model are real numbers restricted to the interval between 0 and 1. Knowing which "family" a number belongs to tells you what operations are even valid on it.
Fractions
A fraction \(\dfrac{a}{b}\) means "split something into \(b\) equal pieces and take \(a\) of them." The top number is the numerator; the bottom is the denominator.
The four operations
Multiply: \(\dfrac{a}{b}\times\dfrac{c}{d}=\dfrac{a\times c}{b\times d}\)
Divide: \(\dfrac{a}{b}\div\dfrac{c}{d}=\dfrac{a}{b}\times\dfrac{d}{c}\)
Add / subtract (common denominator first): \(\dfrac{a}{b}+\dfrac{c}{b}=\dfrac{a+c}{b}\)
As a decimal: \(\dfrac{3}{4}=0.75\)
A dataset of 1,200 images is split so that \(\tfrac{3}{4}\) go to training and the rest to testing. Training size \(=1200\times\tfrac34=900\) images. Testing size \(=1200-900=300\) images.
Fractions are everywhere in AI workflow decisions: an 80/20 train-test split, a dropout rate of \(\tfrac{1}{5}\) that randomly turns off neurons during training, or a model's output being a probability — which is really just a fraction between 0 and 1.
Powers and Roots
A power (exponent) is repeated multiplication: \(a^n\) means multiply \(a\) by itself \(n\) times.
\(2^4 = 2\times2\times2\times2 = 16\)
\(a^0 = 1\) \(a^{-n}=\dfrac{1}{a^n}\) \(a^{m}\times a^{n}=a^{m+n}\) \(\left(a^m\right)^n=a^{mn}\)
A root undoes a power. The square root asks "what number, multiplied by itself, gives this?"
\(\sqrt{16}=4\) because \(4\times4=16\)
General root: \(\sqrt[n]{a}=a^{1/n}\)
Distance between two points \((1,2)\) and \((4,6)\): \(d=\sqrt{(4-1)^2+(6-2)^2}=\sqrt{9+16}=\sqrt{25}=5\).
Squaring is the heart of measuring error: Mean Squared Error squares every mistake before averaging, so bigger mistakes are punished disproportionately. Square roots appear in measuring the "length" (magnitude) of vectors, and in the standard deviation used to normalize data before feeding it to a model.
Logarithms
A logarithm answers the reverse question of a power: "what exponent do I need to reach this number?"
\(\log_b(x) = y \iff b^y = x\)
\(\log_2(8) = 3\) because \(2^3=8\)
Natural log (base \(e\approx2.718\)): \(\ln(x)=\log_e(x)\)
Key properties
\(\log(ab)=\log a + \log b\)
\(\log\!\left(\dfrac{a}{b}\right)=\log a - \log b\)
\(\log(a^n)=n\log a\)
Logarithms are essential to how models are trained. The most common training loss for classification, cross-entropy / log loss, is literally built from \(-\log(p)\), where \(p\) is the probability the model assigned to the correct answer. Because \(-\log(p)\) blows up as \(p\to0\), the model is punished heavily for being confidently wrong.
If a model predicts probability \(p=0.9\) for the correct class, its loss is \(-\log(0.9)\approx0.105\) — small. If it predicts \(p=0.1\) for the correct class, its loss is \(-\log(0.1)\approx2.303\) — much larger. Confidence in the wrong direction is costly.
Functions
A function is a rule: put a number in, get exactly one number out. Think of it as a machine.
Domain and range
The domain is every valid input; the range is every possible output. For \(f(x)=\sqrt{x}\), the domain is \(x\geq0\) — you cannot feed it a negative number and get a real result.
Function families you'll meet constantly in AI
| Function | Formula | Where it shows up |
|---|---|---|
| Linear | \(f(x)=wx+b\) | The basic building block of every neuron |
| Sigmoid | \(f(x)=\dfrac{1}{1+e^{-x}}\) | Squashes any number into a probability (0 to 1) |
| ReLU | \(f(x)=\max(0,x)\) | The most common activation function in deep networks |
| Softmax | turns a list of numbers into probabilities that sum to 1 | Final layer of a classifier |
A neural network is, mathematically, one giant function built by stacking small functions inside each other: \(f(x) = f_3(f_2(f_1(x)))\). Every "layer" you hear about is one of these functions.
Graphs
A graph is a picture of a function or a relationship, drawn on a coordinate plane — two number lines crossing at a point called the origin \((0,0)\). The horizontal line is the \(x\)-axis; the vertical line is the \(y\)-axis. Every point is written as \((x,y)\).
Reading a graph
- Slope — how steep the line is, i.e. how much \(y\) changes per unit of \(x\).
- Intercept — where the line crosses the \(y\)-axis (the value of \(y\) when \(x=0\)).
- Trend — is the relationship going up, down, curving, or flat?
A training curve — loss going down over time — is a graph. A decision boundary that separates two classes on a scatter plot is a graph. Even a neural network's weights, when plotted, reveal patterns engineers use to debug and understand the model.
Variables and Constants
A variable is a symbol standing in for a number that can change — usually letters like \(x\), \(y\), \(w\). A constant is a fixed value that never changes within the problem, like \(2\), \(\pi\), or a specific known number.
In \(f(x) = 3x + 5\): \(x\) is the variable, \(3\) and \(5\) are constants.
Training a neural network means searching for the best values of its variables — the weights and biases — so the model's predictions match reality as closely as possible. Everything the model "learns" lives inside these adjustable numbers.
Equations
An equation is a statement that two expressions are equal. Solving it means finding the value of the variable that makes the statement true.
\(2x + 3 = 11\)
Subtract 3 from both sides: \(2x = 8\)
Divide both sides by 2: \(x = 4\)
Systems of equations
Sometimes multiple equations must be true at once — for example, finding the point where two lines cross. This is the base idea behind solving many small equations simultaneously, exactly what happens (at a massive scale) inside a trained network.
Training an AI model literally means solving (approximately) a giant equation: adjust the weights \(w\) so that the model's output \(f(x)\) is as close as possible to the true answer \(y\). The rule used to nudge each weight closer, over and over, is called the weight update equation: \(w_{\text{new}} = w_{\text{old}} - \eta\dfrac{\partial L}{\partial w}\).
Inequalities
An inequality compares two expressions without claiming they're equal — it says one is bigger, smaller, or at least/at most another.
\(x > 5\) (strictly greater) \(x \geq 5\) (greater or equal)
\(x < 5\) (strictly less) \(x \leq 5\) (less or equal)
Solve \(3x - 4 \leq 11\): add 4 to both sides → \(3x \leq 15\); divide by 3 → \(x \leq 5\). Flip the inequality sign only when multiplying or dividing by a negative number.
Inequalities define decision thresholds. A spam filter might classify an email as spam if the predicted probability satisfies \(p \geq 0.5\). Constraints in optimization ("keep this weight within a range") are also written as inequalities.
Solving Practical Problems
Every real problem — including AI ones — follows the same four moves.
Problem: A model correctly classifies 850 out of 1,000 test images. What is its accuracy as a percentage, and how many mistakes did it make?
Understand: we need a fraction turned into a percentage, plus a subtraction.
Translate: accuracy \(=\dfrac{850}{1000}\times100\); mistakes \(=1000-850\).
Solve: accuracy \(=85\%\); mistakes \(=150\).
Check: \(85\%\) of 1000 is 850 ✓, and \(850+150=1000\) ✓.
This exact four-step habit is what an engineer runs, mentally, dozens of times a day: reading a metric, translating a business requirement into a loss function, running the numbers, and sanity-checking the result before trusting it.
Examples from Artificial Intelligence
Here is how every topic in this lesson combines inside a real, working piece of AI.
1 · A single neuron
\(z = w_1x_1 + w_2x_2 + b\)
\(\hat{y} = \sigma(z) = \dfrac{1}{1+e^{-z}}\)
This uses variables (\(x_1,x_2\)), constants that get learned (\(w_1,w_2,b\)), a function (\(\sigma\)), and the number \(e\) tied to logarithms.
2 · Measuring how wrong the model is
Mean Squared Error: \(L = \dfrac{1}{n}\sum_{i=1}^n (y_i - \hat{y}_i)^2\)
Cross-Entropy Loss: \(L = -\log(\hat{y}_{\text{correct class}})\)
This uses powers (squaring), fractions (the average), and logarithms.
3 · Improving the model — gradient descent
\(w_{\text{new}} = w_{\text{old}} - \eta \cdot \dfrac{\partial L}{\partial w}\)
This is literally an equation being solved again and again, where \(\eta\) (the learning rate) is a small constant that controls the size of each step — and the step size must satisfy an inequality \(0 < \eta < 1\) to avoid the model overshooting.
An AI model is not magic. It is numbers flowing through functions, measured by an equation involving logarithms, corrected by another equation bound by an inequality — repeated until the mistakes shrink. Every section in this lesson is a gear in that machine.