0
0
PyTorchml~20 mins

Dropout (nn.Dropout) in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Dropout Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Dropout layer during training
What is the output of the following PyTorch code snippet when the model is in training mode?
PyTorch
import torch
import torch.nn as nn

torch.manual_seed(0)
dropout = nn.Dropout(p=0.5)
input_tensor = torch.tensor([1.0, 2.0, 3.0, 4.0])
output = dropout(input_tensor)
print(output)
Atensor([2., 0., 6., 8.])
Btensor([1., 2., 3., 4.])
Ctensor([0., 0., 0., 0.])
Dtensor([0.5, 1., 1.5, 2.])
Attempts:
2 left
💡 Hint
Remember that dropout randomly zeroes some elements and scales the others by 1/(1-p) during training.
🧠 Conceptual
intermediate
1:30remaining
Effect of Dropout during evaluation
What happens to the output of nn.Dropout when the model is switched to evaluation mode (model.eval())?
ADropout randomly zeros some inputs and scales the rest.
BDropout sets all inputs to zero.
CDropout scales the input by the dropout probability p.
DDropout passes the input through unchanged without any modification.
Attempts:
2 left
💡 Hint
Think about why dropout is only used during training.
Hyperparameter
advanced
1:30remaining
Choosing dropout probability
Which dropout probability (p) value is generally recommended to prevent overfitting without causing underfitting in a neural network?
Ap = 0.5 (moderate dropout)
Bp = 0.1 (very low dropout)
Cp = 0.0 (no dropout)
Dp = 0.9 (very high dropout)
Attempts:
2 left
💡 Hint
Typical dropout values are between 0.2 and 0.5.
🔧 Debug
advanced
2:00remaining
Identifying error in dropout usage
What error will occur when running this code snippet?
PyTorch
import torch
import torch.nn as nn

dropout = nn.Dropout(p=1.2)
input_tensor = torch.tensor([1.0, 2.0, 3.0])
output = dropout(input_tensor)
print(output)
AValueError: input tensor shape mismatch
BRuntimeError: dropout probability has to be between 0 and 1
CNo error, outputs tensor with all zeros
DTypeError: input_tensor must be float type
Attempts:
2 left
💡 Hint
Check the valid range for dropout probability p.
Model Choice
expert
2:30remaining
Best place to apply dropout in a neural network
Where is the best place to apply nn.Dropout in a feedforward neural network to improve generalization?
AAfter the output layer
BBefore the input layer
CBetween hidden layers after activation functions
DOnly on the final prediction output
Attempts:
2 left
💡 Hint
Dropout is usually applied to internal layers to prevent co-adaptation of neurons.