Training vs Inference
AI & ML: lesson 2 of 15
Learn once, slowly. Answer many times, fast.
Lesson 2 of 15 · 4 min
Training vs Inference
Step 1 of 11
examples4
Training fits parameters from labelled examples. Here are four of them.
The Idea
Training is the loop that adjusts parameters until predictions match labels. It is expensive and runs rarely. Inference is a single pass over frozen parameters to answer one input. Same model, two completely different cost profiles.
Real-World Example
A pianist preparing a concert. Months of rehearsal are slow, repeated and full of corrections. The performance itself runs once, in real time, at full speed, with no chance to redo a bar.
The Code
data = [(1.0, "cold"), (2.0, "cold"), (8.0, "hot"), (9.0, "hot")]
# TRAINING: fit one parameter from the labelled examples
cold = [x for x, y in data if y == "cold"]
hot = [x for x, y in data if y == "hot"]
threshold = (sum(cold) / len(cold) + sum(hot) / len(hot)) / 2
# INFERENCE: apply the frozen parameter to a new input
def predict(x):
return "hot" if x > threshold else "cold"
print(threshold, predict(6.5)) # 5.0 hot
Your turn
What does this print?
data = [(2.0, "low"), (4.0, "low"), (10.0, "high"), (12.0, "high")]
low = [x for x, y in data if y == "low"]
high = [x for x, y in data if y == "high"]
threshold = (sum(low) / len(low) + sum(high) / len(high)) / 2
def predict(x):
return "high" if x > threshold else "low"
print(threshold, predict(7.0))Mini quiz
1 / 3