PyTorch Labels and Targets
Targets are the correct answers a loss compares predictions against. Each loss expects its targets in one set dtype and shape.
Regression targets match predictions
One predicted number needs one float target. The shapes and dtypes line up.
predictions = torch.tensor([[5.1], [6.8], [2.2]])
targets = torch.tensor([[5.0], [7.0], [2.0]])
print("Predictions:", predictions.shape, predictions.dtype)
print("Targets:", targets.shape, targets.dtype)Classification targets are class numbers
The scores carry one column per class. The targets carry one class number per row.
scores = torch.tensor([[2.0, 0.5, -1.0], [0.1, 0.2, 1.5]])
targets = torch.tensor([0, 2])
print("Scores:", scores.shape, scores.dtype)
print("Targets:", targets.shape, targets.dtype)
print("Targets:", targets.tolist())Example
import torch
from torch import nn
torch.manual_seed(0)
reg_model = nn.Linear(2, 1)
reg_x = torch.tensor([[1.0, 2.0], [3.0, 1.0], [2.0, 2.0], [0.0, 1.0]])
reg_y = torch.tensor([[5.0], [7.0], [6.0], [2.0]])
reg_pred = reg_model(reg_x)
reg_loss = nn.MSELoss()(reg_pred, reg_y)
torch.manual_seed(0)
cls_model = nn.Linear(2, 3)
cls_y = torch.tensor([0, 2, 1, 0])
cls_scores = cls_model(reg_x)
cls_loss = nn.CrossEntropyLoss()(cls_scores, cls_y)
print("Regression predictions shape:", reg_pred.shape)
print("Regression targets shape:", reg_y.shape)
print("Regression targets dtype:", reg_y.dtype)
print("Regression loss:", round(reg_loss.item(), 4))
print("Classification scores shape:", cls_scores.shape)
print("Classification targets shape:", cls_y.shape)
print("Classification targets dtype:", cls_y.dtype)
print("Classification loss:", round(cls_loss.item(), 4))Key points
- Target: the correct answer stored beside one input row
- Regression targets are float tensors shaped like the predictions
- Classification targets are int64 class numbers, one per row
- Cross-entropy reads class numbers directly, with no one-hot step
- A dtype or shape mismatch fails inside the loss
- The target must match what the loss expects, in dtype and in shape
In this example, one batch of four rows serves both kinds of task. Seed 0 is set before each model is built. The regression model predicts one number per row. Its targets are floats with the same (4, 1) shape. MSE compares them and prints 28.5773. The classification model predicts three scores per row. Its targets are four int64 class numbers with shape (4). Cross-entropy compares them and prints 1.4073. The prints show the two target forms side by side. Each loss received the form it expects.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
Which targets does cross-entropy expect for a batch of 8 rows and 4 classes?