Ptn BCE Loss
Binary cross-entropy for probabilities, not logits
- PTLOSS
Ptn BCE Loss is the binary cross-entropy loss node in the HowToSD/ComfyUI-Pt-Wrapper pack, wrapping nn.BCELoss. It measures how far your predictions are from the ground truth when you're solving a two-class problem - dog vs. cat, spam vs. not-spam, "contains the thing" vs. "doesn't." The loss is low when predictions match labels, high when they don't, and training is the process of pushing it down.
The critical detail is in the name: this node expects probabilities in (0, 1), not raw logits. If your model's last layer is linear and its output can be any real number, feeding that into this node is wrong and will produce nonsense or NaNs. The author is explicit about it in the node docs: apply a Sigmoid first (via a Ptf Sigmoid node or a chained closure), or skip all that and use the Ptn BCE With Logits Loss node, which is built for exactly that case.
How it works
nn.BCELoss computes, per element, - [ y * log(p) + (1 - y) * log(1 - p) ], where p is your predicted probability and y is the 0/1 label. The reduction dropdown controls what happens to those per-element values: mean averages them (the standard choice for training), sum adds them up (batch-size-dependent, so watch your learning rate), and none keeps them per-element (handy if you want to weight or inspect individual losses yourself). The node outputs a PTLOSS, which you don't compute with directly - you feed it into a training node like Pt Train Classification Model, or into Pt Compute Loss if you want the scalar yourself.
The inputs
Just one: reduction, with choices mean, sum, none. Output: PTLOSS.
Installing the pack
Ptn BCE Loss is in the pack's "Training" category. Install via ComfyUI Manager (search "ComfyUI-Pt-Wrapper") or:
cd ComfyUI/custom_nodes
git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
Restart ComfyUI. Install pulls in transformers, datasets, peft, scikit-learn, gensim and more; no model files are downloaded for loss nodes.
Common issues
- Feeding logits in - the classic. If your loss explodes or goes NaN and your model ends in a linear layer, you're almost certainly feeding logits to a probabilities-expecting loss. Add Sigmoid or switch to the With-Logits variant.
- NaN from p=0 or p=1 -
log(0)is undefined. If sigmoid saturates hard, BCELoss can throw NaN. Clipping or the logits version handles this more gracefully. - Label shape - labels must be the same shape as predictions, values 0/1 (or probabilities for soft targets). Shape mismatches error immediately.
- Thin support network - like the rest of this pack, it's a single-author educational project with essentially zero presence on r/comfyui. The node reference and the training docs on the repo are your best resources.
Inputs (1)
| Name | Type | Default | Description |
|---|---|---|---|
| reduction | COMBO | 3 options: mean, sum, none |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| PTLOSS | PTLOSS | — |