Skip to content
BytePatterns

Training vs Inference

AI & ML: lesson 2 of 32

Learn once, slowly. Answer many times, fast.

Lesson 2 of 32 · 4 min

Training vs Inference

Step 1 of 11

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

Python

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

In a deployed system, which runs far more often?

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.