Ptn BCE With Logits Loss
The numerically stable binary classifier loss
- PTLOSS
Ptn BCE With Logits Loss is the loss node you actually want for binary classification in the HowToSD/ComfyUI-Pt-Wrapper pack, wrapping nn.BCEWithLogitsLoss. It combines a sigmoid and a binary cross-entropy into a single numerically stable operation. The headline difference from the plain Ptn BCE Loss node: this one eats raw logits - the direct output of a model whose last layer is linear - and handles the sigmoid internally.
Why you'd reach for it
Almost every binary classifier you build ends in a linear layer, which means its output is an unbounded real number, not a probability. The mathematically naive approach is sigmoid(logits) then BCELoss(probabilities, labels). That works until the sigmoid saturates - then log(0) rears up and you're chasing NaNs. BCEWithLogits fuses the two steps so the log-sum-exp math cancels out and stays stable precisely in the saturation regime where the two-step version falls apart. In this pack's dog-vs-cat and text-classification workflows, this is the one you want for anything with two classes, and it's the node the author points you to when your model outputs logits.
How it works
nn.BCEWithLogitsLoss(input, target) computes the same binary cross-entropy as before, but internally applies log-sigmoid in a numerically safe way. It's implemented so that extreme logits map to a large-but-finite loss instead of NaN. The reduction dropdown matches the family convention: mean (average, the default choice for training), sum (total, batch-size-dependent), or none (per-element, if you want to reweight or inspect). The output is a PTLOSS object - you don't call it directly; wire it into a training node like Pt Train Classification Model, or into Pt Compute Loss to get the scalar.
The inputs
One required input: reduction (mean / sum / none). Output: PTLOSS.
Installing the pack
This node lives in the "Training" category of the pack. Install via ComfyUI Manager (search "ComfyUI-Pt-Wrapper") or manually:
cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
Restart ComfyUI. The pack installs a heavy requirements.txt (transformers, datasets, peft, scikit-learn, gensim…); no model downloads are needed for the loss nodes.
Common issues
- Sigmoid-ing anyway - if you add a Sigmoid before this node out of habit, you're feeding probabilities where logits belong, which silently gives you the wrong loss. Pick one: probabilities → Ptn BCE Loss, logits → this node. Don't do both.
- Label dtype - targets should be floats (0.0/1.0), not ints. A dtype mismatch raises a torch error.
- Shape mismatch - predictions and labels must be the same shape; broadcasting here is rarely what you intended.
- Support reality - Pt-Wrapper is a single-author educational pack with almost no r/comfyui or r/StableDiffusion footprint. The repo's node reference and training guides are your documentation; there isn't a Reddit thread waiting for you.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| reduction | COMBO | 3 options: mean, sum, none |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| PTLOSS | PTLOSS | — |