Premium problem74. Dropout in Training and Evaluation

Medium Locked

Create nn.Dropout(p) and apply it to x twice: once in training mode and once in evaluation mode. Call torch.manual_seed(seed) immediately before the training-mode call so the dropout mask is reproducible.

Return the tuple (train_output, eval_output).

In training mode dropout zeroes elements at random and scales the survivors by 1/(1-p); in evaluation mode it is the identity. That difference is the whole point of model.eval(), and it is not the same thing as torch.no_grad(), which only stops the graph being recorded.

Input

x =
tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]])
p = 0.5
seed = 0

Output

(tensor([[0., 0., 2., 0., 0.],
        [0., 2., 2., 0., 2.]]), tensor([[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]]))

Premium problem

This one's part of Premium. Unlock the full PyTorch track plus every other premium problem on the site.

Implement solve(...)