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.
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
The notation is read “ of .” It names the output produced when the function receives the input value .
For :
For :
These are two evaluations of one function. The rule did not change.
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
We can name it as a function:
The semicolon is a helpful convention. It separates the ordinary input from parameters and . It does not create a new kind of arithmetic.
When training is finished, suppose the stored parameters are and . The deployed model is then the function
It can process many inputs:
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 into a normalized value, then turns that value into a score.
Let
be the normalization function, and let
be the scoring function. For , evaluate the pipeline from the inside out:
The combined notation
is called composition. The output of becomes the input of .
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:
| Idea | Mathematics | Python |
|---|---|---|
| Define a rule | def f(x): return 2*x + 1 | |
| Evaluate it | f(3) | |
| Compose rules | g(f(x)) | |
| Name an intermediate result | 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
The expression cannot produce a real-number output at because division by zero is undefined. Saying only “ 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 , substitute wherever appears:
For :
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 cannot map to both and 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, where 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:
Use that output as the input to :
The composed function therefore maps to .
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 values while the stored parameters and normally remain fixed. Training is the process that changed and to fit data.
Both phases evaluate functions, but their variable roles differ:
Question 5
Why must an implementation of h(x) = 1/x handle x = 0 before evaluation?
Show the step-by-step solution
At , the rule requests , 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.