Skip to content
Medium Research

Stochastic Depth: Inference

A residual connection adds a branch's contribution to an unchanged input. Stochastic depth randomly skips that branch during training. This exercise implements only the deterministic inference rule, with the branch output already supplied.

Your task

Implement stochastic_depth(x, block_output, drop_prob, training).

  • x and block_output are finite floating-point tensors with shape (batch, d) and the same dtype and device.
  • block_output is the residual branch's contribution before adding x. It is not the result of the complete residual block.
  • drop_prob is a scalar in [0, 1]. The survival probability is 1 - drop_prob.
  • training is always False for this exercise. Random training behavior is outside the task; do not draw a random mask during inference.

Return a tensor with the same shape and dtype as x:

output = x + (1 - drop_prob) * block_output

Scale only the branch contribution, not x. Leave both inputs unchanged. Do not apply ReLU or another activation after the addition.

For x=[[1, -2]], block_output=[[8, -4]] and drop_prob=0.25, return [[7, -5]]. With drop_prob=0, return x + block_output. With drop_prob=1, return x, without division by zero.

Which scaling convention is this?

This uses the inference-time survival scaling described by Huang et al. (2016). For fixed x and branch output, if a training gate is 1 with probability 1 - drop_prob and otherwise 0, the expected pre-activation sum is x + (1 - drop_prob) * block_output.

The exercise stops at that sum; the paper's full block also applies ReLU. The expectation statement concerns the sum before an activation, not the output of a whole nonlinear network. Do not substitute inverted-dropout scaling, which uses a different training/evaluation convention.