Forward internal working

Hi, I’m new in PyTorch.

I was wondering how forward function works. I tried to fed (as a toy example), a net which consist in just a conv2d.
I saw that, instead of requiring a channelsHW tensor, it has an additional dimension which refers to the amount of samples (batch_size), such that if you input a:
input = [batch_size,channels,H,W] you get an output = [batch_size,channels’,H’,W’]

My doubt is:
Does the forward function iterate automatically over the whole batch or is the conv2d function (and all the implemented functions) prepared to process the whole batch.

So if I create a new kind of data to feed the global net (ie, a python dic which contains audio and images) would the dataloader automatically add this aditional dimension, concatenate all the samples of the batch and process the whole batch if i do model(batch) or do i have to consider this fact inside forward function?
Thank you very much for your help

The operations process the whole batch.
Usually you don’t even to add a batch dimension, if you use a Dataset and DataLoader.
While creating your Dataset you just have to define, how one sample is loaded.
The DataLoader automatically stacks these samples to a batch with dimensions [batch_size, ...].
Have a look at the Data loading tutorial for more information.

1 Like