3.3.4 · D1Deep Learning Frameworks

Foundations — Datasets and DataLoaders

2,209 words10 min readBack to topic

Before you can read the parent note, you must be able to read its alphabet. Below is every symbol and idea it silently assumes, built from nothing, in an order where each rung of the ladder rests on the one below it.


1. A "sample" and its index

We have many samples, so we number them The number is called an index and we write it . Sample number is — the little hanging below is just "which one."


2. The counting symbols: , , ,

Because we start counting at (computers do), the valid indices run

That range is written — a compact way of saying "from up to and including ."


3. Sets and the curly-brace notation

The parent writes the whole dataset as

Let us dismantle this piece by piece.

The little $i=1$ underneath and $N$ on top is a recipe for the collection: "let run from 1 to , and for each value drop the pair into the bag." So the fancy line just says:

The curly is simply a name for that whole bag of data — "D for Dataset."


4. The arrow and "a function / a mapping"

  • = the space of all possible inputs (all possible images).
  • = the space of all possible labels (all possible answers).
  • (the is "paired with") = the space of all (input, label) pairs.

Two properties fall straight out of "it's a function":

  • Deterministic: ask for index 5 twice, get the same sample twice. (Randomness lives elsewhere — see §7.)
  • Independent: fetching sample 5 does nothing to sample 6.

5. A "batch", the batch size , and the sum

The reason batches exist is the gradient formula the parent quotes:

Let us earn every symbol here so it is not scary.

  • (curly L) = the loss: one number saying "how wrong was the model on this sample" (big = very wrong, 0 = perfect).
  • (Greek "theta") = the weights of the model — the knobs it is allowed to turn.
  • (upside-down triangle, "nabla") = the gradient w.r.t. : an arrow pointing in the direction that would increase the loss fastest; we step the opposite way to reduce it.
  • = average the per-sample directions over the batch.
  • (a wearing a hat) = the estimate of the true gradient. The hat always means "our best guess of," not the exact thing.

6. Floor, ceiling, and modulo — counting the batches

If samples are cut into groups of , how many groups? Usually doesn't divide evenly, so we need three little tools.

Now the parent's two options make total sense. With , :

  • drop_last=True → throw away the 4 leftovers → batches, all size 32.
  • drop_last=False → keep them as a small final batch → batches (last one size 4).

7. Shuffling — where the randomness lives


8. Tensors and shape — the rectangular box of numbers


The prerequisite map

sample x y pair

index i and count N

set notation and the mapping arrow

Dataset as a function fetch by index

batch and batch size B

sum and gradient average

floor ceiling modulo count batches

shuffle and epoch

DataLoader

tensor shape and rectangle

Datasets and DataLoaders 3.3.4

Everything on the left is foundation; it all funnels into the Datasets and DataLoaders topic itself, which then powers the training loop, scales up with distributed training, and reuses pretrained data pipelines in transfer learning.


Equipment checklist

Read each question, answer in your head, then reveal.

What does a single "sample" contain?
An input (e.g. an image) paired with its label (the correct answer).
Why do we start indices at 0 and end at ?
Computers count from 0, so items occupy positions through ; index would be out of range.
Read aloud: .
"i is one of the whole numbers from 0 up to N minus 1."
What does the curly mean in words?
The collection of all N sample pairs, one for each index i from 1 to N.
Why is a Dataset a function () rather than a stored list?
A function fetches sample i only when asked (lazy loading), so RAM stays tiny; a stored list would need to hold everything at once.
What does tell you to do?
Add up the following expression for i = 1, 2, ..., B.
In , what is and why the hat?
An estimate of the true gradient (the average step-direction over the batch); the hat means "our best guess," not the exact value.
Why average the gradient over a batch instead of using one sample?
One sample's gradient is noisy; averaging B of them cancels the jitter and gives a steadier descent step.
With , : how many batches with drop_last=True vs False?
with drop_last=True; with drop_last=False (last batch has 4 samples).
What does compute?
The remainder — the number of leftover samples after packing as many full batches of size B as possible.
Does shuffling change the samples themselves?
No — only the order in which they are visited each epoch; the pairs are untouched.
Why must a batch tensor be rectangular (shape like )?
GPUs operate on rectangular matrices/tensors; every sample in the batch must share the same shape, which is why unequal-length data must be padded.