My account

Neural network basics

A neural network is a stack of layers. Each layer multiplies its input by a weight matrix, adds a bias and passes the result through an activation. The weights are the parameters you load onto the GPU. Training changes the weights and needs much more memory; inference only runs the network forward with fixed weights.

Not reviewedWritten with AI helpReading time: 11 minShop assistant exampleNumPy

Author: bezzad

Contents

The problem: “what is inside the 16 GB file?”

Our shop downloads an open-weight 8B model. The files are about 16 GB. A new teammate asks: “What is in there? Code? Data?”

The answer: almost only numbers, the weights of a neural network. To serve a model well, you need to know what those numbers do when a token goes through them. This lesson explains it with as little math as possible. If vectors and matrix multiply are new to you, read the “Math for ML” lesson first.

One neuron: a weighted sum

A neuron takes some input numbers and gives one output number:

  1. Multiply each input by its own weight.
  2. Add all of them, plus one extra number, the bias.
  3. Pass the sum through an activation function, a simple curve that is not a straight line.
import numpy as np

x = np.array([0.5, -1.0, 2.0])      # inputs
w = np.array([0.8,  0.1, -0.4])     # weights (learned)
b = 0.2                             # bias (learned)
z = x @ w + b                       # weighted sum: -0.3
out = max(0.0, z)                   # ReLU activation: 0.0

That is all a neuron is. The “intelligence” lives in the values of w and b, which were found in training.

A layer: many neurons at once

A layer is many neurons that read the same input. Their weights together make a matrix, so a whole layer is one matrix multiply:

x = np.random.randn(4096)            # one token, 4,096 numbers
W = np.random.randn(4096, 14336)     # weights of the layer
b = np.random.randn(14336)           # one bias per neuron
y = np.maximum(0, x @ W + b)         # 14,336 outputs

Counting parameters is easy: inputs × outputs for the weights, plus outputs for the biases.

  1. 4,096 × 14,336 + 14,336 = about 58.7 million parameters.
  2. Many modern language models drop the bias, so it is often just inputs × outputs.
  3. Each parameter takes 2 bytes in BF16. That is how “8B parameters” becomes “about 16 GB”.

A network: layers in a stack

A network is layers one after another. The output of one layer is the input of the next. Running the input through all layers is called the forward pass.

token vector (4,096 numbers)layer 1x @ W + b, then activationlayer 2x @ W + b, then activation… up to layer 32scores for the next tokenforward passbottom to topevery weightis read once per step
The forward pass. The token goes up through every layer. Each layer is a matrix multiply plus an activation.

Our 8B model has 32 layers (also called blocks). Each block has a few weight matrices. The forward pass for one token uses every weight once. That is why reading all 16 GB from memory is part of every step.

Why the activation matters

Without the activation, a stack of layers would be useless:

  1. Two matrix multiplies in a row are just one matrix multiply. (x @ A) @ B = x @ (A @ B).
  2. So 32 layers without activations equal one layer. The network could only learn straight-line rules.
  3. The activation bends the line. That lets the network learn complex patterns.

Common activations: ReLU (a negative number becomes 0), and smoother ones like GELU and SiLU, which most language models use. For inference, the activation is cheap; the matrix multiplies are the cost.

Training versus inference

Trainingforward passloss (error)backward: gradientsupdate weightsrepeat many timesmemory: weights + gradients + optimizer state + saved activationsInferenceforward passanswerweights never changememory: weights + KV cache
Training runs forward, measures the error, runs backward and changes the weights, again and again. Inference only runs forward, with fixed weights.

Training finds the weights:

  1. Run the forward pass on an example.
  2. Measure the error with a loss function.
  3. Run the backward pass: compute a gradient for every weight, which says how to change it to lower the error.
  4. An optimizer (like Adam) changes the weights a little. Repeat many, many times.

Inference uses the weights:

  1. Run only the forward pass.
  2. Weights never change.
  3. No gradients, no optimizer.

This changes the memory a lot. A common rule of thumb for mixed-precision training with Adam is about 16 bytes per parameter (weights, gradients and optimizer state), plus the activations kept for the backward pass. For our 8B model that is over 128 GB, so training needs several GPUs. Inference needs about 2 bytes per parameter for BF16 weights, plus the KV cache. The same model can need 8 GPUs to train and 1 GPU to serve.

Batches: many inputs, the same weights

A layer does not care if its input is one token or many. Stack the inputs as rows:

X = np.random.randn(32, 4096)        # 32 tokens, e.g. from 32 users
Y = np.maximum(0, X @ W + b)         # shape (32, 14336)
  1. The weights are read once for all 32 rows.
  2. The extra math is cheap for a GPU.
  3. This is batching, and it is the main way inference engines serve many users on one GPU.

Common mistakes

Mistake Result Right way
Thinking the model file holds code or data Confusion about what “loading a model” costs. It is weights: count parameters × bytes.
Planning inference hardware from training hardware Buying far more GPUs than needed. Inference: weights + KV cache + runtime.
Running inference with gradients on Extra memory and slower runs. Turn gradients off (for example torch.inference_mode()).
Thinking more layers are free Every step gets slower. Every layer’s weights are read for every token.

Summary in five lines

  1. A neuron is a weighted sum plus a bias, passed through an activation.
  2. A layer is one matrix multiply; its parameters are inputs × outputs (+ biases).
  3. A network is a stack of layers; the forward pass uses every weight once per token.
  4. Training changes weights and needs far more memory; inference only runs forward.
  5. A batch uses the same weights for many inputs, which is why batching saves GPUs.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.