Deep Learning Intro
Deep Learning and Neural Networks: Comprehensive Study Notes
Logistic Regression as a Simple Neural Network
Logistic regression is a binary classification algorithm that can be interpreted as a neural network with a single neuron. For a classification task like detecting whether an image contains a cat, the goal is to output a value close to 1 (cat present) or 0 (no cat).
Image Representation: A color image of size 64x64 pixels is represented as a 3D matrix of shape 64×64×3 – one channel each for red, green, and blue. To use logistic regression, this matrix is flattened into a single column vector x of dimension 64×64×3=12,288.
Model Operation: The output y^ is computed as:
where σ is the sigmoid function, mapping (−∞,∞) to (0,1). w is a row vector of shape 1×12,288, and b is a scalar bias. Thus the model has 12,288 weights and 1 bias – one parameter per input pixel, plus the bias.
Definition: A neuron is the operation
linear + activation: first compute z=wTx+b, then apply an activation function (e.g., sigmoid) to get a=σ(z). The entire logistic regression model is a single neuron.
Training Process:
Initialize parameters w and b.
Optimize by minimizing a loss function (proxy for the true objective). For binary classification, the logistic loss (binary cross-entropy) is used: $$ \mathcal{L}(\hat{y}, y) = -y \log(\hat{y}) - (1-y) \log(1-\hat{y}) $$ This loss comes from maximum likelihood estimation under a Bernoulli model.
Apply gradient descent to update parameters iteratively: $$ w := w - \alpha \frac{\partial \mathcal{L}}{\partial w}, \quad b := b - \alpha \frac{\partial \mathcal{L}}{\partial b} $Thederivativeofthesigmoidis\sigma(z)(1-\sigma(z))$, making gradient computation straightforward.
Two Key Equations:
Neuron = linear part + activation part.
Model = architecture (the structure of the network) + parameters (weights and biases). A trained model consists of two files: architecture definition and learned parameter values.
Multi-Class Classification with Multiple Independent Neurons
When the task expands to detect three classes (cat, lion, iguana), we use three neurons in a single layer, each performing logistic regression independently on the same input x. The output becomes a vector y^=[y^1,y^2,y^3]T, where each y^i is the sigmoid of a linear combination:
Notation: Square brackets [1] denote the layer index. Subscripts identify the neuron within the layer: a1[1] is the activation of neuron 1 in layer 1.
Number of Parameters: Each neuron has its own weight vector (1×12,288) and bias, giving 3×12,288+3=36,867 parameters. This equals 3n+3 for n inputs.
Labeling and Training: Labels for multi-class are one-hot vectors, e.g., cat = [1,0,0], lion = [0,1,0], iguana = [0,0,1]. The neurons are trained independently – each learns to detect one class because the labeling defines its responsibility. Even if an image contains multiple animals (e.g., cat and lion), with enough data the network can still learn robustly because the neurons do not interact.
Loss Function: For multiple independent sigmoids, the loss for one example is the sum of binary logistic losses per class:
This sum is straightforward to differentiate because each term depends only on the parameters of its own neuron.
Softmax Regression for Exclusive Multi-Class Classification
When the classes are mutually exclusive (at most one animal per image), we replace the independent sigmoids with a softmax function over the linear outputs z1,z2,z3. The network architecture remains three neurons, but the activation of the layer is changed to:
This ensures the output vector sums to 1, forming a probability distribution over classes.
Why change the loss? The logistic loss cannot handle the sum-to-one constraint because labels like [1,1,0] sum to 2, which is unreachable. The appropriate loss is the cross-entropy loss:
This penalizes incorrect probabilities and respects the exclusive constraint.
Effect on Derivatives: Unlike independent sigmoids, each output y^k depends on all zj through the denominator. Therefore, the derivative of the loss with respect to a weight for neuron 2 involves parameters of neurons 1 and 3 as well. This makes gradient computation more complex but is handled by a neat formula: ∂zk∂L=y^k−yk.
Number of parameters remains 3n+3 – same as the independent sigmoid version.
Regression Modifications: If the goal were to predict a continuous value (e.g., cat age), we would:
Change the activation to a linear function (or ReLU for non-negative outputs).
Switch the loss to mean squared error (L2 loss) or mean absolute error (L1 loss), which are better suited for regression optimization.
Neural Networks with Hidden Layers
To improve capacity, we add intermediate layers between input and output. For cat detection (binary), a network might have:
Input layer: 12,288 neurons (just the input values).
Hidden layer: 3 neurons.
Output layer: 1 neuron (sigmoid for binary classification).
Notation: Layers are indexed with square brackets: a[1] (hidden layer activations), a[2] (output). Each neuron in layer l computes:
Number of Parameters:
Layer 1: 3×12,288 weights + 3 biases = 36,867.
Layer 2: 2×3=6 weights + 2 biases = 8.
Layer 3: 1×2=2 weights + 1 bias = 3. Total: 36,878 – still dominated by the input layer.
Vocabulary:
Layer: A group of neurons with no connections among themselves.
Hidden layer: A layer that is neither input nor output; it sees only the previous layer's activations and the next layer's error.
The network shown has 3 layers (input is not counted as a layer in some conventions; here it is called layer 0 or simply input).
Why Hidden Layers? They allow the network to learn hierarchical representations. In image classification:
First hidden neurons detect simple patterns like edges (because they only see pixels).
Second hidden neurons combine edges into parts (e.g., ears, mouth).
Output neuron assembles parts into a full concept (cat face).
This is called end-to-end learning: the network learns useful features automatically from raw input to output without hand-engineering.
Fully Connected Layers: Every neuron in one layer is connected to every neuron in the next layer. This is the standard architecture used when no prior knowledge is imposed.
Forward Propagation and Vectorization
Forward propagation computes activations layer by layer. For the three-layer network described:
Shapes (single example, x is n×1, n=12,288):
W[1]: 3×n, b[1]: 3×1, z[1]: 3×1
W[2]: 2×3, b[2]: 2×1, z[2]: 2×1
W[3]: 1×2, b[3]: 1×1
Batch Processing (m examples): To parallelize, we stack column vectors into matrices. Let X be n×m (each column is one flattened image). The forward equations become:
Now Z[1] is 3×m, A[1] is 3×m, etc. The additive biases b[1] (3×1) must be added to each column of W[1]X (3×m). This is made possible by broadcasting: the small vector is replicated m times automatically in libraries like numpy (Python) or bsxfun (MATLAB). Broadcasting does not add new parameters – it merely repeats the same bias for every example.
Cost Function and Backward Propagation
The cost function J for a batch of m examples is the average of individual losses:
For binary classification, L is the logistic loss. The goal of training is to minimize J over all parameters W[1],b[1],W[2],b[2],W[3],b[3].
Gradient Descent Update for each layer l:
Backpropagation efficiently computes these gradients using the chain rule. The name comes from starting at the output and moving backward:
Output layer (layer 3): Compute ∂W[3]∂J by decomposing: $$ \frac{\partial J}{\partial W^{[3]}} = \frac{\partial J}{\partial a^{[3]}} \cdot \frac{\partial a^{[3]}}{\partial z^{[3]}} \cdot \frac{\partial z^{[3]}}{\partial W^{[3]}} $Eachpieceiseasy:e.g.,\frac{\partial z^{[3]}}{\partial W^{[3]}} = (a^{[2]})^T$ (vectorized).
Hidden layer (layer 2): Compute ∂W[2]∂J by reusing the result from layer 3: $$ \frac{\partial J}{\partial W^{[2]}} = \frac{\partial J}{\partial z^{[3]}} \cdot \frac{\partial z^{[3]}}{\partial a^{[2]}} \cdot \frac{\partial a^{[2]}}{\partial z^{[2]}} \cdot \frac{\partial z^{[2]}}{\partial W^{[2]}} $Thefirsttwofactorscombinetogive\frac{\partial J}{\partial a^{[2]}}$, part of which was already computed for layer 3.
First hidden layer (layer 1): Similarly, reuse ∂z[2]∂J to compute ∂W[1]∂J.
This reuse of intermediate gradients is the essence of backpropagation – it avoids redundant computation. The chain rule path must respect the forward computation graph to avoid zero derivatives (e.g., a[2] does not depend on W[1] directly).
Flexibility and Tuning of Neural Networks
Building a neural network involves many design choices:
Activation functions: sigmoid, tanh, ReLU (rectified linear unit), softmax, linear.
Loss functions: binary cross-entropy, categorical cross-entropy, mean squared error.
Number of layers and neurons per layer: This architecture hyperparameter is typically chosen by cross-validation. Deeper networks can model more complex functions but require more data and computation.
Optimization algorithm: Batch gradient descent, stochastic gradient descent, Adam, etc.
Regularization: dropout, weight decay, early stopping.
There is no single correct architecture – experimentation and validation set performance guide the choice. The lecture emphasizes that model = architecture + parameters; both files (architecture definition and learned weights) are needed for deployment.

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