torch.nn.Module.forward() contracts

if i understand correctly, if you want your module to be able to evaluate batches as well as single samples, your forward() method must accommodate that by handling BOTH correctly, rather than only single samples, since torch can’t reasonably infer whether a batch or a single sample was passed based on input_size as the input_size often even goes unspecified.
is this understanding correct?

(i came to this conclusion, as making a OneNormalizationLayer with

def forward(s,x):
    return x / x.abs().sum()

yields normalization across all entries of all samples if called with a batch)

It depends on the domain, but usually there is a batch dimension ‘N’ to the data (I assume x in this case), and the convention for a single sample is to simply have a batch dimension of size 1. It’s generally less common that a model is expected to handle data that has differing dimensions depending on the batch size.

1 Like

though the Modules in torch’s standard library, at least those where you specify a specific input size or just some quantity that relates to the input size, it seems to be capable to infer any positive number of batch dimensions. (so you can call them with a sample, or an additional n dimensions of multiple samples)