Neural Network Basics

What You’ll Learn
  • Turn one vector into another using weights, bias, and an activation function
  • Follow one complete training step, from a wrong prediction to a better one
  • Distinguish computing gradients from updating weights
  • Read the tensor shapes and automatic gradients used in a small PyTorch example

You now know how text becomes vectors. Before we give a model memory, we need one more idea: a plain neural layer.

A basic neural network is a stack of these layers. Each layer does the same pattern:

  1. multiply input numbers by learned weights
  2. add a learned bias
  3. pass the result through an activation function
  4. output a new vector

One Layer, Step by Step

Neural Layer: Vector In, Vector Out

Step 1: Inputs
Input vector
x1 = 2x2 = -1

Two numbers enter the layer.

->
Weights
neuron 1 [0.5, 1]
neuron 2 [-1, 0.5]

Each neuron has its own weights.

->
Output
not computed yet
A neural layer starts with a vector: a short list of numbers.

What to look for: the input is a vector, the output is another vector. The weights and bias are learned during training. The activation function lets the layer model curved patterns, not just straight-line relationships.

How Does a Weight Learn?

We called those weights “learned,” but a randomly chosen weight has no reason to produce the right answer. We need a way to measure a mistake and use it to improve the next prediction.

Start with one input number, 2, and a target answer, also 2. Our tiny model multiplies the input by one adjustable weight, initially 0.5. Its first prediction is 1, too low. We leave out bias and activation for this example so there is only one number to adjust.

How does one weight learn?

Input → transform → output

Input2Given by the example
Multiply by weight0.5The number we can adjust
Prediction1.0Target: 2.0

2 × 0.5 = 1.0. The model's first guess is too low.

The input is fixed at 2. Our adjustable weight starts at 0.5, so the prediction is 1. The target is 2.

What to look for: follow all five steps with the default learning rate of 0.1. The prediction moves from 1 to 1.8, closer to the target 2, and the error score falls from 1 to 0.04. Then return to Update weight and try 0.3. A larger adjustment overshoots and makes this example worse.

Think of tuning a shower knob: check the temperature, decide which way to turn, make a small adjustment, and check again. The limit of the analogy is that a model calculates a sensitivity for each weight instead of feeling the water.

Here’s what happened in our example:

  1. Predict. Multiply the input 2 by the current weight 0.5 to get 1. This is the forward pass, the same input → transform → output pattern you just saw.
  2. Measure the mistake. The prediction is 1 below the target. Square that difference to get an error score of 1. We call this score the loss. A perfect prediction would have a loss of 0.
  3. Trace the mistake backward. Compute how a tiny change to the weight would affect the loss. This sensitivity is the gradient. Here it is −4, which tells us that increasing the weight a little would reduce the loss. Backpropagation computes these sensitivities by following the forward calculation backward.
  4. Update the weight. The learning rate, 0.1, controls how far to move. Subtracting the learning rate times the gradient increases our weight from 0.5 to 0.9. The rule that turns gradients into parameter updates is an optimizer; this simple rule is gradient descent.
  5. Predict again. The same input 2 now gives 1.8. Its distance from the target is only 0.2, so the new squared-error loss is 0.04. We checked that this update helped instead of stopping at the new weight.

A common mistake is to think that backpropagation changes the weights. It only computes gradients. The optimizer performs the update. Another is to assume a larger step always learns faster: our 0.3 experiment shows why we must check the resulting loss.

Quick Check
We computed a weight's gradient as −4. What happens before the optimizer runs?
Your turn
Choose an answer to check your understanding.
Fill in the Blanks
Starting at weight 0.5 with gradient −4 and learning rate 0.1, the updated weight is . With input 2, the next prediction is .
Fill in every blank, then check your answer.

A Small Bridge to PyTorch

Hand calculations show what learning does. For a model with thousands of weights, we want a library to organize the numbers and keep track of those sensitivities. PyTorch provides both.

Three ideas are enough to read the example below:

IdeaMeaningSmall example
TensorA container of numbers, from one number to a multidimensional arrayA scalar weight, a vector of features, or a batch of vectors
ShapeThe size of each axis in that container(3, 2) means 3 examples with 2 features each
AutogradAutomatic differentiation: record operations, then compute their gradients backwardloss.backward() fills the weight’s .grad

A batch is a group of examples processed together. Shape labels tell us what each axis means, so we don’t accidentally confuse examples with features. Autograd handles the backward calculation; we still choose the loss, learning rate, and update rule.

You can return to these snippets when you reach Train Your Own GPT. That project repeats this learning loop with batches of token sequences and many parameters. A language model uses a loss suited to next-token probabilities rather than this example’s squared error.

Why RNNs Need This

An RNN is not a totally new kind of math. It is this same vector-in, vector-out layer used repeatedly:

Model pieceWhat goes inWhat comes out
Neural layerone vectora transformed vector
RNN stepcurrent token vector + previous memorynew memory vector

The new part in an RNN is the loop: the output memory from one step becomes part of the input at the next step.

Quick Check
What does a neural layer do at the highest level?
Your turn
Choose an answer to check your understanding.
Fill in the Blanks
A neural layer computes a weighted sum, adds a , then applies an function.
Fill in every blank, then check your answer.

Quick Summary

StepWhat happensIntuition
WeightsScale and mix input numbersWhich inputs matter for each neuron
BiasShifts the scoreA learned default push
ActivationBends the resultLets the network model nonlinear patterns
Layer outputProduces a new vectorA transformed representation
LossMeasures how far a prediction is from its targetAn error score to reduce
Backward passComputes the loss gradient for each trainable parameterWhich way should each knob turn?
Optimizer stepUses gradients to update the parametersMake an adjustment, then predict again
Tensor shapeRecords the size and meaning of each axisKeep examples and features organized
Learning Checklist
  • I can explain the roles of weights, bias, and activation in a neural layer
  • I can follow input → transform → output for one forward pass
  • I can distinguish a prediction, its target, and its loss
  • I can explain why computing a gradient and updating a weight are separate steps
  • I can check the new prediction and loss after a weight update
  • I can read a tensor’s shape and explain what loss.backward() computes

Next Steps

Now that a vector can be transformed, we can add memory. An RNN runs a similar transformation again and again as it reads a sequence. RNN Introduction →