Why torch does train using mini-batches?
I'm currently tring to understand how to train models via pytorch. And while this I saw a pretty interesting feature: passing to train data --- is a mini-batch. For ex.
There is a code fragment from official pytorch web-site
...

data_dir = 'data/hymenoptera_data'
image_datasets = {x: datasets.ImageFolder(os.path.join(data_dir, x),
                                          data_transforms[x])
                  for x in ['train', 'val']}
dataloaders = {x: torch.utils.data.DataLoader(image_datasets[x], batch_size=4,
                                             shuffle=True, num_workers=4)
              for x in ['train', 'val']}

...

...

for inputs, labels in dataloaders[phase]:
    inputs = inputs.to(device)
    labels = labels.to(device)

    optimizer.zero_grad()

    with torch.set_grad_enabled(phase == 'train'):
        outputs = model(inputs)
        _, preds = torch.max(outputs, 1)
        loss = criterion(outputs, labels)

...

According to this code, inputs that passing to model is a mini-batch.
I've tied to fiend some info about this, but unsuccessfully.
But I'm really curious, is it some kind of boost (parallel run, etc.) or neccussary thing. So, would you mind to help me figure it out and tell me, why there is a mini-batch passing to train function?
N.B. Will not refuse a link to the paper [smily :)].

Imagine it this way; say you want to learn the difference between a dog and a cat, and you have never seen them before.
The batches would be that we show you, say, 10 images of a dog and a cat at a time. You can, rather fast, learn the some of differences of cats and dogs after say 4x10 images (4 batches) but you'll of course be biased if e.g all the dogs you've been showed so far have been large dogs, thus you might classify all small dogs as cats. After enough batches you'll learn, and then unlearn, different features since you don't see them all at once, but the important thing is, you start learning something fast.
On the other hand, say we show you 100 images instead of 10. It will take much longer for you to look all the images through and compare them to each other, but you'll learn the differences in &quot;one go&quot; so to speak.
Either way; when you have processed those images (either as batches or the entire dataset), I can then show you one image, and you can tell me if it is a dog or a cat, even though you have learned from multiple images.
