Shiffman, Chapter 10

Neural Networks

The perceptron, weighted inputs, activation functions, supervised learning, and training — the simplest possible neural network.

Prerequisites: Chapters 1-2 (vectors and forces). Helpful but not required: Chapter 6 (steering).
10
Chapters
2
Simulations
10
Quizzes

Chapter 0: Why Neural Networks?

We began with inanimate objects. We gave them desires and autonomy. We let them evolve. Now we ask: can they learn?

Some problems are trivial for computers (square root of 964,324) but impossible to explain as rules. Show a toddler a kitten and a puppy — they'll tell you which is which instantly. Teaching a computer to do the same? That took decades of research.

Neural networks are inspired by the brain — an interconnected web of neurons transmitting electrical signals. We won't model the actual biology. Instead, we'll take the idea of connected, weighted, adaptive nodes and build a computational system that can learn from experience.

A neural network is a complex adaptive system: it can change its internal structure based on information flowing through it. The mechanism? Adjusting weights on the connections between neurons.

What makes a neural network "adaptive"?

Chapter 1: The Perceptron

Invented in 1957 by Frank Rosenblatt, the perceptron is the simplest neural network: a single neuron. It follows a "feed-forward" model: inputs come in, get processed, and an output comes out.

The perceptron algorithm has four steps:

1. Receive inputs
x1, x2, ... , xn
2. Weight inputs
Multiply each input by its weight: xi × wi
3. Sum
Add up all weighted inputs: Σ xi × wi
4. Activate
Pass sum through activation function: sign(sum) → +1 or -1
What are the four steps of the perceptron algorithm?

Chapter 2: Weighted Inputs

Each input is multiplied by a weight — a number (usually between -1 and 1) that controls how much influence that input has. Weights start random and are adjusted during training.

pseudocode
inputs  = [12, 4]
weights = [0.5, -1]

sum = 12 * 0.5 + 4 * (-1) = 6 + (-4) = 2
The bias input. What if both inputs are 0? The sum will always be 0, regardless of weights. To fix this, we add a third input that is always 1 (the bias). Its weight shifts the decision boundary, solving the (0,0) problem.
Why does a perceptron need a bias input?

Chapter 3: Activation & Output

The sum of weighted inputs is passed through an activation function that decides whether the neuron "fires." For a simple perceptron, the activation is just the sign of the sum:

pseudocode
function activate(sum):
  if sum > 0: return +1
  else:      return -1

This makes the perceptron a binary classifier: it divides the input space into two regions with a straight line. Points on one side get +1, points on the other get -1.

The perceptron as a line drawer. A perceptron with two inputs (x, y) and a bias can learn to draw a line in 2D space. It classifies every point as above (+1) or below (-1) the line. The weights define the line's slope and position.
What does the sign activation function do?

Chapter 4: Training

A perceptron with random weights is no better than a coin flip. It must learn. The method: supervised learning.

1. Provide known data
Input + correct answer
2. Make a guess
feedforward(inputs)
3. Compute error
error = desired - guess
4. Adjust weights
weight += error × input × learning_constant
This is exactly like steering! In Chapter 6: steering = desired - velocity. Here: error = desired - guess. Steering adjusts velocity; training adjusts weights. Both correct their direction based on the gap between where they want to be and where they are.
pseudocode
function train(inputs, desired):
  guess = feedforward(inputs)
  error = desired - guess
  for i in weights.length:
    weights[i] += learningRate * error * inputs[i]
How does a perceptron adjust its weights during training?

Chapter 5: The Learning Constant

The learning constant (often called learning rate) controls how fast the weights change. It works exactly like maxforce in steering.

Learning RateEffect
High (0.1+)Fast learning, but may overshoot the optimal weights and oscillate
Medium (0.01)Good balance of speed and accuracy
Low (0.001)Slow but precise — tiny, careful adjustments
Too high: The network swings wildly, never settling. Like a car that oversteers with every turn. Too low: Training takes forever. Like a tanker ship that needs miles to change direction. The sweet spot is problem-dependent.
What is the analog of the learning constant in steering behaviors?

Chapter 6: Beyond One Neuron

A single perceptron can only solve linearly separable problems — problems where a straight line divides the data. AND and OR are linearly separable. XOR is not.

ABANDORXOR
00000
01011
10011
11110
Multi-layered networks solve XOR. By combining perceptrons (one for OR, one for NOT AND), a network of two neurons can solve XOR. This is a multi-layered perceptron with a "hidden layer." Training it requires backpropagation — feeding the error backwards through the network to adjust all weights.
What fundamental limitation does a single perceptron have?

Chapter 7: Steering Brains

What if we gave our Vehicle a perceptron as a brain? Instead of hardcoding how to weight steering forces, let the brain learn.

pseudocode
class Vehicle:
  brain = new Perceptron(numTargets)

  function steer(targets):
    forces = []
    for target in targets:
      forces.add(seek(target))

    result = brain.feedforward(forces)  # Brain weights the forces
    applyForce(result)

    # Reinforcement learning: how far from center?
    error = center - location
    brain.train(forces, error)
Reinforcement learning: The vehicle observes the result of its decision. Did it end up near the center (good) or far away (bad)? The error vector tells the brain how to adjust. Over time, the vehicle learns to weight the steering forces optimally.

This is a creative extension — not a rigorous neural network, but a demonstration of how the perceptron concept can be embedded in our autonomous agents to create learning creatures.

In the steering perceptron, what serves as the "error" for training?

Chapter 8: Perceptron Simulation

Below is a perceptron learning to classify points as above or below a line. Watch it improve as it trains on more data points.

Perceptron Line Classifier

The diagonal line is the true boundary. The perceptron trains point by point. Correct guesses are teal; wrong guesses are orange. Watch accuracy climb over time.

Trained: 0   Accuracy: 50%
Watch the boundary shift. At first, the perceptron is random — roughly 50% accuracy. As it sees more training examples, the weights adjust and accuracy climbs. Within a few hundred points, it consistently classifies correctly. The perceptron has learned where the line is.
What is the perceptron actually learning in the line classification example?

Chapter 9: Summary

TopicKey Takeaway
Neural networksComputational systems inspired by the brain; they learn by adjusting weights
PerceptronSimplest NN: weighted sum of inputs → activation → output
BiasAn always-on input that shifts the decision boundary
Trainingerror = desired - guess; weight += error × input × learning_rate
Learning rateControls adjustment speed; like maxforce in steering
Linear separabilityOne perceptron = one straight line; XOR needs multiple neurons
BackpropagationFeeds error backward through multi-layer networks to adjust all weights
Steering brainPerceptron as a vehicle's brain; reinforcement learning from observation
The lesson: A neural network is not magic. It's weighted sums and error correction. But this simple mechanism — adjust weights based on mistakes — is powerful enough to learn patterns, make decisions, and adapt to changing environments. From a single perceptron to deep learning, the core idea is the same: learn from your errors.

What we covered:

• The perceptron (1957)
• Weights, bias, activation
• Supervised learning
• Error = desired - guess
• Learning constant
• Linear separability and XOR
• Perceptron steering brains

The journey:

From vectors to forces to particles to physics libraries to autonomous agents to cellular automata to fractals to evolution to neural networks. We started with a dot moving across a screen and ended with objects that learn. That's the nature of code.

What is the core mechanism by which a neural network learns?