DL Notes
Essential calculus concepts for deep learning including derivatives, chain rule, partial derivatives, and gradient computation.
Calculus is how neural networks learn. The entire training process — backpropagation — is an application of the chain rule from calculus. Understanding derivatives and gradients is essential.
Why Calculus in Deep Learning?
Training a neural network means finding weights that minimize a loss function. Calculus tells us which direction to adjust weights (gradients) and by how much.
Derivatives: The Foundation
A derivative measures how a function changes as its input changes — the rate of change or slope.
import numpy as np
# Numerical derivative
def numerical_derivative(f, x, h=1e-7):
"""Approximate derivative using finite differences"""
return (f(x + h) - f(x - h)) / (2 * h)
# Example: derivative of f(x) = x^2 is f'(x) = 2x
f = lambda x: x**2
print(numerical_derivative(f, 3)) # ~6.0 (exactly 2*3)
# Derivative of sigmoid
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def sigmoid_derivative(x):
s = sigmoid(x)
return s * (1 - s) # Analytical derivative
x = np.linspace(-5, 5, 100)
print(f"sigmoid(0) = {sigmoid(0)}") # 0.5
print(f"sigmoid'(0) = {sigmoid_derivative(0)}") # 0.25The Chain Rule: Heart of Backpropagation
The chain rule computes derivatives of composite functions — exactly what neural networks are.
# Neural network as composite function:
# output = sigmoid(W2 @ relu(W1 @ x + b1) + b2)
#
# Chain rule unrolls this:
# d(loss)/d(W1) = d(loss)/d(output) * d(output)/d(hidden) * d(hidden)/d(W1)
# Concrete example
def forward_pass(x, w1, w2):
z1 = w1 * x # Linear layer 1
a1 = max(0, z1) # ReLU activation
z2 = w2 * a1 # Linear layer 2
return z2
# Backward pass using chain rule
def backward_pass(x, w1, w2, target):
# Forward
z1 = w1 * x
a1 = max(0, z1)
z2 = w2 * a1
loss = (z2 - target)**2
# Backward (chain rule)
dloss_dz2 = 2 * (z2 - target) # d(loss)/d(z2)
dz2_da1 = w2 # d(z2)/d(a1)
da1_dz1 = 1 if z1 > 0 else 0 # d(ReLU)/d(z1)
dz1_dw1 = x # d(z1)/d(w1)
# Chain them together
dloss_dw1 = dloss_dz2 * dz2_da1 * da1_dz1 * dz1_dw1
dloss_dw2 = dloss_dz2 * a1
return dloss_dw1, dloss_dw2Partial Derivatives and Gradients
With multiple variables (weights), we need partial derivatives — the gradient vector.
Key Derivatives for Deep Learning
| Function | Derivative | Used In |
|---|---|---|
| x^n | n*x^(n-1) | Polynomial networks |
| sigmoid(x) | sigmoid(x)*(1-sigmoid(x)) | Binary classification |
| tanh(x) | 1 - tanh(x)^2 | RNN activations |
| ReLU(x) | 1 if x>0, else 0 | Most hidden layers |
| softmax | Complex (Jacobian) | Multi-class output |
| log(x) | 1/x | Cross-entropy loss |
Interview Questions
- Why is the chain rule essential for training neural networks?
Neural networks are compositions of functions. The chain rule lets us compute how the loss changes with respect to any weight, no matter how deep the network.
- What is a gradient and how is it used in training?
A gradient is the vector of partial derivatives pointing toward steepest ascent. We subtract it from weights to descend toward the minimum loss.
- Why does the sigmoid derivative cause vanishing gradients?
Sigmoid derivative peaks at 0.25 and approaches 0 for large inputs. Multiplying many such small values in deep chains makes gradients vanish.
- Explain the difference between a derivative and a partial derivative.
A derivative is for single-variable functions. A partial derivative holds all other variables constant and differentiates with respect to one variable only.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Calculus for Deep Learning.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Deep Learning topic.
Search Terms
deep-learning, deep learning, deep, learning, prerequisites, calculus, for, calculus for deep learning
Related Deep Learning Topics