Ptn NLL Loss
NLL loss as a node — and why it needs log-probabilities, not logits
- PTLOSS
Negative log-likelihood loss is the standard loss for multi-class classification - but it has one non-negotiable input format: it wants log-probabilities, not raw logits. PtnNLLLoss wraps nn.NLLLoss and is honest about that in its own docstring, which is more than most loss nodes bother to be. You reach for it when your model ends in a LogSoftmax (or equivalent) and you want the classic classifier objective. In this pack the usual move is actually Ptn Cross Entropy Loss, which fuses log-softmax + NLL into one node; this one exists for when you want the pieces explicit.
How it works
nn.NLLLoss takes, per sample, a log-probability vector across classes plus an integer class index, and it scores −log p(class) for the correct class. Minimizing it is maximizing the log-likelihood of the right answer. The node passes through whatever reduction you pick. The critical detail: feed it raw logits and the math silently comes out wrong-ish - the loss value will still be finite, but it won't correspond to any proper probability, and gradients won't train as intended. LogSoftmax first, always.
The input that matters
- reduction -
mean(divide by batch size - the standard),sum, ornone.meankeeps the loss scale comparable across batch sizes, which is why it's the default.
Output is a single PTLOSS, wired into a trainer's loss_function input or a compute-loss node. Unlike the BCE-with-logits node, there's no sigmoid anywhere in this node - the log-softmax must come from your model chain (via a Ptf Log Softmax-style activation or an explicit PtnModelWithClosure wrapper that applies it).
Installing
Same as every node in the pack. ComfyUI Manager → search "Pt-Wrapper", or:
cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
Restart ComfyUI; the pack's requirements install on first launch.
Where people get burned
The format mismatch is the whole failure mode. If your model outputs logits and you connect this node anyway, training behaves badly in ways that are easy to misread as a learning-rate problem. Check the model's final layer: logits → use Ptn Cross Entropy Loss; log-probabilities → use this. And the target must be integer class indices, not one-hot vectors - one-hot targets will error or silently misbehave depending on shapes.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| reduction | COMBO | 3 options: mean, sum, none |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| PTLOSS | PTLOSS | — |