Settings

Theme
Lite Mode (saves mobile data)

In Lite Mode, animations and videos don't play until you tap them, and their size is shown first. Interactive widgets still work.

Text size

Back up or move your progress →

Part 1: Math FoundationsPre-calculus Refresher

Functions and graphs

P1.A130 min●○○ BeginnerReviewed Nov 30, 2026

Why this matters for machine learning

Machine learning is the art of finding good functions: a model is a function, a loss is a function, and a neural network is functions stacked inside each other.

  • A model is a function from inputs to predictions, and learning means choosing the function's settings (parameters) so its outputs match the data. (P0.2: How a machine "learns")
  • Linear regression fits a straight line y = wx + b, and its error is a parabola. (P3.A2: Linear regression, coming soon)
  • A neural network is a composition of functions: the output of one layer is the input of the next. (P4.3: Multi-layer perceptrons, coming soon)
  • Activation functions such as ReLU and the step function are piecewise functions: ReLU gives a network its bends, and the step function makes a sudden jump. (P4.4: Activation functions, coming soon)
  • The loss is a function of the parameters, and optimization means finding the input where that function is lowest. (P1.F1: What is optimization?)

After this lesson you can

  • Explain a function as a rule that gives exactly one output for each input, and use the notation f(x)
  • Build a table of values, plot it, and read a graph, including the vertical line test
  • Find the domain and range of a simple function and say when an input is not allowed
  • Describe lines, parabolas, exponentials, absolute value and step functions, and say what each coefficient does
  • Shift, stretch and flip a graph, and compute and order compositions such as f(g(x))

A taxi charges 3 for getting in and 2 for every kilometre. Tell me how far you go and I can tell you the price: 5 km costs 13. Machine learning does the same trick with far more complicated “prices”: tell the model a photo and it tells you “cat”. Both are functions, and everything else in this course is built on them.

This is the first math lesson. No background is needed beyond basic arithmetic. We go slowly, with small numbers.

A function is a rule: one input, one output

A function is a rule that takes an input and gives back exactly one output. Picture a machine: you drop a number in the top, and one number comes out of the bottom.

Here is a machine that doubles the input and then adds 1. We give it the name ff and write the rule as

f(x)=2x+1f(x) = 2x + 1

Read “f(x)f(x)” as “f of x”: the output of ff when the input is xx. The letter xx stands for whatever input you choose. To use the rule, replace xx by a number:

f(3)=2×3+1=7f(0)=2×0+1=1f(−2)=2×(−2)+1=−3\begin{aligned} f(3) &= 2 \times 3 + 1 = 7 \\ f(0) &= 2 \times 0 + 1 = 1 \\ f(-2) &= 2 \times (-2) + 1 = -3 \end{aligned}

A table of values lists several inputs next to their outputs:

xx (input)−2-2−1-100112233
f(x)f(x) (output)−3-3−1-111335577

The one rule that makes it a function

For each allowed input there must be exactly one output. Two things follow, and students often mix them up:

  • One input giving two different outputs is not allowed. If a machine gave 5 one day and 6 the next for the same input, it would not be a function.
  • Two different inputs giving the same output is fine. For f(x)=x2f(x) = x^2 both f(2)f(2) and f(−2)f(-2) equal 4. Many inputs may share an output; no input may have two.
TableFunction?
1→2, 2→4, 3→61 \to 2,\ 2 \to 4,\ 3 \to 6Yes
1→5, 2→5, 3→51 \to 5,\ 2 \to 5,\ 3 \to 5Yes (a constant function: same output every time)
1→3, 1→4, 2→51 \to 3,\ 1 \to 4,\ 2 \to 5No: the input 1 has two outputs

Where the “machine” picture breaks

The machine picture is useful, but be careful about three things.

  • A function does not need a formula. A phone book that lists one number per name (name → number) or a spelling dictionary (word → meaning) is a function with no equation inside. A trained neural network has a formula, but it is a huge one that no human would write by hand. What makes something a function is the one-output-per-input rule, not an equation.
  • A function always gives the same output for the same input. Anything with randomness (rolling a die, or a chatbot that samples a word at random) is not a function of its input alone. For a chatbot, the probabilities it computes are a function of the input; the random pick is a separate step.
  • Real machines can jam; functions can too. Some inputs are simply not allowed. We come back to that under “domain”.

Several inputs, and vectors in

Many functions take more than one input. For example, f(x,y)=2x+yf(x, y) = 2x + y takes two numbers and gives one: f(3,4)=2×3+4=10f(3, 4) = 2 \times 3 + 4 = 10. A house-price model might take size, number of rooms and age, which is three inputs.

Machine learning bundles the inputs into one list called a vector and writes it as a single bold symbol x\mathbf{x}. A function from a vector to a number is then just a weighted sum:

f(x)=w⋅x+bfor examplew=(2,1), b=1, x=(3,4)⇒  f(x)=2×3+1×4+1=11\begin{gathered} f(\mathbf{x}) = \mathbf{w}\cdot\mathbf{x} + b \\ \text{for example} \\ \mathbf{w} = (2, 1),\ b = 1,\ \mathbf{x} = (3, 4) \\ \Rightarrow\; f(\mathbf{x}) = 2\times 3 + 1\times 4 + 1 = 11 \end{gathered}

(The dot product is the subject of P1.B3.) In this lesson we mostly use one number in, one number out, because those functions can be drawn.

Functions can also output more than one number (for example, the probabilities a classifier gives for each class). Matrices as functions on vectors come in P1.B5.

Variables versus parameters

In f(x)=2x+1f(x) = 2x + 1 the letter xx is the input: it changes every time you use the machine. The numbers 2 and 1 are parameters: settings that you choose once and then keep fixed while you try different inputs. Change them and you get a different function from the same family.

This split is the heart of machine learning. A model is a function of the input xx with parameters (written ww, bb or θ\boldsymbol{\theta}). Training means adjusting the parameters, never the input.

Four ways to describe a function

The same function can be shown in four ways, and you should be able to move between them:

WayExample for f(x)=2x+1f(x) = 2x + 1
Words“double the input, then add 1”
Formulaf(x)=2x+1f(x) = 2x + 1
Table0→1, 1→3, 2→5,…0 \to 1,\ 1 \to 3,\ 2 \to 5, \dots
Grapha straight line (next section)

Graphs: a picture of all the input–output pairs

To draw a graph, put the input on the horizontal axis (xx) and the output on the vertical axis (often called yy, or f(x)f(x)). Every row of the table becomes a point: input across, output up.

For f(x)=2x+1f(x) = 2x + 1 the points are (−2,−3)(-2, -3), (−1,−1)(-1, -1), (0,1)(0, 1), (1,3)(1, 3), (2,5)(2, 5), (3,7)(3, 7). They lie on a straight line. Joining up all inputs, not just the whole numbers, gives the line itself. The graph of a function is the set of all points (x,f(x))(x, f(x)).

How to read a graph:

  • To find f(a)f(a): go to x=ax = a on the horizontal axis, go straight up or down to the curve, then read the height.
  • To solve f(x)=kf(x) = k: draw a horizontal line at height kk and see where it meets the curve. Those crossings are the inputs that give output kk.
  • The y-intercept is where the graph meets the vertical axis, f(0)f(0). An x-intercept (or root) is where it meets the horizontal axis, an input with f(x)=0f(x) = 0. For f(x)=2x+1f(x) = 2x + 1 the root is x=−0.5x = -0.5, because 2(−0.5)+1=02(-0.5) + 1 = 0.

The vertical line test

The one-output rule has a picture: a curve is the graph of a function exactly when every vertical line crosses it at most once. If some vertical line crosses twice, that input has two outputs.

Vertical line test

y = x²/3 − 2

−6−4−2246−6−4−2246

At x = 1.5 the line crosses once: one output, −1.25. That is fine so far. A curve passes only if EVERY vertical line crosses at most once, so keep sliding.

Drag the pink handle on the horizontal axis, or focus it and use the arrow keys.

Choose a curve and slide the vertical line across it. A parabola that opens upward passes the test. A circle and a sideways parabola fail: some inputs give two outputs. Keyboard: Tab to the pink handle, then use the arrow keys.

A vertical line that crosses zero times is fine: that input is simply not allowed (it is outside the domain, next).

Domain and range

Not every number can be fed into every function.

  • The domain is the set of allowed inputs.
  • The range is the set of outputs that actually occur.

For f(x)=2x+1f(x) = 2x + 1 every number works as an input and every number can come out, so both are “all real numbers” (R\mathbb{R} is the symbol for all real numbers, the whole number line). Other functions are pickier:

FunctionDomain (allowed xx)Range (outputs)Why
2x+12x + 1all real numbersall real numbersnothing can go wrong
x2x^2all real numbers00 or morea square is never negative
x\sqrt{x}00 or more00 or moreno real number squares to a negative
1/x1/xevery number except 00every number except 00you cannot divide by 0
log⁡x\log xgreater than 00all real numbersa logarithm needs a positive input (P1.A2)
2x2^xall real numbersgreater than 00a power of 2 is always positive
max⁡(0,x)\max(0, x) (ReLU)all real numbers00 or moreit clips negatives to 0 (P4.4)
σ(x)=11+e−x\sigma(x) = \dfrac{1}{1 + e^{-x}} (sigmoid)all real numbersbetween 00 and 11squashes everything into a probability-like range (P1.A5)

Why does this matter in practice? If you feed a negative number to a logarithm or a square root, or divide by zero, you get an error or nan (“not a number”) in code, and a training run can be ruined. Knowing the domain tells you where a formula is safe.

Interval notation writes domains and ranges compactly. A square bracket includes the end, a round bracket excludes it: [0,∞)[0, \infty) means 0 or more (0 is allowed); (0,∞)(0, \infty) means strictly more than 0 (0 is not allowed). The symbol ∞\infty (“infinity”) is not a number, so it always gets a round bracket. (P1.A4 covers more notation.)

A domain can also come from meaning, not only from arithmetic: a function that gives the price of a taxi ride from the distance dd in km only makes sense for d≥0d \ge 0.

Finding the domain of a formula: ask “is there any input that makes me divide by zero, take the square root of a negative number, or take the logarithm of something that is not positive?” Exclude exactly those. Example: x−2\sqrt{x - 2} needs x−2≥0x - 2 \ge 0, so the domain is x≥2x \ge 2 (and x=2x = 2 itself is allowed, since 0=0\sqrt{0} = 0).

The shapes you will meet most

Five families cover most of what machine learning draws. Use the plotter: pick a family, move the sliders, and drag the pink marker to see the input xx turn into the output f(x)f(x).

Function plotter

f(x) = 2x + 1

−6−4−2246−6−4−2246x (input)f(x) (output)
Input x = 1  →  output f(1) = 3
f(1) = 2 × 1 + 1 = 3

Slope 2 (positive): increasing. Every +1 in x adds 2 to the output. The line crosses the vertical axis at 1, because f(0) = 1.

Domain: all real numbers (every x works). Range (outputs you can get): all numbers.

Drag the pink marker along the curve, or focus it and use the arrow keys. The blue dot is the input, the orange dot the output.

Function plotter. Pick a family and move the coefficient sliders; the formula updates with your numbers. Drag the pink marker (or focus it and use the arrow keys) to read off x → f(x), with the arithmetic shown under the graph.

Lines: slope and intercept

A linear function has the form

f(x)=mx+bf(x) = mx + b

Its graph is a straight line. The two parameters have plain meanings:

  • mm, the slope, is the change in output per 1 step of input. The taxi example f(x)=2x+3f(x) = 2x + 3 has slope 2: every extra kilometre adds 2 to the price.
  • bb, the intercept, is the output when the input is 0, f(0)=bf(0) = b (the starting charge, 3 in the taxi).

Given two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) on a line, the slope is “rise over run”:

m=y2−y1x2−x1m = \frac{y_2 - y_1}{x_2 - x_1}

For (1,3)(1, 3) and (3,7)(3, 7): m=(7−3)/(3−1)=2m = (7 - 3)/(3 - 1) = 2, and then b=3−2×1=1b = 3 - 2 \times 1 = 1, so the line is 2x+12x + 1 again. A positive slope means the line goes up as you move right (increasing); a negative slope means it goes down; slope 0 is a flat, constant function.

A vertical line (like “x=3x = 3”) is not a function of xx: it would give every output for one input. It fails the vertical line test.

In machine learning the same formula appears as y^=wx+b\hat{y} = wx + b, with the weight ww playing the role of the slope and the bias bb the intercept (P0.2, P3.A2).

Parabolas: the shape of squared error

The simplest quadratic function is f(x)=x2f(x) = x^2:

xx−2-2−1-1001122
x2x^24411001144

The graph is a U-shaped curve called a parabola. It is mirror-symmetric (the output for −2-2 equals the output for 22), and its turning point at the origin is called the vertex.

Written in vertex form,

f(x)=a (x−h)2+kf(x) = a\,(x - h)^2 + k

the parameters say where the vertex is and how the curve opens:

  • the vertex is the point (h,k)(h, k),
  • a>0a > 0: opens upward, and the vertex is the lowest point (the minimum),
  • a<0a < 0: opens downward, and the vertex is the highest point (the maximum),
  • a larger ∣a∣|a| makes the parabola narrower and steeper.

Example: f(x)=(x−3)2+1f(x) = (x - 3)^2 + 1 has its vertex at (3,1)(3, 1), opens upward, and its lowest output is 1, reached at x=3x = 3.

Why parabolas matter for ML. Squared error is a parabola. If the true value is 5 and you predict 3, the error is 2 and the squared error is 22=42^2 = 4. Predict 1 and the error is 4, but the squared error is 42=164^2 = 16: doubling the error quadruples the penalty. Over- and under-shooting by the same amount cost the same, because (−2)2=22(-2)^2 = 2^2. That is the mirror symmetry again.

Go deeper Vertex form versus the form ax² + bx + c

Multiply out a(x−h)2+k=ax2−2ah x+(ah2+k)a(x - h)^2 + k = ax^2 - 2ah\,x + (ah^2 + k). So a parabola is often written ax2+bx+cax^2 + bx + c, where b=−2ahb = -2ah and c=ah2+kc = ah^2 + k. The vertex then sits at x=−b/(2a)x = -b/(2a). Check with (x−3)2+1=x2−6x+10(x - 3)^2 + 1 = x^2 - 6x + 10: here a=1a = 1, b=−6b = -6, c=10c = 10, and −b/(2a)=6/2=3-b/(2a) = 6/2 = 3 ✓. The graph crosses the horizontal axis (has real roots) 0, 1 or 2 times. For an upward parabola: no roots if the vertex is above the axis, one root if it is on the axis, two roots if it is below. For a downward parabola it is the other way round.

Exponentials: multiplying instead of adding

An exponential function has the form

f(x)=a⋅bx(b>0)f(x) = a \cdot b^x \qquad (b > 0)

Here the input xx sits in the exponent. Take f(x)=2xf(x) = 2^x:

xx001122331010
2x2^x1122448810241024

The pattern: every +1 in the input multiplies the output by bb (here 2). Compare a line, where every +1 adds the slope.

  • b>1b > 1: exponential growth. The output grows slowly at first, then explodes.
  • 0<b<10 < b < 1: exponential decay. For 0.5x0.5^x the outputs are 1,0.5,0.25,0.125,…1, 0.5, 0.25, 0.125, \dots: they shrink toward 0 but never reach it.
  • aa is the starting value, the output at x=0x = 0 (since b0=1b^0 = 1).
  • The base bb is required to be positive: a negative base like (−2)x(-2)^x is not defined for fractional xx such as x=1/2x = 1/2, so it is not a smooth curve.
  • The output of a⋅bxa \cdot b^x with a>0a > 0 is always positive.

Exponentials appear in compound interest, doubling populations, radioactive decay and, in ML, inside the sigmoid (P1.A5) and softmax (P3.A8).

Absolute value, steps and piecewise functions

Some functions use different rules on different parts of the input. They are called piecewise functions, and ML loves them.

Absolute value ∣x∣|x| is the distance of xx from 0, so it removes the sign:

∣x∣={xif x≥0−xif x<0∣3∣=3, ∣−3∣=3\begin{gathered} |x| = \begin{cases} x & \text{if } x \ge 0 \\ -x & \text{if } x \lt 0 \end{cases} \\ |3| = 3,\ |-3| = 3 \end{gathered}

Its graph is a V with a sharp corner. ∣prediction−truth∣|\text{prediction} - \text{truth}| is the “size of the error, ignoring the sign”.

The ReLU function (rectified linear unit), max⁡(0,x)\max(0, x), outputs 0 for negative inputs and the input itself otherwise: ReLU of −2-2 is 0 and ReLU of 3 is 3. Written in pieces: 00 if x<0x \lt 0, and xx if x≥0x \ge 0. It is one of the most-used activation functions in neural networks (P4.4).

A step function jumps between fixed values at a threshold: output 0 below the threshold and 1 at or above it. Early artificial neurons, such as the perceptron, used a step (P4.1). In the plotter, the “Step” family has a threshold with a filled dot (the value at the jump) and an open dot (the value skipped). A step function is a valid function, even though its graph has a gap: the vertical line test still holds.

Intuition

Each family tells a different story about how the output reacts when you nudge the input:

  • Line: always the same reaction. Each +1 in xx changes the output by the same amount.
  • Parabola: the reaction grows the further you go from the vertex, in both directions.
  • Exponential: the reaction is proportional to what you already have. Growth feeds on itself.
  • Absolute value, ReLU, step: the rule changes at a corner or a jump. Away from that point they are simple (a line, or flat).

The “story” picture is only a guide: real functions can combine these pieces.

Math

FamilyFormulaKey parameters
Linearf(x)=mx+bf(x) = mx + bslope mm, intercept b=f(0)b = f(0)
Quadraticf(x)=a(x−h)2+kf(x) = a(x - h)^2 + kvertex (h,k)(h, k); opens up if a>0a \gt 0, down if a<0a \lt 0
Exponentialf(x)=a bxf(x) = a\,b^x, b>0b \gt 0start value aa; growth if b>1b \gt 1, decay if 0<b<10 \lt b \lt 1
Absolute valuef(x)=a ∣x−h∣+kf(x) = a\,\lvert x - h\rvert + kcorner at (h,k)(h, k)
Stepf(x)=lowf(x) = \text{low} for x<tx \lt t, high\text{high} for x≥tx \ge tthreshold tt

Domain: all real numbers for every row. Range: lines (with m≠0m \ne 0): all numbers; parabola with a>0a \gt 0: [k,∞)[k, \infty); exponential with a>0a \gt 0: (0,∞)(0, \infty) when b≠1b \ne 1; step: just the two values.

Code

def line(x, m, b):
    return m * x + b

def parabola(x, a, h, k):
    return a * (x - h) ** 2 + k

def expo(x, a, b):
    return a * b ** x

line(3, 2, 1)           # 7
parabola(3, 1, 3, 1)    # 1   (the vertex of (x-3)^2 + 1)
expo(3, 1, 2)           # 8

The parameters are just extra arguments. Training a model changes m and b, not x.

Increasing, decreasing, and the lowest point

A function is increasing on an interval if a bigger input gives a bigger output there (the graph climbs as you move right), and decreasing if a bigger input gives a smaller output (the graph falls).

  • 2x+12x + 1 is increasing everywhere; −3x+5-3x + 5 is decreasing everywhere.
  • 2x2^x is increasing everywhere; 0.5x0.5^x is decreasing everywhere.
  • x2x^2 is decreasing for negative xx and increasing for positive xx. It turns around at x=0x = 0.

A point where the output is the smallest is a minimum; the largest is a maximum. A function may have neither: the line 2x+12x + 1 never stops going up. x2x^2 has a minimum of 0 at x=0x = 0 and no maximum. The input where the minimum happens is called the argmin; the output is the minimum value. For (x−3)2+1(x - 3)^2 + 1 the minimum value is 1 and the argmin is x=3x = 3.

This is the bridge to optimization. In machine learning the loss measures how wrong a model is. Take a one-parameter model y^=wx\hat{y} = w x and one data point: input x=2x = 2, true value y=6y = 6. The prediction is 2w2w and the squared error is a function of the parameter ww:

L(w)=(2w−6)2L(w) = (2w - 6)^2
ww001122334455
L(w)L(w)363616164400441616

It is a parabola with its minimum at w=3w = 3 (the model y^=3x\hat{y} = 3x predicts 66 exactly). Training is the search for the input of the loss function where its output is as small as possible. (For this simple bowl there is exactly one lowest point; for real models, training usually finds a good low point rather than a guaranteed lowest one, P1.F2.) Which direction to step when you are not at the bottom is decided by the slope of the graph: that is what derivatives (P1.C1) measure and gradient descent (P1.F3) uses. Optimization is introduced in P1.F1.

Transforming graphs: shift, stretch, flip

Once you know a basic shape, a few small edits to the formula move or reshape it. Start with a base function ff and build

g(x)=a⋅f(k (x−c))+dg(x) = a \cdot f\bigl(k\,(x - c)\bigr) + d

Shift, stretch and flip

g(x) = f(x)

−6−4−2246−6−4−2246xy

original f (dashed grey)   new graph g (solid blue). The two dots are the point (0, f(0)) and where it ends up.

  • Nothing changed yet: g is the same as f. Move a slider.

Check: f(0) = 1, so g(0) = 1 × 1 + 0 = 1.

Pick a base function f (dashed grey) and move the four sliders. The solid blue graph is g(x) = a · f(k(x − c)) + d, and the text lists what each change did. The two dots show the point (0, f(0)) and where it ends up. Try Flip upside down and Mirror left–right.

What each part does:

ChangeLooks likeWhat happens
f(x)+df(x) + doutsidegraph moves up by dd (down if dd is negative)
a⋅f(x)a \cdot f(x)outsideheights are multiplied by aa: stretched if ∣a∣>1\lvert a\rvert \gt 1, squashed if ∣a∣<1\lvert a\rvert \lt 1; a=−1a = -1 flips it upside down
f(x−c)f(x - c)insidegraph moves right by cc (left if cc is negative)
f(k x)f(k\,x)insidegraph is squeezed sideways if ∣k∣>1\lvert k\rvert \gt 1, stretched if ∣k∣<1\lvert k\rvert \lt 1; k=−1k = -1 mirrors it left to right (about the yy-axis; in the widget, about the line x=cx = c once you have shifted it)

The rule of thumb. Changes made outside ff act on the output (the vertical direction) and do what they look like: +d+d goes up. Changes made inside ff act on the input (the horizontal direction) and feel backwards: x−3x - 3 moves the graph to the right, not the left.

Why backwards? Take f(x)=x2f(x) = x^2 and g(x)=(x−2)2g(x) = (x - 2)^2. The lowest point of ff happens where x=0x = 0. For gg the lowest point needs x−2=0x - 2 = 0, so x=2x = 2: whatever ff did at 0, gg does at 2. And check: g(2)=0=f(0)g(2) = 0 = f(0) and g(5)=9=f(3)g(5) = 9 = f(3). The whole picture slid 2 to the right.

This explains vertex form: a(x−h)2+ka(x - h)^2 + k is just x2x^2 stretched by aa, moved right by hh and up by kk.

Composition: one machine feeding the next

Composition means feeding the output of one function into another. Take two machines:

  • g(x)=2xg(x) = 2x (double it)
  • f(x)=x+3f(x) = x + 3 (add three)

Write f(g(x))f(g(x)), read “ff of gg of xx”: do gg first (the inner function), then ff (the outer function). The order of work is from the inside out, which is the reverse of the order you read the letters in.

f(g(2))=f(4)=7g(f(2))=g(5)=10\begin{gathered} f(g(2)) = f(4) = 7 \\ g(f(2)) = g(5) = 10 \end{gathered}

Same two machines, opposite orders, different answers: 7≠107 \ne 10. In general f(g(x))≠g(f(x))f(g(x)) \ne g(f(x)). Here, f(g(x))=2x+3f(g(x)) = 2x + 3 but g(f(x))=2(x+3)=2x+6g(f(x)) = 2(x + 3) = 2x + 6.

Two machines, two orders

f(g(x)): g first, then f

2→ g: 2x→ 4→ f: x + 3→ 7

g(f(x)): f first, then g

2→ f: x + 3→ 5→ g: 2x→ 10

Different answers: 7 versus 10. Order matters.

Pick f and g from the lists and slide the input. The blue row computes f(g(x)): g first. The pink row computes g(f(x)): f first. Try x² with x + 3 (or with x − 1), or choose a pair that breaks, like 1/x with x − 1 at x = 1.

Some pairs happen to agree at one input (or for all inputs: 2x2x and 3x3x in either order give 6x6x), but the order matters in general. An easy example: “put on your socks, then your shoes” is not the same as “shoes, then socks”.

The domain of a composition needs both steps to work: xx must be allowed in the inner function, and its output must be allowed in the outer one. For f(x)=xf(x) = \sqrt{x} and g(x)=x−1g(x) = x - 1, the composition f(g(x))=x−1f(g(x)) = \sqrt{x - 1} needs x≥1x \ge 1.

Neural networks are compositions. A network with two layers computes “layer 1, then layer 2”: output=layer2(layer1(x))\text{output} = \text{layer}_2(\text{layer}_1(\mathbf{x})). Each layer is a simple function; stacking dozens of them builds very complicated ones (P4.3, P4.5).

Inverse functions: undoing a function

An inverse function undoes what a function did. If f(x)=2x+3f(x) = 2x + 3 turns 4 into f(4)=11f(4) = 11, the inverse f−1f^{-1} turns 11 back into 4. To find it, solve for xx:

y=2x+3  ⇒  x=y−32sof−1(y)=y−32,f−1(11)=11−32=4\begin{gathered} y = 2x + 3 \;\Rightarrow\; x = \frac{y - 3}{2} \\ \text{so}\quad f^{-1}(y) = \frac{y - 3}{2}, \\ f^{-1}(11) = \frac{11 - 3}{2} = 4 \end{gathered}

In words: ff doubles and adds 3, so the inverse subtracts 3, then halves (the steps undone in reverse order, like taking off shoes before socks).

  • Not every function has an inverse. f(x)=x2f(x) = x^2 sends both 2 and −2-2 to 4, so given the output 4 you cannot tell which input it came from. An inverse needs each output to come from exactly one input. (Graph test: every horizontal line crosses at most once.) If you only allow x≥0x \ge 0, the inverse exists: it is x\sqrt{x}.
  • Famous pair: the inverse of 2x2^x is log⁡2x\log_2 x, because 23=82^3 = 8 and log⁡28=3\log_2 8 = 3 (P1.A2).
  • Notation warning: f−1f^{-1} is not 1/f1/f. For f(x)=2x+3f(x) = 2x + 3: f−1(11)=4f^{-1}(11) = 4 but 1/f(11)=1/251/f(11) = 1/25. The small “−1-1” means “inverse function”, not “to the power minus one”.

Try it in Python

Python’s def defines a function. NumPy applies it to many inputs at once. Run the cell, then change the numbers.

Try it: functions, tables and composition

Runs real Python in your browser. First run downloads about 9 MB (once; your browser keeps it).

Challenge: define relu(x) as np.maximum(0, x) and print relu(xs - 1). Then define inverse_f(y) for f above and check that inverse_f(f(4)) gives back 4.

Putting it together

A model is a function from inputs to predictions. Its parameters are the knobs on the machine; the loss is a second function that takes those knobs and says how bad the predictions are. Learning is looking for the knob settings where the loss is lowest. Everything in this lesson (inputs and outputs, domains, shapes, shifting and stretching, composition) comes back, again and again, in the rest of the course.

Check your understanding

  1. Which table can NOT describe a function of xx?

  2. What is the domain of f(x)=x−4f(x) = \sqrt{x - 4} (using real numbers)?

  3. Let f(x)=x2f(x) = x^2 and g(x)=x+1g(x) = x + 1. What is f(g(2))f(g(2))?

  4. For f(x)=3⋅2xf(x) = 3 \cdot 2^x, what happens to the output when the input goes up by 1?

  5. How does the graph of f(x−3)f(x - 3) compare with the graph of f(x)f(x)?

Exercises

  1. For f(x)=3x−2f(x) = 3x - 2, find f(0)f(0), f(4)f(4) and f(−1)f(-1).
    Show answerf(0)=−2f(0) = -2, f(4)=10f(4) = 10, f(−1)=−5f(-1) = -5.
  2. A table gives the inputs 0,1,2,30, 1, 2, 3 and the outputs 5,8,11,145, 8, 11, 14. Assuming it comes from a line, find its formula and f(10)f(10).
    Show answerThe output rises by 3 each step, so the slope is 3, and f(0)=5f(0) = 5 is the intercept: f(x)=3x+5f(x) = 3x + 5. So f(10)=35f(10) = 35.
  3. Is the set of pairs (1,2),(2,3),(1,4)(1, 2), (2, 3), (1, 4) a function? And (1,2),(2,2),(3,2)(1, 2), (2, 2), (3, 2)?
    Show answerThe first is not: the input 1 has two outputs (2 and 4). The second is: each input has exactly one output (the same one each time).
  4. Find the domain of x−2\sqrt{x - 2} and of 1x−5\dfrac{1}{x - 5}.
    Show answerx−2\sqrt{x - 2} needs x−2≥0x - 2 \ge 0, so x≥2x \ge 2. 1x−5\dfrac{1}{x - 5} cannot divide by zero, so every number except x=5x = 5.
  5. For the parabola f(x)=(x+1)2−4f(x) = (x + 1)^2 - 4, give the vertex, say whether it opens up or down, and find f(0)f(0).
    Show answerWrite it as (x−(−1))2+(−4)(x - (-1))^2 + (-4), so h=−1h = -1 and k=−4k = -4: the vertex is (−1,−4)(-1, -4). It opens upward (a=1>0a = 1 \gt 0), so −4-4 is the minimum. f(0)=1−4=−3f(0) = 1 - 4 = -3.
  6. With f(x)=x+2f(x) = x + 2 and g(x)=x2g(x) = x^2, compute f(g(3))f(g(3)) and g(f(3))g(f(3)), and write formulas for f(g(x))f(g(x)) and g(f(x))g(f(x)).
    Show answerf(g(3))=f(9)=11f(g(3)) = f(9) = 11 and g(f(3))=g(5)=25g(f(3)) = g(5) = 25. The formulas are f(g(x))=x2+2f(g(x)) = x^2 + 2 and g(f(x))=(x+2)2g(f(x)) = (x + 2)^2.
  7. Find the inverse of f(x)=4x−1f(x) = 4x - 1 and check it at x=2x = 2.
    Show answerSolve y=4x−1y = 4x - 1 for xx: x=(y+1)/4x = (y + 1)/4, so f−1(y)=(y+1)/4f^{-1}(y) = (y + 1)/4. Check: f(2)=7f(2) = 7 and f−1(7)=8/4=2f^{-1}(7) = 8/4 = 2 ✓.
  8. A culture has 100 bacteria and doubles every hour. Write the count after tt hours and find it for t=3t = 3. How does it compare with a plan that adds 100 per hour?
    Show answerN(t)=100⋅2tN(t) = 100 \cdot 2^t, so N(3)=800N(3) = 800. Adding 100 per hour gives 100+100×3=400100 + 100 \times 3 = 400 after 3 hours. The doubling plan pulls far ahead as tt grows.
  9. Describe how to get the graph of g(x)=−(x−3)2+2g(x) = -(x - 3)^2 + 2 from the graph of x2x^2.
    Show answerMove x2x^2 right by 3, flip it upside down (the minus sign outside), and move it up by 2. The vertex is (3,2)(3, 2) and, since it opens downward, 2 is the maximum.
  10. Compute ReLU(−5)(-5), ReLU(5)(5), and the values of max⁡(0,x−2)\max(0, x - 2) at x=1x = 1 and x=3x = 3.
    Show answerReLU(−5)=0(-5) = 0 and ReLU(5)=5(5) = 5. For max⁡(0,x−2)\max(0, x - 2): at x=1x = 1, max⁡(0,−1)=0\max(0, -1) = 0; at x=3x = 3, max⁡(0,1)=1\max(0, 1) = 1.
  11. In the plotter, choose the line family and set it to f(x)=2x+1f(x) = 2x + 1 (slope 2, intercept 1). Move the marker until the output is 3 and read off the input. Check it by solving 2x+1=32x + 1 = 3.
    Show answerThe marker is at x=1x = 1, and 2×1+1=32 \times 1 + 1 = 3 ✓. Algebra: 2x=22x = 2, so x=1x = 1.

Key takeaways

  • A function gives exactly one output for each allowed input. f(x) means 'the output of f when the input is x'. It is not f times x.
  • The domain is the set of allowed inputs; the range is the set of outputs you actually get. Square roots, logarithms and division restrict the domain.
  • A line y = mx + b changes by m for every +1 in x. A parabola a(x − h)² + k has its turning point at (h, k). An exponential a·bˣ multiplies by b for every +1 in x.
  • In f(x − c) + d, the outside changes move the graph the way they look (+d goes up), but the inside change moves it the opposite way (x − c goes right).
  • Composition f(g(x)) means 'do g first, then f'. The order matters: f(g(x)) is usually not g(f(x)). An inverse undoes a function, and it is not 1/f.
  • A model is a function from inputs to predictions; a loss is a function of the parameters; a neural network is many functions composed.
Found a mistake? Tell us