Please enable JavaScript to use CodeHS

LLMs Under the Hood

Neural networks in five minutes

A neural network is a stack of tiny math machines called neurons. Each neuron takes numbers in, multiplies them by weights, adds a bias, and passes the result through an activation function.

AI ~12 min Intermediate
The Big Idea

The perceptron

The smallest unit of a neural network is a perceptron. It takes one or more numbers, multiplies each by a weight, adds them up, adds a bias, and runs the result through an activation function.

A perceptron all by itself can act like a tiny decision maker. Is this email spam? Will it rain? Is the animal a cat?

Key idea: a neural network is just a big collection of perceptrons arranged in layers. The magic comes from learning the right weights.


Math Minute

Weights and bias

Imagine you are trying to guess whether an animal is a cat. You ask two questions:

  • How fluffy is it? (input x1)
  • How much does it meow? (input x2)

Each answer gets a weight that says how important it is. Then you add a bias, which is like a starting guess before you hear the inputs.

score = w1 * x1 + w2 * x2 + b

If the score is high, the neuron fires. If it is low, it stays quiet. The trick is finding weights and bias that make good predictions.


Math Minute

Activation functions

A neuron does not just output the raw score. It squishes the score through an activation function. One common choice is the sigmoid, which turns any number into a value between 0 and 1.

sigmoid(x) = 1 / (1 + e-x)

That 0-to-1 output is nice because it can represent a probability. If the network outputs 0.92, it is 92% confident the answer is "yes."

Why not just use the score? Without an activation function, a multi-layer network would still be a straight line. Activations let the model learn curves and complex patterns.


Interactive

Build a tiny classifier

Below is a one-neuron "cat detector." It uses two inputs: fluffiness and meowing. Use the sliders to set the weights and bias, then click Train one step to let the network update itself and reduce the error.

Predictions

Neuron settings

Loss: — Correct: —

Try this: Set fluffiness weight = 0.5, meowing weight = 1.2, and bias = -0.8. The cats should score high and the dogs should score low. Then press Train one step a few times and watch the loss drop.


Code

The same classifier in Python

The interactive demo above is a single neuron. Here is the same idea in Python. It takes fluffiness and meowing as inputs, applies the weights and bias, and uses the sigmoid to get a probability.

Try changing w1, w2, and b and rerunning the script. When the weights are right, every cat scores above 0.5 and every dog scores below it. Real neural networks learn these numbers automatically, which is what the next tutorial covers.


Your Turn

Check yourself

Why does a neural network need an activation function? What would happen if every neuron just output its weighted sum directly?

Your answer:

Sample answer: Without activation functions, a stack of neurons is just one big weighted sum. The network could only draw straight-line boundaries. Activation functions let it bend and curve to fit complex patterns.


Check Yourself

Quick quiz

1. What does a single neuron compute?

2. What do the weights in a neural network control?

3. Why are activation functions important?

Keep learning about neural networks

A single neuron is just arithmetic. The real power comes from learning the right weights automatically. Next, see how a network updates its weights using loss functions and gradient descent.

Part of the “LLMs Under the Hood” tutorial series.