LogSoftmax converts classification scores into log-probabilities. It is useful before negative log likelihood because it computes logarithms stably when probabilities approach zero. section 7.2.4
Use LogSoftmax when:
- A model predicts class scores for supervised classification. section 7.2.4
- The next loss is nn.NLLLoss, which expects log-probabilities rather than ordinary probabilities. section 7.2.4
- Numerical stability matters for very small predicted probabilities. section 7.2.4
Binary bird-versus-airplane classifier
A PyTorch model can end with nn.LogSoftmax(dim=1), then pass its output and a tensor of class indices to nn.NLLLoss(). Cross-entropy loss provides an alternative by combining these operations internally. section 7.2.4
# In[16]:
model = nn.Sequential(
nn.Linear(3072, 512),
nn.Tanh(),
nn.Linear(512, 2),
nn.LogSoftmax(dim=1))
Inspect the sequential classifier whose final layer is nn.LogSoftmax(dim=1). section 7.2.4
