Math for AILoading progress…

Inputs, outputs and domains

Evaluate functions, compare growth rates and read a loss curve without confusing inputs and outputs.

A function is a rule with a promise

A function takes an input, applies a fixed rule, and gives back exactly one output. Feed it the same input twice and you get the same output twice. Think of a machine with a slot on the left and a tray on the right. f(x) = x² is the machine that squares whatever you drop in: input 3, output 9.

The set of inputs the machine accepts is its domain, and the set of outputs it can produce is its range. Two things are easy to mix up. Different inputs may give the same output (both 7 and −7 give 49 when you square them), but one input never gives two outputs. That asymmetry is what makes something a function.

When the machine refuses

Some rules simply break for certain inputs. 1/x has no answer at x = 0, because no number times 0 equals 1. sqrt(x) has no real answer for negative numbers, because a real number multiplied by itself is never negative. log2(x) asks "which power of 2 gives x?" and no power of 2 gives 0 or a negative number. Those inputs are outside the domain. On a graph they show up as a hole or a missing stretch of the curve.

-10-8-6-4-20246810-6-4-20246x = 0: hole
input x = 2
f(2) = 1 / x
output = 0.5
Python: 1 / 2

Allowed. Domain of this machine: every x except 0.

Pick a machine and move the input. Cross the red line or the red zone and the output disappears. The curve is not drawn there, and Python raises an error.

Predict first. Which input is outside the domain of f(x) = 1/(x − 3)?

Predict first. The function g(x) = x² gives g(4) = 16 and g(−4) = 16. Is that allowed?

The common mistake: ignoring the domain in code

A formula on paper just says "undefined". Code reacts, and different tools react differently. This is what Python does:

>>> 1 / 0
ZeroDivisionError: division by zero         # message text can vary between versions

>>> import math
>>> math.sqrt(-4)
ValueError: math domain error

>>> math.log2(0)
ValueError: math domain error
Chapter context

A function maps an input to an output under a stated rule. Graphs let us see the same rule at many inputs; formulas let us calculate a particular output exactly. Both views should describe the same quantity and units.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

f:X→Y,y=f(x),log⁡b(bx)=x.f:X\to Y,\quad y=f(x),\quad \log_b(b^x)=x.

For the illustrative loss:

L(θ)=(θ−1.2)2≥0.L(\theta)=(\theta-1.2)^2\ge0.

Theta is a tunable scalar, 1.2 is the target, and L is a nonnegative output. An exponent and its logarithm undo each other only on their valid domains.

Worked example

Evaluate L(0)=1.44, L(2)=0.64 and L(1.2)=0. The parameter 2 gives a larger input than 0 but a smaller loss. Squaring makes positive and negative distances contribute the same penalty. This is a synthetic landscape, not a claim that real models have one simple valley.

Reference code

The following example uses the Python standard library and does not call a remote service.

from math import isclose, log2
loss = lambda theta: (theta - 1.2) ** 2
assert isclose(loss(0), 1.44)
assert isclose(loss(2), 0.64)
assert loss(1.2) == 0
assert log2(8) == 3
assert sum(i*i for i in range(1, 4)) == 14

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

The output of a loss function is not its input parameter. Larger inputs do not always produce larger outputs. Logarithms require an explicit base when comparing information quantities.