0
0
PyTorchml~20 mins

Batch size and shuffling in PyTorch - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Batch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Effect of batch size on training speed

Which statement best describes how increasing the batch size affects the training speed of a neural network?

AIncreasing batch size usually speeds up training per epoch because more samples are processed in parallel.
BIncreasing batch size always slows down training because the model updates less frequently.
CIncreasing batch size has no effect on training speed since the total data is the same.
DIncreasing batch size causes the model to train slower because it requires more epochs.
Attempts:
2 left
💡 Hint

Think about how processing multiple samples at once affects computation.

Predict Output
intermediate
2:00remaining
Output shape with different batch sizes

What is the shape of the output tensor after passing a batch of images through a model?

Assume the model outputs a tensor of shape (batch_size, 10) for 10 classes.

PyTorch
import torch
batch_size = 16
model_output = torch.randn(batch_size, 10)
print(model_output.shape)
Atorch.Size([16, 10])
Btorch.Size([10, 16])
Ctorch.Size([1, 10])
Dtorch.Size([16])
Attempts:
2 left
💡 Hint

The first dimension is the batch size, the second is the number of classes.

Model Choice
advanced
2:00remaining
Choosing batch size for memory constraints

You have a GPU with limited memory. Which batch size choice is best to avoid out-of-memory errors while training a large model?

AUse a very large batch size to reduce training time.
BUse a batch size of 1 to minimize memory usage.
CUse a moderate batch size that fits comfortably in memory.
DUse batch size equal to the dataset size.
Attempts:
2 left
💡 Hint

Think about balancing memory use and training efficiency.

Metrics
advanced
2:00remaining
Effect of shuffling on training metrics

How does enabling shuffling in the data loader affect training metrics like loss and accuracy?

AShuffling causes training loss to increase because data order is random.
BShuffling improves training by reducing overfitting and improving generalization.
CShuffling has no effect on training metrics.
DShuffling always decreases accuracy because batches are inconsistent.
Attempts:
2 left
💡 Hint

Consider how randomizing data order affects learning.

🔧 Debug
expert
2:00remaining
Identifying error from incorrect shuffling usage

What error will this PyTorch DataLoader code raise?

from torch.utils.data import DataLoader, TensorDataset
import torch

data = torch.randn(100, 3)
targets = torch.randint(0, 2, (100,))
dataset = TensorDataset(data, targets)
loader = DataLoader(dataset, batch_size=10, shuffle=False)

for batch_data, batch_targets in loader:
    print(batch_data.shape, batch_targets.shape)

loader = DataLoader(dataset, batch_size=10, shuffle='yes')  # Incorrect shuffle argument
AValueError: batch_size must be an integer
BNo error, code runs fine
CRuntimeError: DataLoader shuffle argument missing
DTypeError: shuffle must be a boolean, not str
Attempts:
2 left
💡 Hint

Check the type expected for the shuffle argument.