Math for AI course · Lesson 2 of 180

Functions Are Reusable Machines for AI Calculations

Move from variables to functions: evaluate input-rule-output mappings, compose simple calculations, and see why one model can process many examples.

Share this article

In Lesson 1, variables gave names to inputs, parameters, and outputs. A function packages the relationship among those variables into a reusable rule.

That is already enough structure to describe the central act of inference. A model receives an input, applies a rule determined by its parameters, and returns an output. By the end of this lesson, you will be able to evaluate a function, separate the rule from one particular calculation, and translate the same relationship into Python.

One rule, many evaluations

Consider the function

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

The notation f(x)f(x) is read “ff of xx.” It names the output produced when the function ff receives the input value xx.

For x=3x=3:

f(3)=2(3)+1=6+1=7.\begin{aligned} f(3) &= 2(3)+1 \\ &= 6+1 \\ &= 7. \end{aligned}

For x=2x=-2:

f(2)=2(2)+1=4+1=3.\begin{aligned} f(-2) &= 2(-2)+1 \\ &= -4+1 \\ &= -3. \end{aligned}

These are two evaluations of one function. The rule 2x+12x+1 did not change.

A function is a rule, not one answer. Change the input or select a rule and follow the same three-stage path.
Input2
Rulef(x) = 2x
Output4

For this state, f((2)) = 2x gives 4. Re-entering 2 with the same rule always gives the same output.

The machine picture is useful as long as we remember what it leaves out. A mathematical function does not have to be implemented by physical steps in a box. The key promise is the mapping: allowed input in, one determined output out.

A model is a function with parameters

The tiny score rule from Lesson 1 was

s=wx+b.s=wx+b.

We can name it as a function:

f(x;w,b)=wx+b.f(x;w,b)=wx+b.

The semicolon is a helpful convention. It separates the ordinary input xx from parameters ww and bb. It does not create a new kind of arithmetic.

When training is finished, suppose the stored parameters are w=1.5w=1.5 and b=1b=-1. The deployed model is then the function

f(x)=1.5x1.f(x)=1.5x-1.

It can process many inputs:

f(1)=0.5,f(3)=3.5,f(2)=4.f(1)=0.5, \qquad f(3)=3.5, \qquad f(-2)=-4.

Training chooses the parameter values that define the deployed rule. Inference evaluates that rule for new inputs.

Function notation keeps a pipeline readable

Suppose an application first converts a raw measurement xx into a normalized value, then turns that value into a score.

Let

n(x)=x105n(x)=\frac{x-10}{5}

be the normalization function, and let

g(z)=2z+1g(z)=2z+1

be the scoring function. For x=20x=20, evaluate the pipeline from the inside out:

n(20)=20105=2,g(n(20))=g(2)=2(2)+1=5.\begin{aligned} n(20) &= \frac{20-10}{5}=2, \\ g(n(20)) &= g(2)=2(2)+1=5. \end{aligned}

The combined notation

(gn)(x)=g(n(x))(g\circ n)(x)=g(n(x))

is called composition. The output of nn becomes the input of gg.

This pattern appears throughout AI systems. Preprocessing produces features; model layers transform representations; a final function converts scores into probabilities or decisions. Later lessons will show that the chain rule used in backpropagation follows the same dependency path in reverse.

The same rule in Python

Python’s function syntax makes the input-rule-output structure visible. The Python tutorial describes def as the way to introduce a function definition.

def normalize(x):
    return (x - 10) / 5


def score(z):
    return 2 * z + 1


def model(x):
    normalized = normalize(x)
    return score(normalized)


print(model(20))  # 5.0
print(model(5))   # -1.0

The local name normalized records the intermediate output. The function model composes the two rules without duplicating either rule.

Mathematical notation and Python syntax differ:

IdeaMathematicsPython
Define a rulef(x)=2x+1f(x)=2x+1def f(x): return 2*x + 1
Evaluate itf(3)f(3)f(3)
Compose rulesg(f(x))g(f(x))g(f(x))
Name an intermediate resultz=f(x)z=f(x)z = f(x)

The correspondence is useful, but not perfect. Python functions can read files, mutate external state, use randomness, or return different results at different times. A mathematical function, by definition, assigns one output to each input. When ML writing calls a stochastic procedure a function, the random state must be considered part of the full input or model description.

Boundary cases: not every rule defines a valid output everywhere

Consider

h(x)=1x.h(x)=\frac{1}{x}.

The expression cannot produce a real-number output at x=0x=0 because division by zero is undefined. Saying only “hh takes a real number” is therefore too loose. Its allowed real inputs exclude zero.

This matters in AI code. A normalization function that divides by a feature’s standard deviation needs a plan for zero variance. A logarithm used in a loss needs a positive input. A tensor function expects a compatible shape. Functions come with validity conditions; Lesson 3 gives those conditions precise names.

Check your understanding

Question 1

Evaluate f(x) = 3x - 2 at x = 4 and at x = -1.

Show the step-by-step solution

For x=4x=4, substitute 44 wherever xx appears:

f(4)=3(4)2=122=10.f(4)=3(4)-2=12-2=10.

For x=1x=-1:

f(1)=3(1)2=32=5.f(-1)=3(-1)-2=-3-2=-5.

The function rule stayed fixed. Only the input value changed.

Question 2

A rule sends input 2 to output 5 on one evaluation and output 7 on another, with no other input or state named. Is it a mathematical function of that input alone?

Show the step-by-step solution

No. A function must assign exactly one output to each allowed input. The same input 22 cannot map to both 55 and 77 under one unchanged function.

The procedure might depend on an omitted input such as random state or time. If that state is included, a larger function could be defined—for example, f(2,r)f(2,r) where rr is a random seed. But it is not a function of the stated input alone.

Question 3

Let n(x) = (x - 4) / 2 and g(z) = z². Calculate g(n(10)) step by step.

Show the step-by-step solution

Evaluate the inside function first:

n(10)=1042=62=3.n(10)=\frac{10-4}{2}=\frac{6}{2}=3.

Use that output as the input to gg:

g(n(10))=g(3)=32=9.g(n(10))=g(3)=3^2=9.

The composed function therefore maps 1010 to 99.

Question 4

In f(x; w, b) = wx + b, what changes during ordinary inference and what changed during training?

Show the step-by-step solution

During ordinary inference, the model receives different xx values while the stored parameters ww and bb normally remain fixed. Training is the process that changed ww and bb to fit data.

Both phases evaluate functions, but their variable roles differ:

inference: vary x,training: update w,b.\text{inference: vary }x, \qquad \text{training: update }w,b.

Question 5

Why must an implementation of h(x) = 1/x handle x = 0 before evaluation?

Show the step-by-step solution

At x=0x=0, the rule requests 1/01/0, which has no real-number value. The input is outside the rule’s valid real domain.

An implementation must reject the input, choose a separately defined fallback, or reformulate the operation. Silently adding a small number changes the function being computed and should be documented as a numerical convention, not presented as exact equality.

Sources

  1. Mathematics for Machine Learning companion website
  2. Mathematics for Machine Learning book PDF
  3. Python documentation: defining functions