Lecture 08: Neural Networks - Representation
An introduction to neural network representation, covering non-linear hypotheses, biological and artificial neuron models, forward propagation, complex non-linear function evaluation, and multiclass classification.
Non-linear Hypotheses
Why Do We Need Neural Networks?
Consider a complex supervised classification problem. We could attempt to use logistic regression with many polynomial terms:
- Works well when you have only or features.
- However, if you have features (e.g., predicting the probability of a house selling in the next 6 months based on 100 features of the house), including all quadratic (second-order) terms: results in approximately features for .

The number of quadratic features grows on the order of :
- For , feature count .
- Working with this many features is computationally expensive.
- Simply selecting a small subset of features often leaves the model unable to fit a complex dataset adequately.
If we include cubic (third-order) terms (e.g., ):
- The number of features grows on the order of .
- For , there are approximately features.
- Consequently, polynomial logistic regression becomes impractical when is large.
Example: Problems Where is Large (Computer Vision)
Computer vision algorithms process images as matrices of pixel intensity values.
To build a car detector:
- Collect a training set of images containing cars () and not cars ().
- Test an unseen image to classify whether it contains a car.

To understand why this is challenging:
- Suppose we select two specific pixel locations and plot their intensity values. We need a non-linear decision boundary to separate the two classes in feature space.
- For a small pixel grayscale image, there are pixels.
- Including all quadratic features yields approximately features.
- If using RGB images, , yielding tens of millions of features.
- For a RGB image, , resulting in hundreds of millions of features.
Thus, standard logistic regression with polynomial features is unsuitable for high-dimensional complex data. Neural networks provide a much more effective framework for learning non-linear hypotheses in huge feature spaces.
Neurons and the Brain
Neural networks (NNs) were originally motivated by the desire to build algorithms that replicate the computational functionality of the biological brain.
- Origins: Developed extensively in the 1980s and 1990s. Popularity waned in the late 1990s due to computational constraints, but saw a massive resurgence as modern computing hardware made training large-scale networks computationally feasible.
- The "One Learning Algorithm" Hypothesis: Evidence suggests the brain may use a single master learning algorithm for diverse sensory inputs.
- Neuroplasticity experiments: Re-routing the optic nerve to the auditory cortex (sound processing region) enables the auditory cortex to learn to process visual signals ("learns to see"). Re-routing to the somatosensory cortex yields similar results.
- Human-Machine Interface Examples:
- BrainPort: A camera feeds pixel data to a grid of electrodes on the tongue, allowing visually impaired individuals to perceive visual patterns through tactile tongue stimulation.
- Human Echolocation: Training individuals to navigate environments by interpreting acoustic reflections.
- Haptic Direction Belts: A belt that pulses in the direction of magnetic north, giving users a continuous sensory orientation.
Model Representation I
How Do We Represent Neural Networks?
Neural networks were designed to simulate networks of biological neurons.
A biological neuron consists of three main structural parts:
- Cell body: Performs cellular computations.
- Dendrites: Input wires that receive electrical signals from other neurons.
- Axon: Output wire that transmits electrical pulses (spikes) to other neurons.

At a high level:
- A neuron receives inputs via dendrites.
- Performs processing in the cell body.
- Sends output pulses down the axon to downstream neurons.
Artificial Neural Network - Representation of a Neuron
In an artificial neural network, a neuron is modeled as a logistic unit:
- Inputs are fed in via input wires.
- The unit evaluates a logistic computation.
- Sends output down output wires.

An additional bias unit is often included. The parameters are referred to as the weights of the model:
Where the activation function is the sigmoid (logistic) function:
Neural Network Architecture
When multiple artificial neurons are connected together, they form a neural network:

The network is organized into layers:
- Layer 1 (Input Layer): Contains input features (plus bias unit ).
- Layer 2 (Hidden Layer): Middle layer containing hidden activation units (plus bias unit ). "Hidden" indicates that these values are intermediate computation steps not directly observed in the training set.
- Layer 3 (Output Layer): Produces the final computed value .
Neural Network Notation
- : Activation of unit in layer (the value computed and output by node in layer ).
- : Matrix of parameters (weights) controlling the function mapping from layer to layer .
Matrix Dimensions Rule:
If a network has units in layer and units in layer , then the weight matrix has dimensions:
- Number of rows (): Equal to the number of units in layer .
- Number of columns (): Equal to the number of units in layer plus (for the bias unit).
Example: If Layer 1 has 10 input units and Layer 2 has 20 hidden units, will have dimensions .
Layer Computations

For the 3-layer architecture shown above, the activations of the hidden units in Layer 2 are calculated as:
The final hypothesis output in Layer 3 is computed using the activation values from Layer 2:
Where is the bias unit added to Layer 2.
Parameter Indexing Notation:

In general, parameter element represents:
- : The layer mapping FROM (layer ).
- : The destination unit index in layer .
- : The source unit index in layer .
Example: controls the mapping from unit in Layer 1 to unit in Layer 2.
Model Representation II
Vectorized Implementation of Forward Propagation
To perform computations efficiently, we vectorize the step-by-step activations.
Define intermediate linear values as the weighted inputs to activation function :
Thus, each activation is .
Define vectors:
Vectorizing the calculation of Layer 2:

Where:
- is a matrix.
- is a vector.
- and are vectors.
- applies the sigmoid function element-wise to vector .
To keep notation consistent across layers, define the input layer vector as :
To calculate the output layer, add a bias unit to , expanding into a vector.

Then compute and the final hypothesis:
![]()
This process of starting with the input layer activations and sequentially calculating activations layer-by-layer forward through the network is called Forward Propagation.
Neural Networks Learning Its Own Features
Consider the final output layer (Layer 3) of our network:

The hypothesis function evaluates as:
Notice that this is identical to standard logistic regression! The critical difference is:
- Standard logistic regression performs classification directly on raw input features .
- A neural network performs classification using learned features calculated by the hidden layer mapping .
Instead of manually engineering high-order polynomial features, the neural network automatically learns optimal non-linear feature representations in its hidden layers to feed into the final logistic regression output node.
Network Architecture Variations
Neural networks can be configured with various topologies depending on problem complexity:
- Different numbers of hidden units per layer.
- Multiple hidden layers (e.g., Layer 2 and Layer 3 as hidden layers, with Layer 4 as output).

Neural Network Example: Computing Complex Non-linear Functions
To gain intuition for how neural networks compute complex non-linear decision boundaries, we can construct networks for basic boolean logical functions.
Non-linear Classification: XOR / XNOR
Consider binary inputs . We want to learn a non-linear decision boundary for the XNOR function:

- Positive instances : and .
- Negative instances : and .
Example 1: Logical AND Function
Can a single artificial neuron compute the logical AND function?

Set up a neuron with inputs and bias . Assign weights:

The hypothesis equation is:
Evaluating all four binary input combinations:
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
Thus, .
Example 2: Logical OR Function
Set up a neuron with weights:

The hypothesis equation is:
Evaluating input combinations:
| 0 | 0 | ||
| 0 | 1 | ||
| 1 | 0 | ||
| 1 | 1 |
Thus, .
Example 3: Logical NOT and NOR Functions
To perform negation (NOT ), place a large negative weight in front of input variable :

With weights :
To compute (NOR gate), use weights:
- Any input of 1 yields or .
Combining Gates: XNOR Circuit
We can combine logical AND, NOR, and OR gates into a 2-layer neural network to compute :

Layer 2 (Hidden Layer):
- Node computes :
- Node computes :
Layer 3 (Output Layer):
- Node combines hidden activations using an OR gate:
Truth Table Evaluation:
| () | () | |||
|---|---|---|---|---|
| 0 | 0 | 0 | 1 | |
| 0 | 1 | 0 | 0 | |
| 1 | 0 | 0 | 0 | |
| 1 | 1 | 1 | 0 |
By layering simple linear units, neural networks can build up complex non-linear functions step-by-step.
Practical Application: Handwritten Digit Recognition
Machine learning pioneer Yann LeCun developed early multi-layer neural networks (LeNet) for automated zip code and handwritten digit recognition on postal envelopes. By training hierarchical layers to detect edges, strokes, and digits, these networks achieved commercial success long before the deep learning era.
Multiclass Classification
Multiclass classification involves distinguishing between more than two categories (), such as recognizing digits () or identifying four object classes: Pedestrian, Car, Motorcycle, and Truck.
One-vs-All Extension for Neural Networks
To classify classes, construct a neural network with output nodes in the final layer:

For a 4-class problem (), the output layer yields a 4-dimensional vector:
Where each output node estimates the probability of belonging to one specific class:
- (Pedestrian)
- (Car)
- (Motorcycle)
- (Truck)
Target Output Representation ()

Instead of representing target label as a single scalar integer , represent as a one-hot vector:
- For a Pedestrian image:
- For a Car image:
- For a Motorcycle image:
- For a Truck image:
During training, the goal is to optimize network parameters such that:
Lecture 07: Regularization
An in-depth guide to regularization in machine learning, covering overfitting, underfitting, regularized cost functions, and regularized linear and logistic regression.
Lecture 09: Neural Networks - Learning
An in-depth guide to learning parameters in Neural Networks, covering the cost function, backpropagation algorithm, parameter unrolling, gradient checking, random weight initialization, and complete workflow implementation.