PIXELBANKv9.1.0
Menu

Iterate Through DataLoader

Problem Statement

Write a function that iterates through a DataLoader and collects batch statistics. This simulates the core loop used during model training.

Background

During training, you iterate through the DataLoader to process data in batches. Each iteration yields one batch, and the last batch may be smaller if the dataset size isn't evenly divisible by batch size.

Your Task

Write a function get_batch_stats(dataloader) that iterates through all batches and returns statistics about them.

Output Format

Return a dictionary with keys: "num_batches" (int), "first_batch_size" (int), "last_batch_size" (int), "total_samples" (int).

Example:

Input:
7 samples, batch_size=3
Output:
{"num_batches": 3, "first_batch_size": 3, "last_batch_size": 1, "total_samples": 7}
Reasoning:

7 samples with batch_size=3 gives batches of sizes [3, 3, 1]

Constraints:

  • DataLoader will have at least one batch
  • Features and labels are the first two elements returned
  • Last batch may be smaller than batch_size
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Iterate Through DataLoader - Medium | PixelBank