arrow_back All articles

The Radio Knobs of AI: Weights, Biases, and the Learning Loop

William Golovlev · 3 min read AI/ML
The Radio Knobs of AI: Weights, Biases, and the Learning Loop

In my last post we found the scorecard (loss). But a score doesn’t help if you don’t know how to change it. This post is about the only two things a model can actually move: weights and biases. Then we’ll run the whole learning loop.

What are we actually turning?

Think of a model as an old-school radio. To get a clear signal, you twist the knobs.

Weights decide how much each input matters. Predicting house prices? “Number of rooms” gets a big weight. “Color of the mailbox” gets a tiny one.

Biases are the baseline, the starting value before any input is considered. If you remember y = mx + b from school, m is the weight and b is the bias.

The code

My init_coeffs function sets up the knobs. torch.rand gives random numbers between 0 and 1, and the - 0.5 shifts them to between -0.5 and 0.5, so every knob starts near zero without leaning either way.

def init_coeffs(n_coeffs):
    # Creating our knobs and centering them at 0
    return (torch.rand(n_coeffs) - 0.5).requires_grad_()

requires_grad_() tells the tensors to record every operation that touches these knobs. That recorded history is how the model later figures out which way to turn them.


The compass: gradients

With 1,000 knobs, you’d be guessing forever. You need a compass that says which way to turn each one. That compass is the gradient.

The mountain in the fog

You’re standing on a foggy mountain (high loss) and want to reach the valley (low loss). You can’t see the bottom, but you can feel the slope under your feet.

The gradient is that slope. Gradient descent is taking a step downhill.

Step size (learning rate)

How big is each step? Too small and you’ll never reach the valley. Too big and you jump right over it. In the code, the step size is lr, the learning rate.

Why coeffs.grad.zero_()? PyTorch adds new gradients on top of old ones. If you don’t wipe them after each step, the model follows stale directions mixed with new ones and gets lost.

I go deeper on gradients in the next post.


The full loop

We’ve got the tensors, the scorecard, the knobs, and the compass. Now we put them together.

The 4-step cycle

From simple linear regression to ChatGPT, training follows the same basic loop:

  1. Predict: the model makes a guess.
  2. Loss: the scorecard says how far off it is.
  3. Backward: the gradient finds the downhill direction.
  4. Step: turn the knobs a little in that direction, then repeat.

The code

Here’s the engine in a few lines of PyTorch:

# 1. Start the game
coeffs = init_coeffs(n_coeffs)

# 2. Check the score
loss = calc_loss(coeffs, indeps, deps)

# 3. Find the way downhill
loss.backward()

# 4. Take the step
with torch.no_grad():
    coeffs.sub_(coeffs.grad * lr)
    coeffs.grad.zero_()

That’s one pass. Training is just steps 2 to 4 repeated in a loop.

The result: I ran it 10 times, and the loss dropped from 0.51 to 0.02. The model didn’t “think.” It followed the compass until it stopped being wrong.


That’s the full loop at a glance. Tensors, loss, weights and biases, gradients: the same pieces show up whether the model is a tiny regression or a large language model. Next I zoom in on gradients, then build it all from scratch.

Let me know what you think on LinkedIn.

#Machine Learning#Weights#Biases#AI Tutorial#Neural Networks#Model Training