Q. How much time does it take to compute the Hessian of
a network?
Assume a computational graph with $n$ nodes and parameters also scale $O(n)$.
Forward pass: $O(n)$
Backward pass (first derivatives): $O(n)$
Hessian $H = \nabla^2 L$ is an $n \times n$ matrix — $O(n^2)$ entries
Forward pass
Evaluate $F(x)$ once along the graph — $O(n)$.
Backward pass
Same graph, reverse sweep — yields all first derivatives
$\partial F/\partial z_i$ in $O(n)$.
“Backward–backward”
Run a forward pass on the backward graph — for each node of
interest — to get mixed derivatives such as
$\partial^2 F/(\partial z_1\,\partial x)$.
Over $N$ data samples the full Hessian costs $O(N n^2)$ — already too
large for modern models.
Takeaway
Storing and forming the dense Hessian is $O(n^2)$ (and $O(N n^2)$ with
data). Exact OBS-style methods that need the full $H$ (or $H^{-1}$)
cannot scale naively.
2. Hessian diagonal.
Q. What if we only care about
$\partial^2 L / \partial w_i^2$ (the diagonal of $H$)?
This is exactly what Optimal Brain Damage (OBD) needs:
a diagonal Hessian approximation.
$\vec{x}$ and $\vec{w}$ feed $f$; output $\vec{z}=f(\vec{x};\vec{w})$ then
enters the loss $L(\vec{z})$.
Diagonal of a weight $w_{ij}$
For a scalar edge $x_i \xrightarrow{w_{ij}} z_j$ with local map $f$,
First term — global curvature pushed through the local
Jacobian $J_f$
$\partial L/\partial z_i$ — global gradient;
$\nabla^2_x z_i$ — local curvature
We only need local Hessians to be computed along the graph
Dimensions: $\nabla^2_x L$ is $d\times d$, $J_f$ is $d'\times d$,
$\nabla^2_z L$ is $d'\times d'$.
Complexity
All diagonal entries of the Hessian over the parameters can be obtained
in a single “non-traditional” backward pass — cost
$O(n)$, same order as a gradient.
$\Rightarrow$ OBD is feasible: one forward + one specialized backward.
3. Approximation of the Hessian for MSE loss
Specialize to squared error. Let $o = F(\vec{w},\vec{x})$ and target
$t$, with
$X_i = \nabla_w F(\vec{w},\vec{x}_i)$ is $n\times 1$; each outer
product is $n\times n$
$H$ is the (scaled) sample covariance of the output gradients
Cost to form this approximate Hessian:
$O(N n^2)$
Note: this holds for MSE; exercise — what changes
for cross-entropy?
4. Hessian inverse for MSE loss
A dense inverse from scratch is generally
$O(n^3)$. The MSE approximation
$H = \tfrac{1}{N}\sum_i X_i X_i^\top$ is a sum of rank-1 matrices, so we
can maintain $H^{-1}$ with rank-1 updates.
Sherman–Morrison (rank-1 update)
For invertible $A$ and vectors $u,v$ with
$1 + v^\top A^{-1}u \neq 0$:
Complexity
Updating $H^{-1}$ over $N$ samples costs
$O(N n^2)$ (one outer-product-scale update per
sample), avoiding a separate $O(n^3)$ factorization. Contrast with
forming $H$ then inverting from scratch:
$O(N n^2 + n^3)$.