Intro to Neural Networks
Neural Networks: Comprehensive Study Notes
Introduction: Artificial Intelligence and Machine Learning
Artificial Intelligence (AI) is a broad concept encompassing systems that perform tasks requiring human-like intelligence. Many classical AI algorithms follow a predefined set of rules—examples include chess engines, pathfinding algorithms used in navigation, and bot behavior in video games. While these rule-based systems are considered AI, they do not learn from data.
A significant subset of AI is machine learning, which enables algorithms to learn patterns and create their own rules from data rather than relying on hard-coded instructions. At the core of many machine learning methods are neural networks—computational models inspired by biological neurons that can learn complex relationships.
Neural Network Architecture
A neural network is composed of neurons organized into layers:
Input layer: The first layer receives the input data. Each neuron in this layer corresponds to one feature or value of the input.
Output layer: The final layer produces the network’s prediction or output.
Hidden layers: Any layers between the input and output layers are called hidden layers. They perform intermediate computations.
Each neuron in a layer is connected to every neuron in the next layer. These connections are called weights. Additionally, each neuron (except input neurons) has a bias—a constant value added to the weighted sum. The structure is often described as “fully connected” because every neuron in one layer influences every neuron in the next.
A neural network can be thought of as a machine that takes an input, processes it through multiple layers, and produces an output.
Neural Networks as Function Approximators
A neural network is conceptually similar to a mathematical function—it receives input(s) and produces output(s). In fact, neural networks are function approximators: they can learn to approximate any function given sufficient capacity and training.
A function like f(x)=2x+3 takes a number, multiplies by 2, and adds 3. A neural network can learn this mapping from examples.
Functions can have multiple inputs (e.g., f(x1,x2)=x1+x2). Similarly, neural networks can have multiple input neurons.
While mathematical functions typically have a single output, neural networks can have multiple output neurons. A network with multiple outputs can be viewed as a collection of functions that share the same inputs.
Because of this flexibility, neural networks can learn any mapping expressible as a function or set of functions. Applications include:
Language models (e.g., ChatGPT): input words (converted to numbers) and output the next word.
Image recognition: input pixel color values, output probabilities that the image contains certain objects (e.g., a cat).
Simple Network: One Input, One Output
To understand the basics, consider a network with one input neuron and one output neuron, without any hidden layers. The two neurons are connected by a weight w. The network computes:
This network can represent linear functions of the form f(x)=w⋅x. Changing the weight rotates the graph around the origin. However, to represent functions like f(x)=x+2, which shifts the line away from the origin, a bias is needed.
Each neuron (except input) has a bias. For this single‑neuron network with bias b:
With w=1 and b=2, the network represents f(x)=x+2. This simple network can represent any linear function—but it is limited to linear functions only.
A single neuron with a bias can represent any affine linear combination of its inputs.
Multiple Inputs or Outputs
Two inputs: Each input gets its own weight. Output is w1⋅x1+w2⋅x2+b. This still represents a linear function (a plane in 3D).
Two outputs: Each output is computed independently using its own set of weights and biases. Both are linear functions sharing the same inputs.
Adding more layers (hidden layers) or more neurons per layer without activation functions still yields only linear functions, because the composition of linear functions is itself linear.
Activation Functions: Enabling Non‑Linearity
To approximate non‑linear functions, an activation function is applied to each neuron (except input) after computing the weighted sum and adding the bias. The activation function introduces non‑linearity.
The Rectified Linear Unit (ReLU)
One of the simplest and most popular activation functions is the ReLU (Rectified Linear Unit). It is defined as:
The graph of ReLU is a piecewise linear function: it outputs zero for all negative inputs and passes positive inputs unchanged.
With ReLU, a neuron’s output becomes:
How ReLU Enables Curve Approximation
Even a small network with a hidden layer containing ReLU neurons can approximate non‑linear shapes. The process involves combining multiple ReLU neurons (hidden units) to create piecewise linear segments that together match the target curve.
Each ReLU neuron produces a “bent” linear piece: a slope for positive inputs and zero for negative inputs.
By adjusting the weights (controlling slopes) and biases (controlling the activation point, i.e., where the bend occurs), the network can shape each piece.
Adding more neurons allows more pieces, enabling approximation of arbitrarily complex curves.
The final output is a weighted sum of all hidden neurons’ outputs (plus possibly a bias), combining their contributions.
The term “activation function” comes from this behavior: the function “activates” the neuron only in specific regions (when the weighted sum exceeds the bias threshold).
Other Activation Functions
Step function: Returns 1 if input is positive, 0 otherwise. A hard threshold; not differentiable, so rarely used in modern networks.
Sigmoid function: Squashes input values into the range (0,1). Defined as σ(z)=1+e−z1. Its derivative is σ′(z)=σ(z)(1−σ(z)).
Various others (tanh, Leaky ReLU, etc.) exist, each with slightly different properties.
The essential property of all activation functions is that they are non‑linear, allowing the network to learn non‑linear relationships.
Supervised Learning: Training a Neural Network
In supervised learning, the network is provided with a set of labeled data—input‑output pairs. The goal is to learn the mapping from inputs to outputs by adjusting the network’s weights and biases.
Measuring Performance: Cost (Loss)
Initialize all weights and biases randomly.
For a given input, the network produces an output.
Compare the network’s output to the true target value. The difference is called the error. A common error measure is squared error: error=(apred−ytrue)2 (squaring removes sign and penalizes larger errors more).
Cost: average of squared errors over all data points in the training set: $$ C = \frac{1}{n} \sum_{i=1}^{n} (a_{\text{pred}}^{(i)} - y^{(i)})^2 $$
The training objective is to minimize the cost by adjusting the network’s parameters (weights and biases).
Gradient Descent (Manual Approach)
To minimize the cost, we need to know how each weight and bias affects the cost. A simple method is finite differences:
For a particular weight w, increase it by a tiny amount ϵ (e.g., 0.01).
Recalculate the cost with this new weight.
Compute the approximate derivative: ΔwΔC≈ϵC(w+ϵ)−C(w).
If the derivative is positive, increasing the weight raises the cost, so the weight should be decreased. If negative, increasing the weight lowers the cost, so increase it.
Update the weight proportionally to the derivative, scaled by a learning rate α: $$ w_{\text{new}} = w_{\text{old}} - \alpha \cdot \frac{\Delta C}{\Delta w} $$
Repeat for every weight and bias in the network.
Problem: This method is extremely slow for large networks because it requires a separate forward pass for each parameter to measure the cost change.
Backpropagation: Efficient Gradient Computation
Backpropagation is an algorithm that computes the gradient of the cost with respect to all parameters in a single forward‑backward pass. It relies on the chain rule of calculus.
Derivatives and the Chain Rule
For a simple network with one hidden neuron (using sigmoid activation), the forward equations are:
The derivative of the cost with respect to w1 can be found by following the path of dependencies:
Each partial derivative is simpler to compute:
∂w1∂z1=x (the input to that connection)
∂z1∂a1=σ′(z1)=a1(1−a1) (for sigmoid)
∂a1∂z2=w2
∂z2∂a2=σ′(z2)=a2(1−a2)
∂a2∂C=2(a2−y)
Thus, the gradient for w1 can be computed using numerically calculated values from the forward pass (all z and a values).
The Backward Pass
Backpropagation works backward through the network. For the example above:
Compute ∂a2∂C and ∂z2∂C.
Use these to compute gradients for w2 and b2:
∂w2∂C=∂w2∂z2⋅∂z2∂C=a1⋅∂z2∂C
∂b2∂C=1⋅∂z2∂C
Then propagate back to the previous layer: ∂a1∂C=w2⋅∂z2∂C, and continue to w1 and b1.
This efficient reuse of computed gradients (hence “back‑propagation”) makes it feasible to train networks with millions of parameters.
General Case: Multiple Layers and Neurons
For a network with multiple neurons per layer, the gradient for a weight wijk (connecting neuron j in layer l to neuron k in layer l+1) is computed by summing over all paths through which the output of neuron j influences the cost.
Let:
zl,j = weighted sum before activation for neuron j in layer l
al,j = activation(zl,j)
C = total cost (sum over outputs)
The partial derivative ∂wl,j,k∂C (weight from neuron j in layer l to neuron k in layer l+1) is:
where ∂zl+1,k∂C is computed recursively from the next layer. For biases:
The backpropagation algorithm provides a systematic way to compute all ∂z∂C values layer by layer from the output toward the input.
Training Procedure Summary
Initialize all weights and biases (usually small random values).
For each epoch (or mini‑batch):
Forward pass: For each input in the batch, compute all z and a values through the entire network.
Compute cost: Average the loss over the batch.
Backward pass (backpropagation): Using the stored z and a values, compute the gradient of the cost with respect to every parameter.
Update parameters: For each weight w and bias b:
w←w−α⋅m1∑batch∂w∂Cb←b−α⋅m1∑batch∂b∂C
Repeat until the cost stops decreasing significantly (convergence).
Learning rate α controls the step size: too large may overshoot the minimum, too small leads to slow convergence.
Demonstrations of Neural Network Learning
Approximating a non‑linear function: Using a small network with ReLU activation, the network adapts its piecewise linear segments to match a smooth curve. With enough hidden neurons, it can fit the function accurately.
Classification of colored dots: A network with two inputs (x and y coordinates) and four outputs (probabilities for each color) learns to separate different regions of the plane. The decision boundaries become non‑linear as the network adds hidden layers.
Complex pattern learning: Even intricate patterns—non‑linear and high‑dimensional—can be learned by increasing network capacity (more layers, more neurons). The training process (forward pass, cost computation, backpropagation, parameter update) automatically discovers the necessary weights and biases.
Neural networks are powerful function approximators that learn by adjusting their weights and biases using the backpropagation algorithm. The introduction of activation functions enables them to model non‑linear relationships. With enough neurons and layers, a neural network can theoretically approximate any continuous function on a compact domain—a property known as the universal approximation theorem.

Ask anything
We've read this whole study set, so questions about it get the best answers.