homeproject
blog
search..K
search..K

Navigation

Home
Projects
Writings

Connect

Email
GitHub
Twitter / X
LinkedIn

Latest Writing

04 Articles
01/Deep Learning Foundations: Gradient Descent, Multilayer Backpropagation Calculus & Loss Optimization
02/How Neural Networks Learn: Activation Functions, Weight Initialization & Optimization Dynamics
03/Mermaid Architectural Diagram Studio: Full Design & Color Stress Test
04/Calculus & Geometry: 2D Function Analysis, Tangent Slopes & Interactive Curve Plotting

© 2026 Ayush Kumar.•All rights reserved.

Sitemap•

Built with Next.js & Tailwind

Deep Learning Foundations: Gradient Descent, Multilayer Backpropagation Calculus & Loss Optimization
Home/Writings/Deep Learning & Neural Networks

Deep Learning Foundations: Gradient Descent, Multilayer Backpropagation Calculus & Loss Optimization

Machine LearningNeural NetworksCalculusGradient DescentAIMathematics

Artificial Neural Networks (ANNs) form the computational backbone of contemporary artificial intelligence, powering everything from computer vision models to large language transformers. At its core, training a neural network is a high-dimensional mathematical optimization problem: finding a point in weight space W∈Rd\mathbf{W} \in \mathbb{R}^dW∈Rd that minimizes an empirical loss function L(W)\mathcal{L}(\mathbf{W})L(W).

In this exhaustive analytical guide, we explore the complete mathematical engine driving deep learning: vectorized forward propagation, loss surface geometry, multivariate vector calculus, complete multilayer backpropagation step-by-step derivations, and modern adaptive gradient descent algorithms.

Series·Deep Learning & Neural Networks
Chapter 1 of 2
1Deep Learning Foundations: Gradient Descent, Multilayer Backpropagation Calculus & Loss Optimization
Current
2How Neural Networks Learn: Activation Functions, Weight Initialization & Optimization Dynamics
Read →
First chapter in series
Next
How Neural Networks Learn: Activation Functions, Weight Initialization & Optimization Dynamics

1. Mathematical Foundations & Vectorized Forward Pass

An artificial neural network consists of interconnected layers of artificial neurons. Let LLL denote the total number of layers in the network, where layer l=0l = 0l=0 is the input layer and layer l=Ll = Ll=L is the output layer.

l∈{0,1,2,…,L}l \in \{0, 1, 2, \dots, L\}l∈{0,1,2,…,L}

Vectorized Layer Transformation

For any hidden layer lll, the linear sum vector z(l)∈Rnl\mathbf{z}^{(l)} \in \mathbb{R}^{n_l}z(l)∈Rnl​ is calculated by multiplying the weight matrix W(l)∈Rnl×nl−1\mathbf{W}^{(l)} \in \mathbb{R}^{n_l \times n_{l-1}}W(l)∈Rnl​×nl−1​ with the activation vector from the previous layer a(l−1)∈Rnl−1\mathbf{a}^{(l-1)} \in \mathbb{R}^{n_{l-1}}a(l−1)∈Rnl−1​, plus the bias vector b(l)∈Rnl\mathbf{b}^{(l)} \in \mathbb{R}^{n_l}b(l)∈Rnl​:

z(l)=W(l)a(l−1)+b(l)\mathbf{z}^{(l)} = \mathbf{W}^{(l)} \mathbf{a}^{(l-1)} + \mathbf{b}^{(l)}z(l)=W(l)a(l−1)+b(l)

The activation vector a(l)\mathbf{a}^{(l)}a(l) is subsequently produced by applying an element-wise non-linear activation function σ(⋅)\sigma(\cdot)σ(⋅):

a(l)=σ(z(l))\mathbf{a}^{(l)} = \sigma\left(\mathbf{z}^{(l)}\right)a(l)=σ(z(l))

where a(0)=x(the input feature vector)\text{where } \mathbf{a}^{(0)} = \mathbf{x} \quad (\text{the input feature vector})where a(0)=x(the input feature vector)

Rendering Mermaid Architecture Diagram...

Activation Functions & Nonlinearity

Without non-linear activation functions, a deep neural network—regardless of its depth—collapses into a single linear matrix transformation:

y=W(L)W(L−1)…W(1)x=Weffectivex\mathbf{y} = \mathbf{W}^{(L)} \mathbf{W}^{(L-1)} \dots \mathbf{W}^{(1)} \mathbf{x} = \mathbf{W}_{\text{effective}} \mathbf{x}y=W(L)W(L−1)…W(1)x=Weffective​x

Non-linear activation functions allow neural networks to approximate arbitrary continuous functions (The Universal Approximation Theorem).

1. Sigmoid Activation Function

σ(z)=11+e−z,σ′(z)=σ(z)(1−σ(z))\sigma(z) = \frac{1}{1 + e^{-z}}, \quad \sigma'(z) = \sigma(z)\left(1 - \sigma(z)\right)σ(z)=1+e−z1​,σ′(z)=σ(z)(1−σ(z))

2. Rectified Linear Unit (ReLU)

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

3. Hyperbolic Tangent (Tanh)

tanh⁡(z)=ez−e−zez+e−z,tanh⁡′(z)=1−tanh⁡2(z)\tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}}, \quad \tanh'(z) = 1 - \tanh^2(z)tanh(z)=ez+e−zez−e−z​,tanh′(z)=1−tanh2(z)

Activation Functions Comparison Engine: Sigmoid, ReLU & Tanh
y = 1 / (1 + exp(-x))y = Math.max(0y = x)y = Math.tanh(x)
Curves:
-6-4-2246-4-224
Hover cursor over graph to inspect coordinates

2. Loss Landscapes & Optimization Objectives

To train a neural network, we quantify the discrepancy between network predictions y^=a(L)\mathbf{\hat{y}} = \mathbf{a}^{(L)}y^​=a(L) and true target labels y\mathbf{y}y using a scalar loss function L\mathcal{L}L.

Mean Squared Error (MSE) Loss

Used predominantly in regression tasks:

LMSE(y,y^)=12∥y−y^∥22=12∑k=1K(yk−y^k)2\mathcal{L}_{\text{MSE}}(\mathbf{y}, \mathbf{\hat{y}}) = \frac{1}{2} \|\mathbf{y} - \mathbf{\hat{y}}\|_2^2 = \frac{1}{2} \sum_{k=1}^{K} (y_k - \hat{y}_k)^2LMSE​(y,y^​)=21​∥y−y^​∥22​=21​∑k=1K​(yk​−y^​k​)2

Binary Cross-Entropy (BCE) Loss

Used in binary classification tasks:

LBCE(y,y^)=−[ylog⁡(y^)+(1−y)log⁡(1−y^)]\mathcal{L}_{\text{BCE}}(y, \hat{y}) = - \Big[ y \log(\hat{y}) + (1 - y) \log(1 - \hat{y}) \Big]LBCE​(y,y^​)=−[ylog(y^​)+(1−y)log(1−y^​)]

The Loss Surface Manifold

The loss function L(W)\mathcal{L}(\mathbf{W})L(W) defines a scalar field over high-dimensional weight space. For simple quadratic loss functions, the loss surface is convex (a 3D bowl shape), guaranteeing a global minimum:

L(w1,w2)=w12+w22\mathcal{L}(w_1, w_2) = w_1^2 + w_2^2L(w1​,w2​)=w12​+w22​

Convex Loss Landscape: Quadratic Error Bowl
z = x^2 + y^2
3D Surfaces:
Drag to rotate • Scroll to zoom
Hover surface to inspect (x, y, z)

In deep multi-layer networks, however, non-linear activations render the loss surface non-convex, featuring local minima, high-dimensional saddle points, and flat plateaus:

L(w1,w2)=w12−w22+0.5⋅sin⁡(3w1)cos⁡(3w2)\mathcal{L}(w_1, w_2) = w_1^2 - w_2^2 + 0.5 \cdot \sin(3 w_1) \cos(3 w_2)L(w1​,w2​)=w12​−w22​+0.5⋅sin(3w1​)cos(3w2​)

Non-Convex Deep Loss Surface: Saddle Points & Local Minima
z = x^2 - y^2
3D Surfaces:
Drag to rotate • Scroll to zoom
Hover surface to inspect (x, y, z)

3. Multivariable Vector Calculus & Gradient Descent

The gradient vector ∇WL\nabla_{\mathbf{W}} \mathcal{L}∇W​L collects all partial derivatives of the scalar loss L\mathcal{L}L with respect to every weight parameter:

∇WL=[∂L∂w1,∂L∂w2,…,∂L∂wd]T\nabla_{\mathbf{W}} \mathcal{L} = \left[ \frac{\partial \mathcal{L}}{\partial w_1}, \frac{\partial \mathcal{L}}{\partial w_2}, \dots, \frac{\partial \mathcal{L}}{\partial w_d} \right]^T∇W​L=[∂w1​∂L​,∂w2​∂L​,…,∂wd​∂L​]T

The gradient vector ∇WL\nabla_{\mathbf{W}} \mathcal{L}∇W​L points in the direction of maximum rate of increase of the loss function. Consequently, to minimize the loss, we step in the opposite direction of the gradient:

ΔW=−η∇WL\Delta \mathbf{W} = - \eta \nabla_{\mathbf{W}} \mathcal{L}ΔW=−η∇W​L

where η>0\eta > 0η>0 represents the learning rate hyperparameter.

Gradient Descent Iterative Update Rule

W(t+1)=W(t)−η∇WL(W(t))\mathbf{W}^{(t+1)} = \mathbf{W}^{(t)} - \eta \nabla_{\mathbf{W}} \mathcal{L}\left(\mathbf{W}^{(t)}\right)W(t+1)=W(t)−η∇W​L(W(t))

b(t+1)=b(t)−η∇bL(b(t))\mathbf{b}^{(t+1)} = \mathbf{b}^{(t)} - \eta \nabla_{\mathbf{b}} \mathcal{L}\left(\mathbf{b}^{(t)}\right)b(t+1)=b(t)−η∇b​L(b(t))


4. Multilayer Backpropagation Algorithm: Step-by-Step Calculus Derivation

Backpropagation (backward propagation of errors) is an efficient application of the multivariable Chain Rule of Calculus used to evaluate gradients ∂L∂W(l)\frac{\partial \mathcal{L}}{\partial \mathbf{W}^{(l)}}∂W(l)∂L​ and ∂L∂b(l)\frac{\partial \mathcal{L}}{\partial \mathbf{b}^{(l)}}∂b(l)∂L​ across all layers l∈{1,…,L}l \in \{1, \dots, L\}l∈{1,…,L}.

Let us define the error delta vector δ(l)∈Rnl\delta^{(l)} \in \mathbb{R}^{n_l}δ(l)∈Rnl​ for layer lll as the partial derivative of loss with respect to pre-activation linear sums z(l)\mathbf{z}^{(l)}z(l):

δ(l)≡∂L∂z(l)\delta^{(l)} \equiv \frac{\partial \mathcal{L}}{\partial \mathbf{z}^{(l)}}δ(l)≡∂z(l)∂L​

Complete Multilayer Backpropagation Calculus Derivation

Derivation Pad: Layer-by-Layer Matrix Vector Calculus

Notebook Pad
Compute Output Layer Error Delta δ⁽ᴸ⁾[Chain Rule Output Delta]

δ(L)=∂L∂z(L)=(∂L∂a(L))⊙σ′(z(L))\delta^{(L)} = \frac{\partial \mathcal{L}}{\partial \mathbf{z}^{(L)}} = \left( \frac{\partial \mathcal{L}}{\partial \mathbf{a}^{(L)}} \right) \odot \sigma'\left(\mathbf{z}^{(L)}\right)δ(L)=∂z(L)∂L​=(∂a(L)∂L​)⊙σ′(z(L))

Backward Propagation of Error Delta δ⁽ˡ⁾ to Hidden Layers[Matrix Backprop]

δ(l)=∂L∂z(l)=((W(l+1))Tδ(l+1))⊙σ′(z(l))\delta^{(l)} = \frac{\partial \mathcal{L}}{\partial \mathbf{z}^{(l)}} = \left( \left(\mathbf{W}^{(l+1)}\right)^T \delta^{(l+1)} \right) \odot \sigma'\left(\mathbf{z}^{(l)}\right)δ(l)=∂z(l)∂L​=((W(l+1))Tδ(l+1))⊙σ′(z(l))

Compute Partial Derivatives w.r.t Weight Matrix W⁽ˡ⁾[Outer Product Weight Gradient]

∂L∂W(l)=δ(l)(a(l−1))T\frac{\partial \mathcal{L}}{\partial \mathbf{W}^{(l)}} = \delta^{(l)} \left(\mathbf{a}^{(l-1)}\right)^T∂W(l)∂L​=δ(l)(a(l−1))T

Compute Partial Derivatives w.r.t Bias Vector b⁽ˡ⁾[Bias Derivative]

∂L∂b(l)=δ(l)\frac{\partial \mathcal{L}}{\partial \mathbf{b}^{(l)}} = \delta^{(l)}∂b(l)∂L​=δ(l)

Execute Gradient Descent Parameter Updates[Simultaneous Matrix Update]

W(l)←W(l)−η⋅δ(l)(a(l−1))T,b(l)←b(l)−η⋅δ(l)\mathbf{W}^{(l)} \leftarrow \mathbf{W}^{(l)} - \eta \cdot \delta^{(l)} \left(\mathbf{a}^{(l-1)}\right)^T, \quad \mathbf{b}^{(l)} \leftarrow \mathbf{b}^{(l)} - \eta \cdot \delta^{(l)}W(l)←W(l)−η⋅δ(l)(a(l−1))T,b(l)←b(l)−η⋅δ(l)


5. Advanced Optimization Algorithms

Plain Stochastic Gradient Descent (SGD) can oscillate in ravines or stall at saddle points. Modern deep learning relies on adaptive momentum-based optimization algorithms.

1. SGD with Momentum

Accumulates an exponentially decaying moving average of past gradients to accelerate along persistent directions:

vt=βvt−1+(1−β)gt\mathbf{v}_t = \beta \mathbf{v}_{t-1} + (1 - \beta) \mathbf{g}_tvt​=βvt−1​+(1−β)gt​

Wt+1=Wt−ηvt\mathbf{W}_{t+1} = \mathbf{W}_t - \eta \mathbf{v}_tWt+1​=Wt​−ηvt​

2. Adam (Adaptive Moment Estimation)

Combines momentum (first moment mtm_tmt​) and RMSprop (second uncentered moment vtv_tvt​) with bias correction:

mt=β1mt−1+(1−β1)gt,vt=β2vt−1+(1−β2)gt2m_t = \beta_1 m_{t-1} + (1 - \beta_1) g_t, \quad v_t = \beta_2 v_{t-1} + (1 - \beta_2) g_t^2mt​=β1​mt−1​+(1−β1​)gt​,vt​=β2​vt−1​+(1−β2​)gt2​

m^t=mt1−β1t,v^t=vt1−β2t\hat{m}_t = \frac{m_t}{1 - \beta_1^t}, \quad \hat{v}_t = \frac{v_t}{1 - \beta_2^t}m^t​=1−β1t​mt​​,v^t​=1−β2t​vt​​

Wt+1=Wt−ηv^t+ϵm^t\mathbf{W}_{t+1} = \mathbf{W}_t - \frac{\eta}{\sqrt{\hat{v}_t} + \epsilon} \hat{m}_tWt+1​=Wt​−v^t​​+ϵη​m^t​


6. Interactive Deep Learning Engine & Real-Time Activation Explorer

Below is an interactive deep neural network engine. You can tweak layer activation functions, adjust learning rate η\etaη, execute single backpropagation steps, or run auto-training loops to observe real-time loss convergence L(W)L(\mathbf{W})L(W):

Interactive Deep Neural Network & Real-Time Backpropagation Engine

Architecture: 3 → 4 → 3 → 1 | Epoch: 0 | Loss: 0.10733

η:0.1
0.80-0.500.400.570.540.450.470.320.780.240.49
Real-Time Loss Convergence
Press Step or Train to record loss trajectory
Neuron Inspector

Click any neuron node in the graph to inspect exact linear sum z, activation a, and backprop delta δ.


Summary & Architectural Insights

  1. Forward Propagation: Computes linear transformations z(l)=W(l)a(l−1)+b(l)\mathbf{z}^{(l)} = \mathbf{W}^{(l)} \mathbf{a}^{(l-1)} + \mathbf{b}^{(l)}z(l)=W(l)a(l−1)+b(l) followed by non-linear activations a(l)=σ(z(l))\mathbf{a}^{(l)} = \sigma(\mathbf{z}^{(l)})a(l)=σ(z(l)).
  2. Loss Surfaces: Non-convex loss manifolds in high dimensions contain saddle points and local minima, requiring momentum and adaptive learning rates.
  3. Backpropagation: Calculates error deltas δ(l)=((W(l+1))Tδ(l+1))⊙σ′(z(l))\delta^{(l)} = ((\mathbf{W}^{(l+1)})^T \delta^{(l+1)}) \odot \sigma'(\mathbf{z}^{(l)})δ(l)=((W(l+1))Tδ(l+1))⊙σ′(z(l)) from output to input via multivariable chain rule.
  4. Gradient Updates: Weights and biases adapt proportionally to η⋅δ(l)(a(l−1))T\eta \cdot \delta^{(l)} (\mathbf{a}^{(l-1)})^Tη⋅δ(l)(a(l−1))T.
First chapter in series
Next Chapter
How Neural Networks Learn: Activation Functions, Weight Initialization & Optimization Dynamics
Recommended Reading

Hand-picked related technical articles

How Neural Networks Learn: Activation Functions, Weight Initialization & Optimization Dynamics
Machine LearningNeural Networks
How Neural Networks Learn: Activation Functions, Weight Initialization & Optimization Dynamics

An in-depth, mathematical and interactive guide into how neural networks learn—exploring activation non-linearities (Sigmoid, ReLU, Tanh), Xavier/He weight initialization, learning rate schedules, and optimization dynamics.

Aug 16, 2026
Read Article
Advanced Calculus: Rigorous Integration Theory, Special Forms & Numerical Algorithms
MathematicsCalculus
Advanced Calculus: Rigorous Integration Theory, Special Forms & Numerical Algorithms

A comprehensive mathematical exploration of integral calculus—covering Riemann sums, the Fundamental Theorem, Gaussian Integrals, contour Integration by Parts, and numerical quadratures.

Aug 2, 2026
Read Article
Multivariable Calculus: 3D Quadric Surfaces, Implicit Equations & Interactive WebGL Geometry
3DGeometry
Multivariable Calculus: 3D Quadric Surfaces, Implicit Equations & Interactive WebGL Geometry

An architectural and mathematical deep dive into 3D implicit surfaces—exploring spheres, paraboloids, hyperboloids, and tori using interactive Three.js WebGL visualizations.

Aug 12, 2026
Read Article
Calculus & Geometry: 2D Function Analysis, Tangent Slopes & Interactive Curve Plotting
GeometryCalculus
Calculus & Geometry: 2D Function Analysis, Tangent Slopes & Interactive Curve Plotting

A mathematical deep dive into 2D explicit and implicit curves—exploring polynomial roots, trigonometric waves, derivative tangent slopes, and real-time interactive 2D graph visualization.

Aug 14, 2026
Read Article