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

Study methods

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

Deep Learning Lecture 2

  • 32Unfamiliar
  • 0Learning
  • 0Familiar
  • 0Mastered
Topic: Backpropagation and Cost Functions

Why do we process multiple training examples (a batch) at once during forward propagation in a neural network?

32 questions

  1. 1

    Why do we process multiple training examples (a batch) at once during forward propagation in a neural network?

    Model answer

    To leverage vectorization and GPU parallelism for efficient computation.

    Explanation

    Processing a batch of m examples simultaneously allows matrix operations to be performed in parallel on hardware like GPUs, significantly speeding up training compared to processing one example at a time.

  2. 2

    Define the cost function J in terms of the loss function L for a batch of m examples.

    Model answer

    J=1m∑i=1mL(i)J = \frac{1}{m} \sum_{i=1}^{m} L^{(i)}J=m1​∑i=1m​L(i), where L(i)L^{(i)}L(i) is the loss for the i-th example.

    Explanation

    The cost function is the average of the individual loss functions over the entire batch, allowing gradient descent to update parameters based on the overall error.

  3. 3

    Write the binary cross-entropy loss function for a single training example.

    Model answer

    L(y,y^)=−[ylog⁡y^+(1−y)log⁡(1−y^)]L(y, \hat{y}) = -[y \log \hat{y} + (1-y) \log(1-\hat{y})]L(y,y^​)=−[ylogy^​+(1−y)log(1−y^​)]

    Explanation

    This loss measures the difference between the true label y and predicted probability y^\hat{y}y^​, penalizing confident wrong predictions heavily.

  4. 4

    In backpropagation, why do we compute derivatives starting from the last layer (W3, b3) before earlier layers?

    Model answer

    Because the chain rule makes the derivative with respect to later layers easier to compute, and earlier layers depend on those results.

    Explanation

    The relationship between the cost and the weights in the final layer is more direct; computing those first allows reusing intermediate results for earlier layers via the chain rule.

  5. 5

    Explain how the linearity of the derivative simplifies computing the derivative of the cost function J.

    Model answer

    The derivative of J is the average of the derivatives of the individual loss functions: ∂J∂w=1m∑i=1m∂L(i)∂w\frac{\partial J}{\partial w} = \frac{1}{m} \sum_{i=1}^{m} \frac{\partial L^{(i)}}{\partial w}∂w∂J​=m1​∑i=1m​∂w∂L(i)​.

    Explanation

    Since the derivative is a linear operation, the derivative of the average equals the average of the derivatives, allowing us to compute per-example gradients and then average them.

  6. 6

    Write the forward propagation equation for the output layer (layer 3) in a neural network with sigmoid activation.

    Model answer

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

    Explanation

    The linear combination of the previous layer's activations passes through a sigmoid to produce a probability output.

  7. 7

    What is the derivative of the sigmoid activation function σ(z)\sigma(z)σ(z)?

    Model answer

    σ′(z)=σ(z)(1−σ(z))\sigma'(z) = \sigma(z) (1 - \sigma(z))σ′(z)=σ(z)(1−σ(z))

    Explanation

    This derivative is used in backpropagation when computing gradients through a sigmoid layer; it is simple and efficient to compute from the activation value itself.

  8. 8

    In backpropagation, what is the derivative of log⁡(σ(z))\log(\sigma(z))log(σ(z)) with respect to zzz?

    Model answer

    ∂∂zlog⁡(σ(z))=1σ(z)⋅σ′(z)=1−σ(z)\frac{\partial}{\partial z} \log(\sigma(z)) = \frac{1}{\sigma(z)} \cdot \sigma'(z) = 1 - \sigma(z)∂z∂​log(σ(z))=σ(z)1​⋅σ′(z)=1−σ(z)

    Explanation

    Using the chain rule, the derivative of log and sigmoid combine to a simple expression, 1−σ(z)1 - \sigma(z)1−σ(z), which appears in the cross-entropy gradient.

  9. 9

    When computing ∂L∂W[3]\frac{\partial L}{\partial W^{[3]}}∂W[3]∂L​, why is the term ∂z[3]∂W[3]\frac{\partial z^{[3]}}{\partial W^{[3]}}∂W[3]∂z[3]​ equal to a[2]⊤a^{[2]\top}a[2]⊤?

    Model answer

    Because z[3]=W[3]a[2]+b[3]z^{[3]} = W^{[3]} a^{[2]} + b^{[3]}z[3]=W[3]a[2]+b[3], the derivative of a scalar with respect to a matrix W[3]W^{[3]}W[3] (1×2) yields a 1×2 matrix; shape analysis shows it must be a[2]⊤a^{[2]\top}a[2]⊤.

    Explanation

    The derivative of WaW aWa with respect to W is a⊤a^\topa⊤ to match dimensions: WWW is (1×2), aaa is (2×1), so the gradient has shape (1×2).

  10. 10

    Derive ∂L∂W[3]\frac{\partial L}{\partial W^{[3]}}∂W[3]∂L​ for a single training example using binary cross-entropy loss and sigmoid output.

    Model answer

    ∂L∂W[3]=(y−a[3])a[2]⊤\frac{\partial L}{\partial W^{[3]}} = (y - a^{[3]}) a^{[2]\top}∂W[3]∂L​=(y−a[3])a[2]⊤

    Explanation

    Starting from L=−[ylog⁡a[3]+(1−y)log⁡(1−a[3])]L = -[y \log a^{[3]} + (1-y)\log(1-a^{[3]})]L=−[yloga[3]+(1−y)log(1−a[3])] with a[3]=σ(W[3]a[2]+b[3])a^{[3]} = \sigma(W^{[3]} a^{[2]} + b^{[3]})a[3]=σ(W[3]a[2]+b[3]), applying chain rule and simplifying yields this compact result.

  11. 11

    How does the chain rule decompose ∂L∂W[2]\frac{\partial L}{\partial W^{[2]}}∂W[2]∂L​ in terms of intermediate derivatives?

    Model answer

    ∂L∂W[2]=∂L∂a[3]⋅∂a[3]∂z[3]⋅∂z[3]∂a[2]⋅∂a[2]∂z[2]⋅∂z[2]∂W[2]\frac{\partial L}{\partial W^{[2]}} = \frac{\partial L}{\partial a^{[3]}} \cdot \frac{\partial a^{[3]}}{\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]}}∂W[2]∂L​=∂a[3]∂L​⋅∂z[3]∂a[3]​⋅∂a[2]∂z[3]​⋅∂z[2]∂a[2]​⋅∂W[2]∂z[2]​

    Explanation

    This decomposition backpropagates the error from the output through the network to the weights of layer 2, reusing derivatives computed for later layers.

  12. 12

    What is the derivative ∂L∂z[3]\frac{\partial L}{\partial z^{[3]}}∂z[3]∂L​ equal to, based on the derivation for W[3]W^{[3]}W[3]?

    Model answer

    ∂L∂z[3]=y−a[3]\frac{\partial L}{\partial z^{[3]}} = y - a^{[3]}∂z[3]∂L​=y−a[3]

    Explanation

    From ∂L∂W[3]=∂L∂z[3]a[2]⊤\frac{\partial L}{\partial W^{[3]}} = \frac{\partial L}{\partial z^{[3]}} a^{[2]\top}∂W[3]∂L​=∂z[3]∂L​a[2]⊤ and the known result, we identify ∂L∂z[3]=y−a[3]\frac{\partial L}{\partial z^{[3]}} = y - a^{[3]}∂z[3]∂L​=y−a[3].

  13. 13

    Why is caching (storing intermediate values) important during forward propagation?

    Model answer

    To avoid recomputing activations and linear outputs during backpropagation, improving computational efficiency at the cost of memory.

    Explanation

    Values like a[1],a[2],z[2],W[3]a^{[1]}, a^{[2]}, z^{[2]}, W^{[3]}a[1],a[2],z[2],W[3] are needed in backprop; caching them saves forward passes during backward computation.

  14. 14

    When computing ∂L∂W[2]\frac{\partial L}{\partial W^{[2]}}∂W[2]∂L​ via chain rule, which term involves an element-wise product?

    Model answer

    The derivative of the activation function ∂a[2]∂z[2]\frac{\partial a^{[2]}}{\partial z^{[2]}}∂z[2]∂a[2]​ is an element-wise product (e.g., a[2](1−a[2])a^{[2]}(1-a^{[2]})a[2](1−a[2]) for sigmoid).

    Explanation

    Since activation functions are applied element-wise, their derivatives also act element-wise when backpropagating through that layer.

  15. 15

    In a neural network with no activation functions (identity activations), what does the entire network reduce to?

    Model answer

    A linear regression model, regardless of depth.

    Explanation

    Without nonlinear activations, successive linear transformations compose into a single linear transformation, making the network equivalent to a linear model.

  16. 16

    List three common activation functions and their output ranges.

    Model answer

    Sigmoid: (0,1); Tanh: (-1,1); ReLU: [0, ∞)

    Explanation

    These ranges affect how gradients flow: sigmoid and tanh saturate at extremes, while ReLU does not for positive inputs.

  17. 17

    What is the main disadvantage of the sigmoid activation function?

    Model answer

    It saturates (gradient near zero) for large positive or negative inputs, causing vanishing gradients.

    Explanation

    When |z| is large, the sigmoid derivative becomes very small, slowing or stopping learning in early layers.

  18. 18

    Write the derivative of the ReLU activation function.

    Model answer

    ReLU′(z)={1if z>00if z≤0\text{ReLU}'(z) = \begin{cases} 1 & \text{if } z > 0 \\ 0 & \text{if } z \le 0 \end{cases}ReLU′(z)={10​if z>0if z≤0​

    Explanation

    The derivative is 1 for positive z, allowing gradients to pass through unchanged, and 0 for non-positive z, creating sparsity.

  19. 19

    When would you use a tanh activation in the output layer?

    Model answer

    When the desired output is between -1 and 1, such as reward values in reinforcement learning.

    Explanation

    Tanh maps inputs to (-1,1), making it suitable for outputs like normalized rewards or centered data.

  20. 20

    What is a common practice for choosing activation functions across layers in a neural network?

    Model answer

    Use the same activation function for all hidden layers (e.g., ReLU), and a different one for the output layer based on the task.

    Explanation

    Empirically, using one activation per layer (e.g., ReLU hidden, sigmoid for binary classification) works well, and mixing within a layer is uncommon.

  21. 21

    Why is random initialization of weights important?

    Model answer

    To break symmetry; without randomness, neurons in the same layer would learn identical features.

    Explanation

    If all weights start equally, gradients are the same for each neuron, preventing diversification.

  22. 22

    Describe the vanishing gradient problem in deep neural networks.

    Model answer

    Gradients become extremely small as they are backpropagated through many layers, making early layers learn very slowly or not at all.

    Explanation

    When activation derivatives (e.g., sigmoid) are <1, repeated multiplication causes gradients to exponentially shrink with depth.

  23. 23

    Explain the exploding gradient problem.

    Model answer

    Gradients become excessively large, causing unstable updates and divergence during training.

    Explanation

    When weight matrices have eigenvalues >1, repeated multiplication in backprop leads to exponentially growing gradients.

  24. 24

    What is the purpose of input normalization?

    Model answer

    To center the data (subtract mean) and scale to unit variance, which speeds up optimization by making the loss landscape more spherical.

    Explanation

    Normalization prevents features with large ranges from dominating the gradients, allowing gradient descent to move more directly toward the minimum.

  25. 25

    When normalizing inputs, should you use the mean and variance computed on the training set or the test set for test data?

    Model answer

    Use the mean and variance computed on the training set.

    Explanation

    The network was trained on normalized training data; applying the same transformation ensures consistent input distribution across training and test.

  26. 26

    State a common weight initialization for layers with sigmoid activation.

    Model answer

    Initialize weights from a distribution with variance 1n[l−1]\frac{1}{n^{[l-1]}}n[l−1]1​, e.g., W[l]∼N(0,1n[l−1])W^{[l]} \sim \mathcal{N}(0, \frac{1}{n^{[l-1]}})W[l]∼N(0,n[l−1]1​).

    Explanation

    This keeps the linear outputs z from growing too large, reducing saturation and aiding gradient flow.

  27. 27

    What modification to the initialization variance is often used for ReLU activations?

    Model answer

    Use variance 2n[l−1]\frac{2}{n^{[l-1]}}n[l−1]2​ instead of 1n[l−1]\frac{1}{n^{[l-1]}}n[l−1]1​.

    Explanation

    Empirically, this helps ReLU networks train better, possibly compensating for the zero gradient on negative side.

  28. 28

    What is the Xavier (Glorot) initialization, and which activation is it typically used for?

    Model answer

    Initialize weights with variance 2n[l−1]+n[l]\frac{2}{n^{[l-1]} + n^{[l]}}n[l−1]+n[l]2​, often used with tanh activation.

    Explanation

    It considers both forward and backward passes, balancing the variance of activations and gradients.

  29. 29

    What is the advantage of mini-batch gradient descent over batch gradient descent?

    Model answer

    It provides a trade-off between vectorization efficiency (using batches) and faster updates (not needing full dataset per iteration).

    Explanation

    Mini-batch allows leveraging GPU parallelism with moderate batch sizes (e.g., 1000) while making more frequent parameter updates than full-batch.

  30. 30

    How does the cost function graph differ between batch gradient descent and mini-batch gradient descent?

    Model answer

    Batch gradient descent shows a smooth decreasing curve; mini-batch decreases with noise due to gradient approximation.

    Explanation

    Mini-batch gradients are estimates based on the batch, so the cost may fluctuate but trends downward faster per computation.

  31. 31

    Describe the intuition behind momentum in gradient descent.

    Model answer

    Momentum accumulates past gradient updates (velocity) to dampen oscillations in directions of high curvature and accelerate in consistent directions.

    Explanation

    By averaging gradients over time, momentum reduces zigzagging in steep ravines, leading to faster convergence.

  32. 32

    Write the update equations for momentum gradient descent.

    Model answer

    v=βv+(1−β)∂J∂wv = \beta v + (1-\beta) \frac{\partial J}{\partial w}v=βv+(1−β)∂w∂J​; w=w−αvw = w - \alpha vw=w−αv

    Explanation

    Velocity vvv is an exponentially weighted average of past gradients, smoothed by β\betaβ (commonly 0.9). The weight is then updated by velocity times learning rate.

1 of 32
Question 1 of 32

Ask anything

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

FlashcardsQuizFill in the blanksWritten testLessonNotesPodcastContent