Measure the vanishing gradient through a chain of sigmoid layers
Why it matters This is why deep networks moved from sigmoid to ReLU and why LSTMs add an additive cell path.
Stacking sigmoids makes early layers learn very slowly. You can see why by computing the gradient layer by layer.
Write sigmoid_chain_grads(x, weights). It is a scalar chain: a_0 = x and a_l = sigmoid(w_l * a_{l-1}) for each weight w_l in the list. Return a float array g of length L + 1 where g[l] = d a_L / d a_l, so g[L] = 1. Use d a_l / d a_{l-1} = w_l * s_l * (1 - s_l) with s_l = a_l.
def sigmoid_chain_grads(x, weights)Example
sigmoid_chain_grads(1.0, [1.0, 1.0]) is about [0.0431, 0.2194, 1.0].
Understand the idea
The gradient that reaches an early layer is a product of one local slope per layer. The sigmoid's slope is at most 0.25, so with ordinary weights, ten layers can multiply the gradient by something like 0.25^10, about one in a million. It is like whispering a message through a long line where each person speaks four times quieter.
Tiny example: x = 1, w = 1 twice. a1 = sigmoid(1) = 0.731, a2 = sigmoid(0.731) = 0.675. d a2 / d a1 = 0.675 * 0.325 = 0.219. d a1 / d x = 0.731 * 0.269 = 0.197. So d a2 / d x = 0.219 * 0.197 = 0.041.
Rule: g[l - 1] = g[l] * w_l * a_l * (1 - a_l), with g[L] = 1.
ReLU, residual connections and careful init were all adopted to stop this shrinking in deep CNNs and transformers.