What exactly does the forward function output in Pytorch?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In PyTorch, the forward method outputs whatever you explicitly return from it. In many tutorials that is a single tensor of logits or predictions, but PyTorch does not force that shape or type. A forward method can return a tensor, a tuple, a dictionary, a list of tensors, or any Python object structure that downstream code knows how to use.
What Happens When You Call a Model
When you write model(x), PyTorch does not call forward directly in the naive sense. It goes through the module call machinery, which then invokes forward and also handles hooks and other framework behavior.
A minimal model:
Here forward returns a tensor of shape 3 x 2, so model(x) also returns that tensor.
There Is No Special Hidden Output Type
A common misconception is that forward must return predictions in some special internal PyTorch format. It does not.
PyTorch only requires that the returned object works with the rest of your training or inference code. For example, if you plan to compute a loss, the loss function must know how to consume the returned value.
That is why many training loops assume the model returns a tensor, but that is a convention of the surrounding code, not a hard rule of forward itself.
Returning Multiple Values
It is common to return more than one value.
This can be useful when you want both raw logits for a loss function and normalized probabilities for reporting.
Returning Dictionaries Is Also Valid
Large models often return named outputs.
Again, this is perfectly legal as long as your training and inference code expects that structure.
The Output Is Often Logits, Not Final Class Labels
In classification examples, forward often returns logits rather than already-thresholded labels or argmax classes. That is because loss functions such as cross-entropy often expect raw logits.
So if you ask "what exactly does forward output?" the most accurate answer is:
- whatever the model designer decided to return
- often a tensor of logits in classification models
- often a tensor of continuous predictions in regression models
- sometimes a richer structure for more complex pipelines
Common Pitfalls
The biggest mistake is assuming forward must always return a single tensor.
Another mistake is returning post-processed probabilities or class indices when the loss function actually expects raw logits.
A third issue is calling model.forward(x) directly instead of model(x), which bypasses parts of the normal module call path.
Summary
- '
forwardreturns whatever object you explicitly return from the method' - In many models that is a tensor, but it can also be a tuple, dict, or other structure
- The surrounding training and inference code determines what output format is practical
- Classification models often return logits, not final labels
- Use
model(x)rather than callingforwarddirectly in normal PyTorch code

