Sub topic 5
Multilabel outputs with independent sigmoids
This chapter assumes you know binary logistic regression, the sigmoid function, and binary cross-entropy for one target. It also assumes you have seen softmax for multiclass problems.
The working model
In a multilabel problem, each example can have any subset of K labels. The target is a multi-hot vector such as [1, 0, 1]. Labels do not compete, so the output layer treats each label as its own yes/no question.
The model produces K raw scores (logits), one per label. Each logit goes through its own sigmoid:
pk = 1 / (1 + e^(-zk))
Each p_k is the model's estimate that label k applies. The K probabilities do not need to sum to 1. An example can score 0.9, 0.8 and 0.1 across three labels, and that is a valid output.
The loss: binary cross-entropy per label
Training uses binary cross-entropy (BCE) on every label, then averages over labels and examples. For one label with target y and probability p:
loss = -(y log p + (1 - y) log(1 - p))
import numpy as np
logits = np.array([2.0, 1.0, -1.0])
y = np.array([1, 0, 1]) # multi-hot target
p = 1.0 / (1.0 + np.exp(-logits))
bce = -(y * np.log(p) + (1 - y) * np.log(1 - p))
print("probabilities:", p.round(3))
print("loss per label:", bce.round(3))
print("mean loss:", bce.mean().round(3))The second and third labels both have loss about 1.313, because the model is confidently wrong on the second and unsure on the third. The first label is nearly free at 0.127. Each label's gradient depends only on its own logit, which is why the labels are called independent.
Softmax versus sigmoid
| Property | Softmax | Per-label sigmoid |
|---|---|---|
| Target | One class index or one-hot | Multi-hot vector |
| Outputs sum to 1 | Yes | No |
| Loss | Categorical cross-entropy | Binary cross-entropy per label |
| Labels per example | Exactly one | Zero or more |
Common errors
- Using softmax on a multilabel target. Softmax forces the labels to share one unit of probability. With two true labels, the best it can do is about 0.5 each, and a 0.5 threshold then misses them.
- Applying argmax to sigmoid outputs. Argmax always returns exactly one label. That discards every other true label and can never predict "no label".
- Applying the sigmoid twice. Many frameworks provide a loss that takes raw logits and applies the sigmoid internally, for example
BCEWithLogitsLossin PyTorch. Passing probabilities into it gives wrong gradients and no error message. - Reading the probabilities as a distribution. A label probability of 0.3 says nothing about the other labels. Compare each p_k only with its own threshold.
Turning probabilities into labels
The sigmoid gives scores, not decisions. The next chapter covers thresholds. The default is p_k >= 0.5 for every label, which is a convention, not a rule. Rare labels often need a lower cutoff.
Exercise
A three-label model outputs probabilities 0.9, 0.7 and 0.6 for one example. Could a softmax output layer produce these values, and why does a sigmoid layer allow them?
Show answer
No. They sum to 2.2, and softmax outputs always sum to 1. Each sigmoid is computed from its own logit alone, so the three values are unconstrained and can all be high at once, which suits an example with several true labels.