Bird
Raised Fist0
PyTorchml~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using nn.Dropout in a PyTorch model?
easy
A. To increase the learning rate automatically
B. To add noise to the input data
C. To randomly disable neurons during training to prevent overfitting
D. To speed up the training process by skipping layers

Solution

  1. Step 1: Understand dropout's role in training

    Dropout randomly disables neurons during training to reduce overfitting by preventing co-adaptation of neurons.
  2. Step 2: Compare options with dropout purpose

    Only To randomly disable neurons during training to prevent overfitting correctly describes dropout's function; others describe unrelated concepts.
  3. Final Answer:

    To randomly disable neurons during training to prevent overfitting -> Option C
  4. Quick Check:

    Dropout = random neuron disabling [OK]
Hint: Dropout disables neurons randomly during training only [OK]
Common Mistakes:
  • Thinking dropout speeds up training
  • Confusing dropout with data augmentation
  • Believing dropout changes learning rate
2. Which of the following is the correct way to create a dropout layer with 30% dropout rate in PyTorch?
easy
A. nn.Dropout(30)
B. nn.Dropout(p=30)
C. nn.Dropout(rate=0.3)
D. nn.Dropout(0.3)

Solution

  1. Step 1: Check PyTorch dropout syntax

    The dropout layer takes a float between 0 and 1 as the probability of dropout, passed as the first argument or named 'p'.
  2. Step 2: Validate each option

    nn.Dropout(0.3) uses nn.Dropout(0.3) which is correct. nn.Dropout(p=30) uses p=30 (invalid, should be 0.3). nn.Dropout(rate=0.3) uses 'rate' which is not a valid argument. nn.Dropout(30) passes 30 (integer) which is invalid.
  3. Final Answer:

    nn.Dropout(0.3) -> Option D
  4. Quick Check:

    Dropout probability is float 0-1 [OK]
Hint: Dropout probability is a float between 0 and 1 [OK]
Common Mistakes:
  • Using integer instead of float for dropout rate
  • Using wrong argument name like 'rate'
  • Passing percentage as whole number
3. Consider the following PyTorch code snippet:
import torch
import torch.nn as nn

layer = nn.Dropout(0.5)
input_tensor = torch.ones(4)
layer.train()
output_train = layer(input_tensor)
layer.eval()
output_eval = layer(input_tensor)
print(output_train)
print(output_eval)

What will be the output of print(output_eval)?
medium
A. A tensor of all ones: tensor([1., 1., 1., 1.])
B. A tensor with some zeros randomly placed
C. A tensor of all zeros
D. An error because dropout is disabled in eval mode

Solution

  1. Step 1: Understand dropout behavior in eval mode

    Dropout disables neuron dropping during evaluation mode and passes input unchanged.
  2. Step 2: Analyze output_eval value

    Since layer.eval() is called before output_eval, the output will be the same as input: all ones tensor.
  3. Final Answer:

    A tensor of all ones: tensor([1., 1., 1., 1.]) -> Option A
  4. Quick Check:

    Dropout off in eval mode = input unchanged [OK]
Hint: Dropout disables only in eval mode, output equals input [OK]
Common Mistakes:
  • Expecting dropout to apply in eval mode
  • Confusing train() and eval() modes
  • Thinking dropout outputs zeros always
4. You wrote this PyTorch code but the dropout layer seems to have no effect during training:
import torch.nn as nn
layer = nn.Dropout(0.4)
output = layer(input_tensor)

What is the most likely reason dropout is not working as expected?
medium
A. Dropout only works on GPU tensors
B. You forgot to call layer.train() to enable dropout
C. The dropout probability 0.4 is too low to see effect
D. You need to call layer.eval() to activate dropout

Solution

  1. Step 1: Recall dropout behavior in train vs eval modes

    Dropout only disables neurons during training mode. In eval mode, dropout is disabled.
  2. Step 2: Identify missing train mode call

    If layer.train() is not called (e.g., after a previous layer.eval()), the layer stays in eval mode, so dropout has no effect.
  3. Final Answer:

    You forgot to call layer.train() to enable dropout -> Option B
  4. Quick Check:

    Dropout active only in train mode [OK]
Hint: Call train() to activate dropout during training [OK]
Common Mistakes:
  • Assuming dropout works without train() mode
  • Thinking dropout depends on tensor device
  • Calling eval() instead of train()
5. You want to add dropout to a neural network to reduce overfitting. Which of the following is the best practice when using nn.Dropout in your model?
hard
A. Apply dropout only during training and disable it during evaluation
B. Apply dropout during both training and evaluation for consistency
C. Apply dropout only during evaluation to test robustness
D. Apply dropout only to the input layer and never to hidden layers

Solution

  1. Step 1: Understand dropout's intended use

    Dropout is designed to randomly disable neurons during training to prevent overfitting.
  2. Step 2: Recall dropout behavior during evaluation

    During evaluation, dropout is disabled to use the full network for predictions.
  3. Step 3: Evaluate options

    Apply dropout only during training and disable it during evaluation correctly states dropout is applied only during training. Options B and C are incorrect because dropout should not be active during evaluation. Apply dropout only to the input layer and never to hidden layers is incorrect because dropout can be applied to hidden layers as well.
  4. Final Answer:

    Apply dropout only during training and disable it during evaluation -> Option A
  5. Quick Check:

    Dropout active in train, off in eval [OK]
Hint: Dropout off during eval, on during training [OK]
Common Mistakes:
  • Applying dropout during evaluation
  • Limiting dropout only to input layer
  • Confusing dropout with data augmentation