AI Logic

Regularization

ShakeDrop

Updated:

Three views of a ShakeDrop residual block and the role of the switch b_l. On the left, the training forward pass multiplies the output of a branch made of two convolutions by b_l + α − b_l α and adds it to the input x. In the middle, the training backward pass multiplies the gradient by b_l + β − b_l β, which uses a separate random number, and passes it down. On the right, inference multiplies by the expected value. The table below shows that when b_l = 1 (probability p_l) both passes use a factor of 1, matching the original ResNet; when b_l = 0 the forward pass uses α and the backward pass β, perturbing strongly; and with p_l = 1 − (l / L)(1 − p_L), deeper blocks are more likely to be perturbed

Background and Overview

ShakeDrop is a regularization method that strongly perturbs the output of a residual block's branch by multiplying it by different random numbers in the forward and backward passes, and switches that perturbation on and off probabilistically using the mechanism of Stochastic Depth. Yamada and colleagues at Osaka Prefecture University and Preferred Networks presented it at an ICLR workshop in 2018 and published the full paper in IEEE Access in 2019.

It builds on Shake-Shake, which was known as a strong regularizer. Because Shake-Shake mixes the outputs of two branches in a random ratio, it only works with two-branch architectures such as ResNeXt, and why it works was not clear. The ShakeDrop paper tackles both issues: after interpreting how Shake-Shake works, it reformulates the method so that it also works with single-branch ResNet, Wide ResNet, and PyramidNet.

The key was a way to stabilize training. Applying Shake-Shake's style of perturbation to a single-branch network makes training fail. So the method uses the mechanism of Stochastic Depth (called RandomDrop in the paper), which randomly drops residual blocks, as a switch that decides "perturb or not." On CIFAR-100, it lowered the error rate of PyramidNet-110 from 18.01% to 15.78%.

The Two Methods It Builds On

The ShakeDrop formula combines the formulas of Shake-Shake and Stochastic Depth. Following the equations in Section 2 of the paper, the two are shown side by side. Let x\boldsymbol{x} be the input to a residual block, G(x)G(\boldsymbol{x}) its output, and FF the output of a branch.

Shake-Shake (Equation (2)) mixes two branches F1,F2F_1, F_2 with α\alpha in the forward pass and a separate random number β\beta in the backward pass, and uses the expected value 0.5 at inference (α,β∼U(0,1)\alpha, \beta \sim \mathcal{U}(0, 1)).

G(x)={x+αF1(x)+(1−α)F2(x)training forwardx+βF1(x)+(1−β)F2(x)training backwardx+0.5 F1(x)+0.5 F2(x)inferenceG(\boldsymbol{x}) = \begin{cases} \boldsymbol{x} + \alpha F_1(\boldsymbol{x}) + (1-\alpha) F_2(\boldsymbol{x}) & \text{training forward} \\ \boldsymbol{x} + \beta F_1(\boldsymbol{x}) + (1-\beta) F_2(\boldsymbol{x}) & \text{training backward} \\ \boldsymbol{x} + 0.5\,F_1(\boldsymbol{x}) + 0.5\,F_2(\boldsymbol{x}) & \text{inference} \end{cases}

The next figure shows a Shake-Shake residual block: the training forward pass on the left, the backward pass in the middle, and inference on the right. The coefficients applied to the branches differ between the forward and backward passes.

A Shake-Shake residual block. On the left, the training forward pass multiplies the two branches by α and 1 − α; in the middle, the backward pass splits the gradient with a separate random number, β and 1 − β; on the right, inference multiplies by 0.5

Stochastic Depth (Equations (4) and (5)) keeps the branch of the ll-th residual block with probability plp_l and drops it entirely with probability 1−pl1-p_l. bl∈{0,1}b_l \in \{0, 1\} is a Bernoulli random variable, and the same value is used in the forward and backward passes.

G(x)=x+blF(x),bl∼Bernoulli(pl),pl=1−lL(1−pL)G(\boldsymbol{x}) = \boldsymbol{x} + b_l F(\boldsymbol{x}), \qquad b_l \sim \mathrm{Bernoulli}(p_l), \qquad p_l = 1 - \frac{l}{L}(1 - p_L)

LL is the number of blocks; plp_l is close to 1 for blocks near the input and reaches pLp_L at the last block (linear decay). The paper recommends pL=0.5p_L = 0.5.

Theory (The ShakeDrop Procedure)

The explanation follows Equations (6)–(8) of the paper. The target is a single-branch residual block G(x)=x+F(x)G(\boldsymbol{x}) = \boldsymbol{x} + F(\boldsymbol{x}).

  1. For each block ll, draw a switch blb_l that decides whether to perturb. bl=1b_l = 1 means "do not perturb." bl∼Bernoulli(pl),pl=1−lL(1−pL)b_l \sim \mathrm{Bernoulli}(p_l), \qquad p_l = 1 - \frac{l}{L}(1 - p_L)
  2. Draw the forward coefficient α\alpha and the backward coefficient β\beta from independent uniform distributions. The ranges the paper found best come in two variants, depending on the network's structure.
    • Networks whose branch ends with batch normalization (PyramidNet, etc.): α∈[−1,1]\alpha \in [-1, 1], β∈[0,1]\beta \in [0, 1]
    • The original ResNet, etc.: α=0\alpha = 0, β∈[0,1]\beta \in [0, 1]
  3. In the training forward pass, multiply the branch output by bl+α−blαb_l + \alpha - b_l\alpha. G(x)=x+(bl+α−blα) F(x)G(\boldsymbol{x}) = \boldsymbol{x} + (b_l + \alpha - b_l\alpha)\,F(\boldsymbol{x})
  4. In the training backward pass, multiply the gradient flowing into the branch by bl+β−blβb_l + \beta - b_l\beta (blb_l is the same value as in the forward pass).
  5. At inference, fix the coefficient at its expected value. G(x)=x+E[bl+α−blα] F(x)G(\boldsymbol{x}) = \boldsymbol{x} + \mathrm{E}[b_l + \alpha - b_l\alpha]\,F(\boldsymbol{x})

The coefficients in steps 3 and 4 switch with the value of blb_l as follows (Equations (7) and (8)).

SwitchProbabilityForward coefficientBackward coefficientMeaning
bl=1b_l = 1plp_l11Same as the original network
bl=0b_l = 01−pl1 - p_lα\alphaβ\betaStrongly perturbs the branch computation

Because blb_l and α\alpha are independent, the expected value in step 5 is pl+(1−pl) E[α]p_l + (1-p_l)\,\mathrm{E}[\alpha]. In both ranges above, E[α]=0\mathrm{E}[\alpha] = 0, so the inference coefficient becomes plp_l, the same as Stochastic Depth at inference.

α\alpha and β\beta can be drawn at four granularities: per mini-batch, per image, per channel, or per element. In the paper's experiments (Table 3), per element gave the best results. Setting α=β=0\alpha = \beta = 0 makes ShakeDrop identical to Stochastic Depth.

Why It Works (How ShakeDrop Was Derived)

Starting from an interpretation of Shake-Shake, the paper derives ShakeDrop in three steps.

  1. Interpreting Shake-Shake: Interpolating the two branches with α\alpha in the forward pass amounts to creating plausible augmented data in feature space. Using a separate random number β\beta in the backward pass appears to shake up parameter updates and keep them from getting trapped in local minima.
  2. A single branch breaks it: In feature space, augmented data can be created not only by interpolation but also by adding noise. So the authors tried multiplying the output of a single-branch block by α\alpha and β\beta (Single-branch Shake), and PyramidNet-110 on CIFAR-100 reached an error rate of 77.99%: training failed. In Shake-Shake, the backward gradient becomes β/α\beta/\alpha times its correct value, which grows very large when α\alpha is near 0 or 1. Even so, because the two branches' coefficients are α\alpha and 1−α1-\alpha, when one branch is badly perturbed, the other branch's learning is protected. A single branch has no such safeguard.
  3. Using Stochastic Depth as a switch: Weakening the perturbation also weakens the regularization, so training had to be stabilized while keeping the perturbation strong. The solution was to choose the original network (where learning proceeds correctly) with probability plp_l and the strongly perturbed network (which escapes local minima) with probability 1−pl1-p_l.

As an analogy, it is like practicing normally most of the time and only occasionally practicing on unsteady ground. Practicing only on unsteady ground leads nowhere, but mixed with normal practice it builds a resilience that normal practice alone cannot.

Section 4.3 of the paper also examines what the values of α\alpha and β\beta mean.

  • β=0\beta = 0: The weights of a block chosen for perturbation are not updated in that step.
  • Negative α\alpha: The sign of the branch output flips in the forward pass, and this perturbation propagates to later layers. If the backward coefficient is then positive (β=1\beta = 1), the weights of the perturbed block are updated in the direction opposite to the correct one for the computation actually performed in the forward pass, which tends to break training.
  • The combination α=−1\alpha = -1 and β=0\beta = 0: Strongly perturbs the forward pass while skipping the fragile update, which proved effective.

On PyramidNet, restricting (α,β)(\alpha, \beta) to just the two choices (1,1)(1, 1) and (−1,0)(-1, 0) gave an error rate of 16.24%, nearly the same as drawing from the ranges (16.22%). The paper concludes that random sampling from a range is not itself the main reason the method works.

ShakeDrop is a different kind of regularization from input data augmentation such as Mixup, so the two can be combined. In Table 8 of the paper, PyramidNet-110 on CIFAR-100 went from 17.47% with Mixup alone to 15.04% with ShakeDrop added. For ResNet-110, however, it got slightly worse, from 23.79% to 24.12%.

Experimental Results

CIFAR results from Table 5 of the paper. Values are error rates (%, lower is better). Some CIFAR-100 entries (ResNet-110 and PyramidNet-110) are averages over 4 runs, and the "None" entry for PyramidNet-272 is the value from prior work. ShakeDrop draws random numbers per element, with pL=0.5p_L = 0.5, and training runs for 300 epochs.

NetworkRegularizationCIFAR-10CIFAR-100
ResNet-110None6.5927.42
ResNet-110Stochastic Depth5.5124.07
ResNet-110ShakeDrop4.5623.74
PyramidNet-110 α270None3.8518.01
PyramidNet-110 α270Stochastic Depth3.6317.74
PyramidNet-110 α270ShakeDrop3.3315.78
PyramidNet-272 α200None3.5316.35
PyramidNet-272 α200Stochastic Depth3.4115.94
PyramidNet-272 α200ShakeDrop3.0814.96
ResNeXt-29 2-4-64dNone5.3921.19
ResNeXt-29 2-4-64dShake-Shake4.6419.16
ResNeXt-29 2-4-64dShakeDrop3.9118.27

For ResNeXt-29, ShakeDrop is applied to each of the two branches (Type B in the paper). Even on a two-branch architecture, it achieved lower error rates than Shake-Shake.

On ImageNet, 152-layer networks gave the following results (Table 6, top-1 error rate, pL=0.9p_L = 0.9).

NetworkNoneStochastic DepthShakeDrop
ResNet-15221.7221.3320.88
ResNeXt-15220.4920.4520.34
PyramidNet-152 α30021.5421.2320.94

Furthermore, when a ResNet-152 trained on ImageNet served as the feature extractor for object detection on COCO, the detection average precision (AP) of Mask R-CNN rose from 39.7 without regularization to 40.9 with ShakeDrop (Table 7). This shows that ShakeDrop improves not only classification but also the quality of features used for other tasks.

Practical Considerations

Adjust the ranges of α\alpha and β\beta to the network's structure. With the wrong ranges, training does not converge. In Table 2 of the paper, using α∈[−1,1]\alpha \in [-1, 1], β∈[0,1]\beta \in [0, 1] (the best for PyramidNet) on the original ResNet-110 did not converge, and conversely using α=0\alpha = 0, β∈[0,1]\beta \in [0, 1] (the best for ResNet) on PyramidNet did not converge either. Use the former for networks whose branch ends with batch normalization, and the latter otherwise. Wide ResNet failed to train unless batch normalization was added at the end of the branch (CIFAR-10 error rate 87.6%).

  • Draw random numbers per element: On ResNet-110, drawing per image gave an error rate of 99.00% and per channel 66.30%; training failed. Per element (23.74%) and per mini-batch (23.77%) trained successfully.
  • Use a larger pLp_L for shallow networks: As with Stochastic Depth, ShakeDrop tends to help more in deeper networks, and shallow networks did better with a larger pLp_L (Table 9). On a 28-layer Wide ResNet, it did not beat Stochastic Depth. The ImageNet experiments use pL=0.9p_L = 0.9.
  • Do not combine with Shake-Shake: The paper notes that regularizers of the same kind (those that create new data in feature space from a single example), such as ShakeDrop and Shake-Shake, may not work together. They can be combined with regularizers of a different kind, such as Mixup.
  • Multiply by the expected value at inference: In PyTorch, implement it so that model.eval() switches the behavior (see the code example below). With the ranges above, the inference coefficient is plp_l.

Comparison with Other Methods

The table compares regularizers that apply random numbers to the branches of residual blocks, from the same viewpoints.

MethodValue applied to the branchForward vs. backward random numbersApplicable architecturesValue applied at inference
Stochastic Depthblb_l (0 or 1)SameResidual networksplp_l
Shake-Shakeα\alpha and 1−α1-\alpha (two branches)Different (β\beta)Two or more branches0.5
ShakeDropbl+α−blαb_l + \alpha - b_l\alphaDifferent (β\beta); blb_l is sharedResNet, PyramidNet, ResNeXt, etc.E[bl+α−blα]\mathrm{E}[b_l + \alpha - b_l\alpha]
Dropout0 or 1 (per element)SameAny layerScaling to match the expected value
  • Choose ShakeDrop for deep single-branch residual networks (ResNet, PyramidNet) when stronger regularization than Stochastic Depth is needed.
  • Even on two-branch architectures, ShakeDrop often achieved lower error rates than Shake-Shake in the paper.
  • ShakeDrop is sensitive to three settings: α\alpha, β\beta, and pLp_L. Without room to explore them, starting with Stochastic Depth, whose only setting is pLp_L, is the safe choice.

PyTorch example

Show code
python
import torch
import torch.nn as nn


def uniform_like(t, low, high):
    # When low == high (e.g., α = 0), fill with that value
    return torch.empty_like(t).uniform_(low, high) if low < high else torch.full_like(t, low)


class ShakeDropFunction(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x, p_l, alpha_range, beta_range):
        b = torch.bernoulli(torch.tensor(p_l)).item()   # b_l: 1 means no perturbation (probability p_l)
        ctx.b, ctx.beta_range = b, beta_range
        if b == 1:
            return x
        alpha = uniform_like(x, *alpha_range)           # per-element α (the best granularity in the paper)
        return alpha * x

    @staticmethod
    def backward(ctx, grad_output):
        if ctx.b == 1:
            return grad_output, None, None, None
        beta = uniform_like(grad_output, *ctx.beta_range)  # β drawn separately from α
        return beta * grad_output, None, None, None


class ShakeDrop(nn.Module):
    """Multiply the branch output by (b_l + α − b_l α); the backward pass uses (b_l + β − b_l β)"""

    def __init__(self, p_l, alpha_range=(-1.0, 1.0), beta_range=(0.0, 1.0)):
        super().__init__()
        self.p_l, self.alpha_range, self.beta_range = p_l, alpha_range, beta_range

    def forward(self, x):
        if self.training:
            return ShakeDropFunction.apply(x, self.p_l, self.alpha_range, self.beta_range)
        # Inference: E[b_l + α − b_l α] = p_l + (1 − p_l) E[α] (with the ranges above, E[α] = 0, giving p_l)
        mean_alpha = sum(self.alpha_range) / 2
        return (self.p_l + (1 - self.p_l) * mean_alpha) * x


class PyramidStyleBlock(nn.Module):
    """A residual block whose branch ends with BN (PyramidNet style). Uses α ∈ [−1, 1], β ∈ [0, 1]"""

    def __init__(self, channels, p_l):
        super().__init__()
        self.branch = nn.Sequential(
            nn.BatchNorm2d(channels),
            nn.Conv2d(channels, channels, 3, padding=1, bias=False),
            nn.BatchNorm2d(channels),
            nn.ReLU(),
            nn.Conv2d(channels, channels, 3, padding=1, bias=False),
            nn.BatchNorm2d(channels),
        )
        self.shake_drop = ShakeDrop(p_l, alpha_range=(-1.0, 1.0), beta_range=(0.0, 1.0))

    def forward(self, x):
        return x + self.shake_drop(self.branch(x))


# p_l decays linearly: p_l = 1 − (l / L)(1 − p_L), p_L = 0.5
L, p_L = 4, 0.5
model = nn.Sequential(*[PyramidStyleBlock(32, 1 - (l / L) * (1 - p_L)) for l in range(1, L + 1)])

x = torch.randn(8, 32, 16, 16)
model.train()                 # training: draw b_l, α, and β every time
model(x).sum().backward()

model.eval()                  # inference: multiply the branch output by p_l (same output every time)
with torch.no_grad():
    print(torch.allclose(model(x), model(x)))  # True

# If the branch does not end with BN, as in the original ResNet, use α = 0, β ∈ [0, 1]
resnet_style = ShakeDrop(p_l=0.5, alpha_range=(0.0, 0.0), beta_range=(0.0, 1.0))

FAQ

Q. How should p_L be chosen?
A.

Following Stochastic Depth, the paper uses p_L = 0.5 as the default, with 0.5 for CIFAR and 0.9 for ImageNet. p_L is the probability that the last block is not perturbed, so smaller values mean stronger regularization. Shallow networks did better with a larger (weaker) p_L. Choose a value in the range 0.5–0.9 while watching the validation error.

Q. Which ranges of α and β should I use?
A.

For networks whose branch ends with batch normalization (PyramidNet, the EraseReLU variant of ResNet with the ReLU removed, etc.), use α ∈ [−1, 1], β ∈ [0, 1]. For networks with a ReLU after the branch, like the original ResNet, use α = 0, β ∈ [0, 1]. In the paper, swapping the two made training fail to converge.

Q. What does it improve over Shake-Shake?
A.

Mainly two things. First, it works with single-branch ResNet, Wide ResNet, and PyramidNet. Second, even on two-branch ResNeXt, where Shake-Shake applies, it lowered the error rate in many cases. For example, on CIFAR-100 with ResNeXt-29 2-4-64d, ShakeDrop scored 18.27% versus 19.16% for Shake-Shake.

References

  1. Y. Yamada, M. Iwamura, T. Akiba, and K. Kise, "ShakeDrop Regularization for Deep Residual Learning," IEEE Access, 2019.
  2. Y. Yamada, M. Iwamura, T. Akiba, and K. Kise, "ShakeDrop" (the paper's implementation, GitHub).
  3. X. Gastaldi, "Shake-Shake regularization," arXiv:1705.07485, 2017.
  4. G. Huang, Y. Sun, Z. Liu, D. Sedra, and K. Q. Weinberger, "Deep Networks with Stochastic Depth," ECCV, 2016.
  5. D. Han, J. Kim, and J. Kim, "Deep Pyramidal Residual Networks," CVPR, 2017.