Apply drop path (stochastic depth) to a residual branch
Why it matters Drop path is in the training recipe of most modern ViT and ConvNeXt models.
Very deep vision transformers randomly skip whole residual branches during training. This is drop path, also called stochastic depth.
Write drop_path(x, branch, keep, p, training). x and branch are (n, d) arrays. keep is an (n,) boolean array: True means that sample keeps its branch on this step. If training, return x + branch * keep[:, None] / (1 - p). If not training, return x + branch (keep is ignored). p is the drop probability, 0 <= p < 1.
def drop_path(x, branch, keep, p, training)Example
x = [[1, 1], [2, 2]], branch = [[1, 2], [3, 4]], keep = [True, False], p = 0.5, training = True gives [[3, 5], [2, 2]].
Understand the idea
In a residual block the output is x + f(x). Drop path randomly turns off f for some samples during training, so the network behaves like a shallower one for them. It trains many depths at once and makes each block less dependent on the others. Think of a team where random members sit out practice so nobody becomes irreplaceable.
Tiny example: x = [1, 1], branch = [1, 2], p = 0.5. If kept, output = [1, 1] + [1, 2] / 0.5 = [3, 5]. If dropped, output = [1, 1]. On average that is [2, 3], exactly x + branch, which is the eval output.
Rule: train: y = x + branch * keep / (1 - p); eval: y = x + branch.
Deep image models such as ViT-B and ConvNeXt use drop path rates that grow with depth.
Shapes and data flow
x, branch (n, d); keep (n,) -> (n, 1) to broadcast across features