The problem
Dropout randomly switches off parts of a network during training, so that it cannot rely on any single number and has to spread what it learns. Each activation is set to 0 with probability . The survivors are multiplied by , so that the average value of every activation stays the same ("inverted" dropout). When the model is used for real (evaluation), the layer does nothing.
Forward
where:
- is the probability of dropping an entry;
- is the mask: for each entry, 1 (keep) with probability , and 0 (drop) otherwise;
- is elementwise multiplication, and dividing by makes up for the dropped entries.
To build the mask, draw one random number between 0 and 1 for every entry, u = rng.random(x.shape), and keep the entries where u >= p: that happens with probability .
Example. ,
x = [1, 1, 1, 1], and the random numbers come out asu = [0.7, 0.2, 0.9, 0.6].
- Mask:
u >= 0.5is[True, False, True, True].- Keep the entries:
[1, 0, 1, 1].- Divide by :
y = [2, 0, 2, 2].Each entry is kept half of the time with value 2, and dropped the other half: on average it is still 1.
Backward
An entry that was dropped did not influence the output, so its gradient is 0. An entry that was kept was multiplied by , so its gradient is multiplied by the same factor. In short, the backward pass applies the same mask to the upstream gradient:
Example. Same mask, upstream gradient
[3, 3, 3, 3]: the result is[6, 0, 6, 6].
What to implement
dropout_forward(x, p, training, rng), returning(y, mask):- training (
training=True): drawu = rng.random(x.shape)once, keep the entries whereu >= p.maskis that boolean array. - evaluation (
training=False): returnxunchanged andmask = None. Do not draw any random numbers.
- training (
dropout_backward(grad_y, mask, p): the gradient with respect tox. WhenmaskisNone(evaluation), the gradient passes through unchanged.
Requirements
p = 0keeps everything.p = 1drops everything: return zeros, without dividing by zero.- Use the
rngyou are given (anp.random.Generator), exactly as described, so the tests can reproduce your mask.