Every time your phone finishes your sentence, or your bank flags a strange charge, an AI model is quietly doing the work. So what is an AI model, really? The honest answer is smaller than the hype suggests. A model is just a pile of numbers that got tuned, through a lot of trial and error, until they turn an input into a useful answer. Those tunable numbers are called weights, the tuning is called training, and using the finished model afterwards is called inference. That really is the whole vocabulary, and you already have a feel for more of it than you think. In this post you build a real, tiny model in about twenty lines of plain Python, watch it tune its own numbers as it learns, then see how the very same idea scales up to the giant models behind tools like ChatGPT.
“The real problem is not whether machines think but whether men do.”
B. F. Skinner, Contingencies of Reinforcement
Last Updated: July 2026 | Tested on: Python 3.14.6 | Difficulty: Beginner | Reading Time: 16 minutes
People throw around words like weights, parameters, training, and inference as if you are supposed to already know them. You are not. By the end of this post you will have trained your own two-number model, seen its file on disk, and understood exactly what “7 billion parameters” means. No machine learning background needed, and every code block here is pure Python that you can run right now with nothing installed. The heavier maths gets its own posts later, and we point you to them at each step.
Table of Contents
What an AI Model Really Is
Think of a coffee machine with two dials, one for how much water and one for how much coffee powder. The machine itself never changes, but by turning those two dials you can move from a watery cup to a strong one. A model is exactly that: a fixed shape with some dials, and the dials are the numbers we call weights. Training is the process of turning the dials until the output tastes right. Using the machine after that, just pressing the button, is inference.
So when someone says “an AI model,” they mean two things bundled together: a function (the fixed shape) and a set of learned numbers (the dial settings). A spam filter, a photo tagger, and a chatbot are all the same idea at heart. They take some input, multiply it by learned weights, add things up, and produce an output. What makes one clever and another simple is mostly how many weights it has and how the maths is wired, not any deep difference in kind.
Build a Tiny Model by Hand
Enough words. Let us build the smallest honest model there is: a straight line with two weights. Say a teacher notices that students who revise more tend to score more, and wants a model that predicts marks from hours revised. Our model is marks = w * hours + b, where w and b are the two dials the model must learn. We start them at silly values and let the code correct them.
📄 line_fit.py: train a two-weight model in pure Python
# line_fit.py: a model is a function with two learnable numbers (w and b)
# Data: hours a student revised (x) and the marks they scored (y).
data = [(1, 35), (2, 45), (3, 55), (4, 65), (5, 75)]
# The two numbers the model will LEARN. We start with silly guesses.
w = 0.0 # weight: how much each extra hour is worth
b = 0.0 # bias: the score with zero revision
lr = 0.01 # learning rate: how big a nudge we take each step
for step in range(1, 2001):
# 1) GUESS: predict marks for every student with the current w and b.
# 2) MEASURE ERROR: mean squared error, how wrong the guesses are.
# 3) NUDGE: work out which way to move w and b to shrink the error.
grad_w = 0.0
grad_b = 0.0
loss = 0.0
for x, y in data:
pred = w * x + b # the entire "model": one line of maths
error = pred - y
loss += error ** 2
grad_w += 2 * error * x
grad_b += 2 * error
n = len(data)
loss /= n
w -= lr * grad_w / n # step w a little in the right direction
b -= lr * grad_b / n # step b too
if step in (1, 5, 20, 100, 500, 2000):
print(f"step {step:>4}: w = {w:6.3f} b = {b:6.3f} loss = {loss:8.3f}")
print(f"\nLearned model: marks = {w:.2f} * hours + {b:.2f}")
▶ Output
step 1: w = 3.700 b = 1.100 loss = 3225.000 step 5: w = 11.548 b = 3.572 loss = 438.495 step 20: w = 15.273 b = 5.686 loss = 68.329 step 100: w = 14.076 b = 10.285 loss = 39.680 step 500: w = 11.052 b = 21.203 loss = 2.642 step 2000: w = 10.007 b = 24.976 loss = 0.000 Learned model: marks = 10.01 * hours + 24.98
What happened here: that output is real, printed straight from Python. Watch the weights move. They start at zero, overshoot around step 20 (w climbs past 15), then settle down as the loss falls from 3225 to basically zero. The model taught itself that marks = 10 * hours + 25, which is exactly the hidden pattern in the data. Nobody told it those numbers. It found them by guessing, measuring how wrong it was, and nudging the two weights over and over. That is the whole of machine learning in one screen, and everything bigger is a scaled-up version of this loop.
The Training Loop in Plain Words
The loop you just ran has a name, the training loop, and it is the single most important idea in this whole series. It is like learning to throw a paper ball into a bin across the room. You throw (guess), see how far you missed (measure the error), adjust your aim a little (nudge), and throw again. After enough throws your aim is good. The model does the same thing with numbers instead of muscles.
The diagram shows the four beats of every training run. You start with rough weights. Then you guess outputs with the current weights, measure the error against the real answers (this error number is called the loss), and ask a simple question: is the loss small enough yet? If not, you nudge each weight a little in the direction that shrinks the loss and loop back to guessing. When the loss is finally small, you stop and freeze the weights.
Those frozen weights are your trained model. The one piece we glossed over is which direction shrinks the loss, and that direction is worked out with something called a gradient. We go deeper into that maths, gently, in the math behind machine learning post.
Training vs Inference: Why Using a Model Is Cheap
Here is a distinction that clears up a lot of confusion about why AI can be expensive. Training is finding the weights. Inference is using them. Training our line took 2000 passes over the data. Inference is one multiply and one add. It is the difference between writing a recipe by trial and error over a hundred attempts (training) and then cooking it once you know it (inference). Let us feel the gap with real numbers.
📄 inference.py: using the trained weights is nearly free
# inference.py: TRAINING found these two numbers. Using them is inference.
# We just paste the learned weights in; no data, no loop, no learning.
w = 10.01 # learned during training
b = 24.98
def predict(hours):
return w * hours + b # one multiply, one add. That is inference.
# A new student, Aviraj, revised for 6 hours. What do we expect?
print(f"Aviraj revised 6 hours -> predicted marks: {predict(6):.1f}")
print(f"Anvi revised 2.5 hours -> predicted marks: {predict(2.5):.1f}")
# Time a single prediction vs a full re-training, to feel the gap.
import time
start = time.perf_counter()
for _ in range(100_000):
predict(6)
infer_ms = (time.perf_counter() - start) / 100_000 * 1000
print(f"\nOne inference takes about {infer_ms:.6f} ms")
print("Training this model took 2000 loops over the data.")
▶ Output
Aviraj revised 6 hours -> predicted marks: 85.0 Anvi revised 2.5 hours -> predicted marks: 50.0 One inference takes about 0.000115 ms Training this model took 2000 loops over the data.
What happened here: once the weights are known, a prediction is a single tiny sum, about a ten-thousandth of a millisecond here. This is why running a model (inference) is cheap enough to do millions of times a day, while training the big models can cost weeks of computer time and a serious electricity bill. For the giant language models the same split holds: training happens once on huge hardware, and after that every reply you get is comparatively cheap inference. This is also why you can download a trained model and use it on a laptop even though you could never have trained it there.
What a Model File Actually Is (and what ‘7B’ means)
When you “download a model,” what lands on your disk? Just the learned numbers. A model file is a container of weights, nothing more mysterious than that. Our line model has two weights, so its file is tiny. Let us save it, check its real size, and then scale the same logic up to the models with the scary names.
📄 model_size.py: weights on disk, from 24 bytes to 130 GB
# model_size.py: a model file is just its learned numbers, saved to disk.
import json, os
# Our line model is two numbers. "Saving the model" means saving them.
model = {"w": 10.01, "b": 24.98}
with open("line_model.json", "w") as f:
json.dump(model, f)
size = os.path.getsize("line_model.json")
print(f"Our 2-parameter model on disk: {size} bytes")
# Now scale the SAME idea up. Big models store one number per parameter.
# A common storage format uses 2 bytes per parameter (half precision).
BYTES_PER_PARAM = 2
def file_size(param_count):
gb = param_count * BYTES_PER_PARAM / (1024 ** 3)
return gb
for label, params in [
("our line model", 2),
("a small model, 125M", 125_000_000),
("a '7B' model", 7_000_000_000),
("a '70B' model", 70_000_000_000),
]:
print(f"{label:<22} {params:>15,} params -> {file_size(params):8.2f} GB on disk")
▶ Output
Our 2-parameter model on disk: 24 bytes our line model 2 params -> 0.00 GB on disk a small model, 125M 125,000,000 params -> 0.23 GB on disk a '7B' model 7,000,000,000 params -> 13.04 GB on disk a '70B' model 70,000,000,000 params -> 130.39 GB on disk
What happened here: now the jargon has a plain meaning. A “7B model” simply has 7 billion weights, and “70B” has 70 billion. Each weight takes a couple of bytes to store, so a 7B model is roughly 13 GB of numbers on your disk and a 70B model is around 130 GB. That is the same JSON-of-weights idea as our 24-byte line, just with billions of dials instead of two.
The parameter count is the headline number people quote because, very loosely, more weights means more capacity to capture patterns (and a bigger download and a heavier machine to run it). The exact byte sizes shift with the storage format used, and the specific model sizes named here are examples at the time of writing, but the relationship stays: parameters times bytes-per-parameter equals file size.
A Quick Model Zoo: Same Skeleton, Different Jobs
You will meet models with many names: regressor, classifier, image model, language model. It sounds like a zoo of unrelated animals, but under the skin they share the same skeleton we already built. They take inputs, multiply by weights, add a bias, and get a number. The only real difference is what they do with that number at the end. A regressor keeps it as a prediction. A classifier turns it into a label. Here are the first two, side by side, from one shared function.
📄 model_zoo.py: one skeleton, a regressor and a classifier
# model_zoo.py: different jobs, same skeleton -> multiply inputs by weights,
# add a bias, then decide what to do with the number that comes out.
def raw_score(inputs, weights, bias):
total = bias
for x, w in zip(inputs, weights):
total += x * w
return total
# A REGRESSOR predicts a number and returns it as-is.
def regressor(inputs, weights, bias):
return raw_score(inputs, weights, bias)
# A CLASSIFIER runs the same maths, then turns the number into a label.
def classifier(inputs, weights, bias):
score = raw_score(inputs, weights, bias)
return "spam" if score > 0 else "not spam"
# Regressor: predict a house rent from [rooms, distance_km].
print("Regressor rent:", regressor([2, 5], weights=[8000, -300], bias=5000))
# Classifier: is this message spam? features [links, all_caps_words].
print("Classifier:", classifier([4, 6], weights=[1.0, 0.5], bias=-5))
print("Classifier:", classifier([0, 1], weights=[1.0, 0.5], bias=-5))
▶ Output
Regressor rent: 19500 Classifier: spam Classifier: not spam
What happened here: both models call the very same raw_score skeleton. The regressor returns 19500 as a rent estimate. The classifier takes its score and, because the number came out above zero, labels the first message “spam” and the second “not spam.” Image models and language models add many more of these weighted sums, stacked in layers, but the atom is identical: inputs times weights, plus a bias. Once you see that all models share this skeleton, the field stops looking like a hundred separate mysteries and starts looking like one idea repeated at different sizes.
We stack these into real layers in the neural networks from scratch post, and the full train-test-predict routine gets its own treatment in what is machine learning.
Common Mistakes
Mistake 1: Thinking the model “stores” the training data
A trained model does not keep a copy of the examples it learned from. Our line model ended up as just two numbers, w and b. The five student records are gone; only the pattern they implied survives in the weights. Big models are the same, just with far more weights. The data shaped the dials, then stepped away. Where those examples come from, and why their quality sets the model’s ceiling, is the subject of the AI training data guide.
Mistake 2: Confusing training cost with inference cost
Beginners hear “training a model costs millions” and assume every use of AI is expensive. Training is the pricey one-time step. Inference, running the finished model, is cheap and fast, which is why apps can call a model on every keystroke. Keep these two words apart and a lot of headlines make more sense.
Mistake 3: Assuming more parameters always means better
A bigger parameter count gives a model more room to capture patterns, but it also means a bigger download, slower inference, and more chance of memorising noise instead of learning the real signal. For our line, two weights were perfect and a thousand would have been silly. The right size depends on the problem, not on chasing the biggest number.
Best Practices
- DO hold the mental model “function plus learned weights” for anything called an AI model, from a spam filter to a chatbot.
- DO keep training and inference separate in your head: one finds the weights, the other uses them.
- DO read a “7B” label as “7 billion weights,” and expect roughly that many times two bytes on disk.
- DON’T picture a model as a database of answers; it is a set of dials, not a lookup table.
- DON’T assume the biggest model is the right one; match the size to the job.
- DON’T treat any specific parameter count or model name as permanent; they change often, so check current sources when it matters.
Conclusion
So, what is an AI model? A function with learnable numbers called weights, nothing more. You trained one by hand and watched its two weights slide from silly guesses to the exact hidden pattern. You saw the training loop (guess, measure the loss, nudge, repeat), felt why inference is cheap while training is costly, opened up what a model file really holds, and learned that “7B” just means seven billion dials. Most importantly, you saw that a regressor, a classifier, and even a giant language model all share one small skeleton.
From here the series goes deeper along the exact threads we pulled on. The direction that shrinks the loss, the gradient, is unpacked gently in the math behind machine learning post. The full workflow of splitting data, training, and testing lives in what is machine learning. And stacking these weighted sums into layers begins in neural networks from scratch. You have the foundation now; the rest is this same idea, grown up.
Keep going: browse the full Python + AI/ML tutorial series home to see where this fits and what comes next.
Frequently Asked Questions
What is an AI model in simple terms?
An AI model is a function with adjustable numbers inside it called weights. Training turns those numbers until the function gives useful answers, and using the finished function is called inference. A spam filter, a photo tagger, and a chatbot are all this same idea at different sizes: inputs multiplied by learned weights, added up, to produce an output.
What are weights and parameters in a model?
Weights (also called parameters) are the numbers a model learns during training. They are like dials on a machine: fixed shape, adjustable settings. A tiny line model has two weights; a large language model has billions. When people say a ‘7B model’, they mean it has 7 billion weights.
What is the difference between training and inference?
Training is the process of finding the weights by guessing, measuring the error, and nudging the weights over and over. Inference is using the finished weights to make a prediction. Training is expensive and happens once; inference is cheap and can run millions of times, which is why using a model is far cheaper than training one.
What does ‘7B parameters’ mean and how big is the file?
‘7B’ means the model has 7 billion weights. Each weight is stored in a couple of bytes, so a 7B model is roughly 13 GB of numbers on disk. The exact size depends on the storage format, but the rule is simple: parameter count times bytes-per-parameter equals the file size.
Does a trained model store the training data?
No. A trained model keeps only its learned weights, not the examples it trained on. Our line model ended as just two numbers; the student records that shaped them are gone. Large models work the same way, storing patterns in their weights rather than a copy of the data.
Do I need heavy maths to understand AI models?
Not to start. The core idea is a function with learnable weights, trained by a guess-measure-nudge loop, and you can build one in about twenty lines of plain Python. The deeper maths of gradients and neural network layers is useful later, but it is not required to understand what a model is and how it learns.
Interview Questions on AI Models
If you can walk through these without peeking, you are ready for this topic in an interview.
Q: In one or two sentences, what is a machine learning model?
A model is a function with learnable numbers, called weights, that are adjusted during training so the function produces useful outputs. Once the weights are set, the model maps inputs to outputs; a line model does it with two weights, a large model with billions, but the shape of the idea is the same.
Q: Walk me through the training loop.
You start with rough weights, then repeat four steps: guess outputs with the current weights, measure the error against the real answers (the loss), work out the direction that shrinks the loss, and nudge each weight a little that way. You loop until the loss is small enough, then freeze the weights. That frozen set of weights is the trained model.
Q: Your CFO asks why the AI budget shows one huge one-time item and a smaller bill that grows every month. Explain training versus inference in those terms.
Training is the search for good weights and involves many passes over data, so it is compute-heavy and usually done once. Inference is applying the finished weights to a new input, which is often just a few multiplications and additions, so it is cheap and fast. This split is why training a large model can cost a fortune while each individual prediction from it is inexpensive.
Q: What does it mean when a model is described as “7B”, and how does that relate to file size?
“7B” means the model has about 7 billion parameters, or weights. Since each weight is stored in a fixed number of bytes (commonly two for half precision), the on-disk size is roughly the parameter count times the bytes per parameter, so a 7B model is around 13 GB. Change the storage precision and the size changes, but the arithmetic is the same.
Q: Is a bigger model always better? Explain.
No. More parameters give more capacity to fit complex patterns, but they also raise the download size, slow inference, and increase the risk of memorising noise rather than learning the true signal. The right size depends on the problem and the data available; a simple task can be solved better by a small model than by a needlessly huge one.
Q: Does a trained model contain the data it was trained on?
Generally no. Training distils patterns from the data into the weights, and the original examples are not stored inside the model. Our line model kept two numbers, not the five student records. Large models hold patterns in their weights rather than a retrievable copy of the dataset, though care is still needed because very large models can sometimes reproduce fragments of what they saw.
Go deeper: when you outgrow this post, the official Python documentation is the next stop.
Related Posts
Previous: What is Artificial Intelligence? AI, ML, and GenAI Explained
Next: AI Training Data: Where Datasets Come From and Why They Matter
Series Home: Python + AI/ML Tutorial Series

No comment