1. Home
  2. Explore
  3. Computer Science
  4. Deep Learning Intro

Study methods

Flashcards
Quiz
Fill in the blanks
Written test
Lesson
Notes
Podcast
Content

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×364 \times 64 \times 364×64×3 – one channel each for red, green, and blue. To use logistic regression, this matrix is flattened into a single column vector xxx of dimension 64×64×3=12,28864 \times 64 \times 3 = 12{,}28864×64×3=12,288.

Model Operation: The output y^\hat{y}y^​ is computed as:

y^=σ(wTx+b)\hat{y} = \sigma(w^T x + b)y^​=σ(wTx+b)

where σ\sigmaσ is the sigmoid function, mapping (−∞,∞)(-\infty, \infty)(−∞,∞) to (0,1)(0, 1)(0,1). www is a row vector of shape 1×12,2881 \times 12{,}2881×12,288, and bbb 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+bz = w^T x + bz=wTx+b, then apply an activation function (e.g., sigmoid) to get a=σ(z)a = \sigma(z)a=σ(z). The entire logistic regression model is a single neuron.

Training Process:

  1. Initialize parameters www and bbb.

  2. 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.

  3. 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} $ThederivativeofthesigmoidisThe derivative of the sigmoid isThederivativeofthesigmoidis\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 xxx. The output becomes a vector y^=[y^1,y^2,y^3]T\hat{y} = [\hat{y}_1, \hat{y}_2, \hat{y}_3]^Ty^​=[y^​1​,y^​2​,y^​3​]T, where each y^i\hat{y}_iy^​i​ is the sigmoid of a linear combination:

y^1=σ(w1Tx+b1),y^2=σ(w2Tx+b2),y^3=σ(w3Tx+b3)\hat{y}_1 = \sigma(w_1^T x + b_1), \quad \hat{y}_2 = \sigma(w_2^T x + b_2), \quad \hat{y}_3 = \sigma(w_3^T x + b_3)y^​1​=σ(w1T​x+b1​),y^​2​=σ(w2T​x+b2​),y^​3​=σ(w3T​x+b3​)

Notation: Square brackets [1][1][1] denote the layer index. Subscripts identify the neuron within the layer: a1[1]a^{[1]}_1a1[1]​ is the activation of neuron 1 in layer 1.

Number of Parameters: Each neuron has its own weight vector (1×12,2881 \times 12{,}2881×12,288) and bias, giving 3×12,288+3=36,8673 \times 12{,}288 + 3 = 36{,}8673×12,288+3=36,867 parameters. This equals 3n+33n + 33n+3 for nnn inputs.

Labeling and Training: Labels for multi-class are one-hot vectors, e.g., cat = [1,0,0][1, 0, 0][1,0,0], lion = [0,1,0][0, 1, 0][0,1,0], iguana = [0,0,1][0, 0, 1][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:

L=∑i=13−yilog⁡(y^i)−(1−yi)log⁡(1−y^i)\mathcal{L} = \sum_{i=1}^{3} -y_i \log(\hat{y}_i) - (1-y_i) \log(1-\hat{y}_i)L=∑i=13​−yi​log(y^​i​)−(1−yi​)log(1−y^​i​)

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,z3z_1, z_2, z_3z1​,z2​,z3​. The network architecture remains three neurons, but the activation of the layer is changed to:

y^k=ak[1]=ezk∑j=13ezj,for k=1,2,3\hat{y}_k = a^{[1]}_k = \frac{e^{z_k}}{\sum_{j=1}^{3} e^{z_j}}, \quad \text{for } k = 1,2,3y^​k​=ak[1]​=∑j=13​ezj​ezk​​,for k=1,2,3

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][1,1,0][1,1,0] sum to 2, which is unreachable. The appropriate loss is the cross-entropy loss:

L=−∑k=13yklog⁡(y^k)\mathcal{L} = -\sum_{k=1}^{3} y_k \log(\hat{y}_k)L=−∑k=13​yk​log(y^​k​)

This penalizes incorrect probabilities and respects the exclusive constraint.

Effect on Derivatives: Unlike independent sigmoids, each output y^k\hat{y}_ky^​k​ depends on all zjz_jzj​ 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: ∂L∂zk=y^k−yk\frac{\partial \mathcal{L}}{\partial z_k} = \hat{y}_k - y_k∂zk​∂L​=y^​k​−yk​.

Number of parameters remains 3n+33n + 33n+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]a^{[1]}a[1] (hidden layer activations), a[2]a^{[2]}a[2] (output). Each neuron in layer lll computes:

zi[l]=wi[l]a[l−1]+bi[l],ai[l]=σ(zi[l])z^{[l]}_i = w^{[l]}_i a^{[l-1]} + b^{[l]}_i, \quad a^{[l]}_i = \sigma(z^{[l]}_i)zi[l]​=wi[l]​a[l−1]+bi[l]​,ai[l]​=σ(zi[l]​)

Number of Parameters:

  • Layer 1: 3×12,2883 \times 12{,}2883×12,288 weights + 333 biases = 36,86736{,}86736,867.

  • Layer 2: 2×3=62 \times 3 = 62×3=6 weights + 222 biases = 888.

  • Layer 3: 1×2=21 \times 2 = 21×2=2 weights + 111 bias = 333. Total: 36,87836{,}87836,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:

z[1]=W[1]x+b[1],a[1]=σ(z[1])z^{[1]} = W^{[1]} x + b^{[1]}, \quad a^{[1]} = \sigma(z^{[1]})z[1]=W[1]x+b[1],a[1]=σ(z[1])
z[2]=W[2]a[1]+b[2],a[2]=σ(z[2])z^{[2]} = W^{[2]} a^{[1]} + b^{[2]}, \quad a^{[2]} = \sigma(z^{[2]})z[2]=W[2]a[1]+b[2],a[2]=σ(z[2])
z[3]=W[3]a[2]+b[3],a[3]=σ(z[3])=y^z^{[3]} = W^{[3]} a^{[2]} + b^{[3]}, \quad a^{[3]} = \sigma(z^{[3]}) = \hat{y}z[3]=W[3]a[2]+b[3],a[3]=σ(z[3])=y^​

Shapes (single example, xxx is n×1n \times 1n×1, n=12,288n=12{,}288n=12,288):

  • W[1]W^{[1]}W[1]: 3×n3 \times n3×n, b[1]b^{[1]}b[1]: 3×13 \times 13×1, z[1]z^{[1]}z[1]: 3×13 \times 13×1

  • W[2]W^{[2]}W[2]: 2×32 \times 32×3, b[2]b^{[2]}b[2]: 2×12 \times 12×1, z[2]z^{[2]}z[2]: 2×12 \times 12×1

  • W[3]W^{[3]}W[3]: 1×21 \times 21×2, b[3]b^{[3]}b[3]: 1×11 \times 11×1

Batch Processing (m examples): To parallelize, we stack column vectors into matrices. Let XXX be n×mn \times mn×m (each column is one flattened image). The forward equations become:

Z[1]=W[1]X+b[1],A[1]=σ(Z[1])Z^{[1]} = W^{[1]} X + b^{[1]}, \quad A^{[1]} = \sigma(Z^{[1]})Z[1]=W[1]X+b[1],A[1]=σ(Z[1])
Z[2]=W[2]A[1]+b[2],A[2]=σ(Z[2])Z^{[2]} = W^{[2]} A^{[1]} + b^{[2]}, \quad A^{[2]} = \sigma(Z^{[2]})Z[2]=W[2]A[1]+b[2],A[2]=σ(Z[2])
Z[3]=W[3]A[2]+b[3],A[3]=σ(Z[3])Z^{[3]} = W^{[3]} A^{[2]} + b^{[3]}, \quad A^{[3]} = \sigma(Z^{[3]})Z[3]=W[3]A[2]+b[3],A[3]=σ(Z[3])

Now Z[1]Z^{[1]}Z[1] is 3×m3 \times m3×m, A[1]A^{[1]}A[1] is 3×m3 \times m3×m, etc. The additive biases b[1]b^{[1]}b[1] (3×13 \times 13×1) must be added to each column of W[1]XW^{[1]}XW[1]X (3×m3 \times m3×m). This is made possible by broadcasting: the small vector is replicated mmm 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 JJJ for a batch of mmm examples is the average of individual losses:

J(W,b)=1m∑i=1mL(y^(i),y(i))J(W,b) = \frac{1}{m} \sum_{i=1}^{m} \mathcal{L}(\hat{y}^{(i)}, y^{(i)})J(W,b)=m1​∑i=1m​L(y^​(i),y(i))

For binary classification, L\mathcal{L}L is the logistic loss. The goal of training is to minimize JJJ over all parameters W[1],b[1],W[2],b[2],W[3],b[3]W^{[1]}, b^{[1]}, W^{[2]}, b^{[2]}, W^{[3]}, b^{[3]}W[1],b[1],W[2],b[2],W[3],b[3].

Gradient Descent Update for each layer lll:

W[l]:=W[l]−α∂J∂W[l],b[l]:=b[l]−α∂J∂b[l]W^{[l]} := W^{[l]} - \alpha \frac{\partial J}{\partial W^{[l]}}, \quad b^{[l]} := b^{[l]} - \alpha \frac{\partial J}{\partial b^{[l]}}W[l]:=W[l]−α∂W[l]∂J​,b[l]:=b[l]−α∂b[l]∂J​

Backpropagation efficiently computes these gradients using the chain rule. The name comes from starting at the output and moving backward:

  1. Output layer (layer 3): Compute ∂J∂W[3]\frac{\partial J}{\partial W^{[3]}}∂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.,Each piece is easy: e.g.,Eachpieceiseasy:e.g.,\frac{\partial z^{[3]}}{\partial W^{[3]}} = (a^{[2]})^T$ (vectorized).

  2. Hidden layer (layer 2): Compute ∂J∂W[2]\frac{\partial J}{\partial W^{[2]}}∂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]}} $ThefirsttwofactorscombinetogiveThe first two factors combine to giveThefirsttwofactorscombinetogive\frac{\partial J}{\partial a^{[2]}}$, part of which was already computed for layer 3.

  3. First hidden layer (layer 1): Similarly, reuse ∂J∂z[2]\frac{\partial J}{\partial z^{[2]}}∂z[2]∂J​ to compute ∂J∂W[1]\frac{\partial J}{\partial W^{[1]}}∂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]a^{[2]}a[2] does not depend on W[1]W^{[1]}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.

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent