π Sigmoid
Descriptionβ
< What is it? >β
The sigmoid function, written , squeezes any real-valued logit into the range :
It is commonly used at the output of a binary classifier, where its result can be interpreted as the model's probability for the positive class.
Key pointsβ
- Derivative:
- Not usually a hidden-layer default: for large positive or negative inputs it saturates, making its derivative near zero and slowing gradient flow; ReLU or GELU is usually preferred
- Binary vs. multiclass: use sigmoid for independent binary labels; use softmax for one choice among mutually exclusive classes
- Numerical stability: during training, give logits directly to a combined binary-cross-entropy loss when available instead of computing sigmoid separately
Q&Aβ
< Why use sigmoid, not y = x? >β
Q: Why use sigmoid instead of a linear function ?
A: Exploding gradients is the primary downside. Using (a linear classifier) works in theory but fails in practice:
- Downsides:
- Exploding gradients: To minimize loss, the model pushes class-1 scores to +β and class-0 scores to -β. This creates massive gradients, causing weight updates to balloon and training to crash (NaN).
- Brittle thresholds: The decision threshold depends on the arbitrary scale of your weights. Sigmoid naturally fixes the boundary at 0.5; y=x forces you to manually re-tune the threshold for every run.
- Outlier sensitivity: A single outlier scoring 100 creates a linearly massive gradient, warping the whole model. Sigmoid saturates, making 100 and 2 produce similar gradients, so outliers don't dominate.
- Crucial nuance: We do output y=x in practiceβthey're called logits. We don't apply sigmoid manually; instead, we feed them into a loss function like BCEWithLogitsLoss, which applies the sigmoid inside the loss calculation for numerical stability. This ensures we still compare probabilities to labels, but without the gradient chaos.
< Max derivative >β
Q: What is the maximum derivative of the sigmoid function?
A: what is the max slope of sigmoid function and why?
For the standard sigmoid function
its derivative is
Let , where . Then
This is maximized when , which occurs at . Therefore,
So the sigmoidβs maximum slope is 0.25 at .
For a scaled sigmoid , the maximum slope becomes . This bound also helps explain vanishing gradients: away from zero, the sigmoid derivative approaches , and even at its steepest it is only .
< what is the slope when x is 2 or -2? >β
Q: What is the slope (derivative / gradient) of the sigmoid function when x is 2 or -2?
A: The sigmoid derivative is
At :
At :
The slopes are equal because the sigmoidβs derivative is symmetric around (x=0):