j-devlog
KO
~/nav

// categories

// tags

[MMD]

Calculus Concept Practice: Problems You Encounter in ML Optimization

·10 min read·

This post is a set of practice problems applying the concepts covered in Calculus Chapters 1–3 to real-world machine learning scenarios. The focus is on applying and interpreting concepts rather than on symbolic computation.


Part 1. Differentiation and Activation Functions#

Problem 1-1. Chain Rule and Activation Function Derivatives#

ReLU, a commonly used activation function in deep learning, is defined as follows.


ReLU(x)=max(0,x)\text{ReLU}(x) = \max(0, x)


What is dReLUdx\frac{d \text{ReLU}}{dx} during backpropagation, and what does it mean for training?

Show Answer

dReLUdx={1x>00x0\frac{d \text{ReLU}}{dx} = \begin{cases} 1 & x > 0 \\ 0 & x \leq 0 \end{cases}


Training implications:

  • When x>0x > 0, the gradient passes through unchanged → error signals propagate to deeper layers
  • When x0x \leq 0, the gradient is 0 → that neuron receives no update

This is known as the Dead ReLU problem. Once a neuron's output becomes permanently 0, it will never activate again.

Solutions: Use Leaky ReLU (outputs 0.01x0.01x instead of 00 when x0x \leq 0), ELU, GELU, etc.

Comparison with Sigmoid: The derivative of sigmoid is σ(1σ)0.25\sigma(1-\sigma) \leq 0.25, which is always less than 1. In deep networks, this causes gradients to shrink with each layer — the vanishing gradient problem. ReLU avoids this.


Problem 1-2. Interpreting Loss Function Derivatives#

Compare the derivatives of these two loss functions.

  • MSE: L=12(y^y)2\mathcal{L} = \frac{1}{2}(\hat{y} - y)^2
  • MAE: L=y^y\mathcal{L} = |\hat{y} - y|

What does each look like when differentiated with respect to y^\hat{y}? What practical differences does this create?

Show Answer

MSE derivative:


dLdy^=y^y\frac{d\mathcal{L}}{d\hat{y}} = \hat{y} - y


MAE derivative:


dLdy^=sign(y^y)={+1y^>y1y^<y\frac{d\mathcal{L}}{d\hat{y}} = \text{sign}(\hat{y} - y) = \begin{cases} +1 & \hat{y} > y \\ -1 & \hat{y} < y \end{cases}

PropertyMSEMAE
Gradient magnitudeProportional to error (large error → large update)Always ±1 (constant update)
Outlier sensitivityHigh (squared error → outliers over-penalized)Low (linear error)
Near the minimumGradient → 0 (smooth convergence)Gradient changes sign abruptly
DifferentiabilityEverywhereDiscontinuous at y^=y\hat{y} = y

Practical selection criteria:

  • Data with many outliers → MAE (minimizes outlier influence)
  • Want to penalize outliers → MSE
  • Robust to outliers but still differentiable → Huber Loss (combines MSE and MAE)

Problem 1-3. Tracing the Chain Rule#

Differentiate f(x)=log(σ(wx+b))f(x) = \log(\sigma(wx + b)) with respect to ww. (σ\sigma = sigmoid)

Show Answer

Applying the chain rule step by step:


f=log(u),u=σ(v),v=wx+bf = \log(u), \quad u = \sigma(v), \quad v = wx + b


dfdw=dfdududvdvdw\frac{df}{dw} = \frac{df}{du} \cdot \frac{du}{dv} \cdot \frac{dv}{dw}


Each term:

  • dfdu=1u=1σ(v)\frac{df}{du} = \frac{1}{u} = \frac{1}{\sigma(v)}
  • dudv=σ(v)(1σ(v))\frac{du}{dv} = \sigma(v)(1 - \sigma(v))
  • dvdw=x\frac{dv}{dw} = x

Combining:


dfdw=1σ(v)σ(v)(1σ(v))x=(1σ(wx+b))x\frac{df}{dw} = \frac{1}{\sigma(v)} \cdot \sigma(v)(1 - \sigma(v)) \cdot x = (1 - \sigma(wx+b)) \cdot x


Intuition: In a classification model, the gradient of the loss takes the form (1y^)x(1 - \hat{y}) \cdot x — the further off the prediction (the lower the probability), the larger the update.


Part 2. Loss Function Optimization#

Problem 2-1. Why Log Loss Instead of MSE?#

Explain why log loss is preferred over MSE for classification problems from two perspectives: (1) probabilistic, and (2) optimization.

Show Answer

(1) Probabilistic perspective — MLE

The sigmoid output y^\hat{y} is a probability. The likelihood over nn data points:


L=i=1ny^iyi(1y^i)1yiL = \prod_{i=1}^{n} \hat{y}_i^{y_i} (1 - \hat{y}_i)^{1-y_i}


Maximizing this = maximizing log-likelihood = minimizing negative log loss:


logL=i[yilogy^i+(1yi)log(1y^i)]-\log L = -\sum_i [y_i \log \hat{y}_i + (1-y_i)\log(1-\hat{y}_i)]


MSE is equivalent to MLE under a Gaussian distribution assumption, so it is not a natural fit for probability outputs in the range (0, 1).

(2) Optimization perspective — gradient shape

Using MSE for classification gives:


MSEw=(y^y)σ(z)x=(y^y)y^(1y^)x\frac{\partial \text{MSE}}{\partial w} = (\hat{y} - y) \cdot \sigma'(z) \cdot x = (\hat{y} - y) \cdot \hat{y}(1 - \hat{y}) \cdot x


When y^\hat{y} is close to 0 or 1, σ(z)0\sigma'(z) \approx 0vanishing gradient.

Using log loss instead:


LogLossw=(y^y)x\frac{\partial \text{LogLoss}}{\partial w} = (\hat{y} - y) \cdot x


The σ(z)\sigma'(z) term cancels out cleanly, enabling learning without gradient vanishing.


Problem 2-2. Analyzing the Optimal Learning Rate with Partial Derivatives#

Given a loss function of the form L(α)=α24α+5L(\alpha) = \alpha^2 - 4\alpha + 5, find the optimal learning rate α\alpha^*. (Here α\alpha is the search variable for finding the learning rate.)

Show Answer

L(α)=2α4=0L'(\alpha) = 2\alpha - 4 = 0


α=2\alpha^* = 2


L(α)=2>0confirmed minimumL''(\alpha) = 2 > 0 \Rightarrow \text{confirmed minimum}


L(2)=48+5=1L(2) = 4 - 8 + 5 = 1


Interpretation: This example illustrates the principle behind Line Search. In gradient descent, the direction of each step is determined by the gradient, but the step size (learning rate) is chosen to minimize the loss along that direction. This is called Optimal Line Search, and it converges faster than a fixed learning rate.


Problem 2-3. Minimizing a Multivariate Loss Function#

Find the (m,b)(m, b) that minimizes the loss function E(m,b)=(m+b3)2+(2m+b5)2E(m, b) = (m + b - 3)^2 + (2m + b - 5)^2.

Show Answer

Setting partial derivatives to 0:


Em=2(m+b3)+4(2m+b5)=10m+6b26=0\frac{\partial E}{\partial m} = 2(m+b-3) + 4(2m+b-5) = 10m + 6b - 26 = 0


Eb=2(m+b3)+2(2m+b5)=6m+4b16=0\frac{\partial E}{\partial b} = 2(m+b-3) + 2(2m+b-5) = 6m + 4b - 16 = 0


Solving the system of equations:


10m+6b=26(1)10m + 6b = 26 \quad \cdots (1) 6m+4b=16(2)6m + 4b = 16 \quad \cdots (2)


(1)×2(2)×3(1) \times 2 - (2) \times 3:


20m18m=52482m=4m=220m - 18m = 52 - 48 \Rightarrow 2m = 4 \Rightarrow m = 2


b=16124=1b = \frac{16 - 12}{4} = 1


Verification: Plugging the line y=2x+1y = 2x + 1 into the points (1,3)(1, 3) and (2,5)(2, 5) gives exact matches — for this dataset, the analytical minimum yields zero error.


Part 3. Gradient Descent and Backpropagation#

Problem 3-1. Diagnosing Learning Rate Issues#

During model training, you observe the following. What is the problem in each case, and how would you fix it?

(a) The loss drops sharply at first, then suddenly becomes NaN.

(b) The loss decreases very slowly and hasn't converged even after thousands of epochs.

(c) The loss oscillates and hovers around a fixed value without descending further.

Show Answer

(a) NaN — Learning rate too large

Gradients are exploding, or the learning rate is so large that the loss diverges to \infty, resulting in NaN.

Fix: Reduce the learning rate by 10×, or apply Gradient Clipping.

(b) Slow convergence — Learning rate too small

The step size is so tiny that it takes an enormous number of steps to reach the minimum.

Fix: Increase the learning rate by 10×, or use a learning rate scheduler (large at the start, small toward the end).

(c) Oscillation — Learning rate slightly too large, or near a minimum

The optimizer keeps overshooting and bouncing back and forth around the minimum.

Fix: Reduce the learning rate by 2–5×, or use Momentum or the Adam optimizer.


Problem 3-2. Computing Backpropagation Gradients#

For a perceptron classifier with y^=σ(wx+b)\hat{y} = \sigma(wx + b) and log loss L=[ylogy^+(1y)log(1y^)]\mathcal{L} = -[y \log \hat{y} + (1-y)\log(1-\hat{y})]:

Given w=0.5w = 0.5, b=0.1b = 0.1, x=2x = 2, y=1y = 1, what is Lw\frac{\partial \mathcal{L}}{\partial w}?

Show Answer

Step 1: Forward pass


z=wx+b=0.5×2+0.1=1.1z = wx + b = 0.5 \times 2 + 0.1 = 1.1


y^=σ(1.1)=11+e1.10.7503\hat{y} = \sigma(1.1) = \frac{1}{1 + e^{-1.1}} \approx 0.7503


Step 2: Backpropagation

Gradient of log loss with respect to ww:


Lw=(y^y)x=(0.75031)×2=0.4994\frac{\partial \mathcal{L}}{\partial w} = (\hat{y} - y) \cdot x = (0.7503 - 1) \times 2 = -0.4994


Interpretation: The gradient is negative, so increasing ww will reduce the loss.


wnew=wα(0.4994)=0.5+0.4994αw_{\text{new}} = w - \alpha \cdot (-0.4994) = 0.5 + 0.4994\alpha


With learning rate α=0.1\alpha = 0.1, wnew0.5499w_{\text{new}} \approx 0.5499.


Problem 3-3. Gradient Descent at a Saddle Point#

The point (0,0)(0, 0) of the function f(x,y)=x2y2f(x, y) = x^2 - y^2 is a saddle point. What happens when you apply gradient descent starting from (0.01,0)(0.01, 0)?

Show Answer

Computing the gradient:


f=[2x2y]\nabla f = \begin{bmatrix} 2x \\ -2y \end{bmatrix}


At the initial point (0.01,0)(0.01, 0):


f(0.01,0)=[0.020]\nabla f(0.01, 0) = \begin{bmatrix} 0.02 \\ 0 \end{bmatrix}


Update direction:


xnew=0.01α×0.02(x direction: toward origin)x_{\text{new}} = 0.01 - \alpha \times 0.02 \quad \text{(x direction: toward origin)} ynew=0α×0=0(y direction: no change)y_{\text{new}} = 0 - \alpha \times 0 = 0 \quad \text{(y direction: no change)}


In the xx direction, the optimizer moves toward the local minimum (origin), but yy stays fixed at 0 and the process converges to the origin — which is actually a local maximum in the yy direction.

Conclusion: Gradient descent can converge to saddle points depending on the initialization. In deep learning, the noise from SGD (stochastic gradient descent) and the use of momentum help escape saddle points.


Problem 3-4. Classifying Critical Points with the Hessian#

Find the stationary points (where gradient = 0) of f(x,y)=x2+4xy+y2f(x, y) = x^2 + 4xy + y^2, and use the Hessian to classify each point.

Show Answer

Setting gradient to 0:


fx=2x+4y=0\frac{\partial f}{\partial x} = 2x + 4y = 0 fy=4x+2y=0\frac{\partial f}{\partial y} = 4x + 2y = 0


x=0,y=0x = 0, \quad y = 0


Computing the Hessian:


H=[2fx22fxy2fyx2fy2]=[2442]H = \begin{bmatrix} \frac{\partial^2 f}{\partial x^2} & \frac{\partial^2 f}{\partial x \partial y} \\ \frac{\partial^2 f}{\partial y \partial x} & \frac{\partial^2 f}{\partial y^2} \end{bmatrix} = \begin{bmatrix} 2 & 4 \\ 4 & 2 \end{bmatrix}


Computing eigenvalues:


det(HλI)=(2λ)216=0\det(H - \lambda I) = (2-\lambda)^2 - 16 = 0 λ24λ12=0\lambda^2 - 4\lambda - 12 = 0 λ=6orλ=2\lambda = 6 \quad \text{or} \quad \lambda = -2


Eigenvalues of mixed sign (66 and 2-2) → saddle point.



Quiz: Integrating the Concepts#

Q1. Which differentiation rule does Backpropagation use?

  1. Integration by substitution
  2. Chain Rule
  3. Product Rule only
  4. L'Hôpital's Rule
Show Answer

Answer: 2 — Chain Rule

Backpropagation repeatedly applies the chain rule to differentiate composite functions step by step, propagating gradients from the output layer back toward the input layer.


Q2. What happens if you set the learning rate to 0?

Show Answer

wnew=wold0L=woldw_{\text{new}} = w_{\text{old}} - 0 \cdot \nabla L = w_{\text{old}}


The parameters are never updated. The model stays in its initial state and no learning occurs.

Conversely, a learning rate that is too large causes divergence, while one that is too small results in extremely slow convergence. In practice, a starting range of 10410^{-4} to 10210^{-2} is typical.


Q3. Why does the sigmoid function cause the "vanishing gradient" problem?

Show Answer

Sigmoid derivative: σ(z)=σ(z)(1σ(z))\sigma'(z) = \sigma(z)(1-\sigma(z))

  • When zz is very large or very small, σ(z)1\sigma(z) \to 1 or σ(z)0\sigma(z) \to 0
  • Therefore σ(z)0\sigma'(z) \to 0

During backpropagation, each layer's gradient is multiplied by σ\sigma'. As the network gets deeper:


gradientσ1σ2σn0\text{gradient} \propto \sigma'_1 \cdot \sigma'_2 \cdots \sigma'_n \to 0


Gradients decrease exponentially, leaving earlier layers nearly untrained. ReLU avoids this because σ=1\sigma' = 1 in the positive region.


Q4. Explain why Newton's method converges faster than gradient descent, and why it is rarely used in deep learning.

Show Answer

Why it's faster:

Newton's method uses xnew=xffx_{\text{new}} = x - \frac{f'}{f''}, incorporating second-order derivative (curvature) information. Knowing the curvature at the current position allows the method to estimate how far to jump toward the minimum in a single step, resulting in much faster convergence (quadratic convergence).

Gradient descent uses only first-order derivatives, so it simply moves a little in the direction of the gradient (linear convergence).

Why it's not used in deep learning:

The multivariate Newton's method requires computing the inverse of the Hessian matrix HH:


xnew=xH1f\mathbf{x}_{\text{new}} = \mathbf{x} - H^{-1} \nabla f


With nn parameters, the Hessian is n×nn \times n — for a model like GPT, that's billions × billions.

  • Computational complexity: O(n3)O(n^3) (matrix inversion)
  • Memory: O(n2)O(n^2)

For a model with 1 billion parameters, this is completely infeasible. In practice, methods that approximate the Hessian — such as L-BFGS and Adam — are used instead.

// Related Posts