Video 14 - Binary Cross Entropy Loss
Video 14 - Binary Cross Entropy Loss
Antonio Rueda-Toicen
Learning goals
model = nn.Sequential(
# We get raw logits as output
nn.Linear(input_size, 3),
)
criterion = nn.CrossEntropyLoss()
loss = criterion(predictions, targets)
Binary Cross Entropy
Image from Grounding DINO: Marrying DINO with Grounded Pre-Training for Open-Set Object Detection
Single label binary classifiers in PyTorch
model = nn.Sequential(
model = nn.Sequential( nn.Linear(input_size, 1),
# Raw logits output # Convert to probabilities
nn.Linear(input_size, 1) ) nn.Sigmoid()
)
criterion = nn.BCEWithLogitsLoss()
loss = criterion(logits, targets) criterion = nn.BCELoss()
loss = criterion(predictions, targets)
Multilabel classification on satellite images
https://www.kaggle.com/c/planet-understanding-the-amazon-from-space/
Multilabel classifiers
p=0.9
Multilabel classifiers in PyTorch
https://www.kaggle.com/c/planet-understanding-the-amazon-from-space/
Summary
Cross entropy and the final activation function
● Cross entropy measures the mismatch between predicted and true distributions
● Softmax handles single-label problems with multiple classes
● Sigmoid handles both binary and multi-label cases
Network design
● Match output layer size to number of classes
● Consider single-label vs multi-label requirements for final layer
● Choose activation function based on classification type (e.g. single label vs multi-label)
Different tasks require different variants of cross entropy loss
● Use nn.BCELoss or nn.BCEWithLogitsLoss for binary or multi-label classification
tasks
● Use nn.CrossEntropyLoss for single label tasks
Further reading
LogSoftmax vs Softmax
● https://discuss.pytorch.org/t/logsoftmax-vs-softmax/21386