Nodes/ComfyUI-Pt-Wrapper/Ptn Cross Entropy Loss
ComfyUI Node

Ptn Cross Entropy Loss

The default loss for multiclass classification

By HowToSD·Created about a year ago·Updated about a year ago· 7
Ptn Cross Entropy Loss
    • PTLOSS
    reduction

    Ptn Cross Entropy Loss is the go-to loss for multiclass classification in the HowToSD/ComfyUI-Pt-Wrapper pack, wrapping nn.CrossEntropyLoss. If your model classifies an image or a text into one of N categories - CIFAR-10's ten classes, Fashion-MNIST's ten, your own dog-vs-cat-plus-everything-else dataset - this is the loss node you'll wire in. It's the default for a reason: it combines a log-softmax and a negative log-likelihood into one numerically stable operation.

    Why you'd reach for it

    For classification, "the model output should be a score per class, and we want to punish being confident about the wrong class" is the entire story, and CrossEntropy is the standard formalization of it. The node takes your model's raw logits (the direct output of a linear head - no separate softmax needed) and the integer class labels, and produces a single loss value. It's the loss used across the pack's classification training examples, so if you're following along with a doc, you'll see this node in almost every training graph. Multiclass with more than two classes → reach for this. Two classes only → BCEWithLogits is often simpler, but CrossEntropy works there too.

    How it works

    nn.CrossEntropyLoss(input, target) internally applies log-softmax over the class dimension of the input, then takes the negative log-likelihood of the correct class. Doing it in one fused step avoids the float precision problems you'd hit by computing softmax and log separately. It expects the input shape (batch, num_classes) with the class dimension last, and target labels as integers in [0, num_classes). The reduction dropdown follows the family convention: mean (default choice, averages the batch), sum (total, batch-size-dependent), none (per-sample losses). Output is a PTLOSS to feed into a training node like Pt Train Classification Model, or Pt Compute Loss if you want the scalar.

    The inputs

    One: reduction (mean / sum / none). Output: PTLOSS.

    Installing the pack

    Part of the "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 drags in transformers, datasets, peft, scikit-learn, gensim and more; no model downloads for loss nodes.

    Common issues

    • Input shape - the class scores dimension must be second ((batch, num_classes)). If your model outputs (num_classes, batch) you'll get a confusing mismatch error; use Pt Permute to fix.
    • Label range - labels must be integers between 0 and num_classes−1. A label of 10 on a 10-class model errors immediately.
    • Applying softmax yourself - don't. This node expects logits and softmaxes internally; feeding already-softmaxed probabilities silently produces wrong (and sometimes NaN) losses.
    • Support net is thin - single-author educational pack with essentially no r/comfyui footprint. The node reference and training docs on the repo are your documentation.
    CategoryTraining

    Inputs (1)

    NameTypeDefaultDescription
    reductionCOMBO3 options: mean, sum, none

    Outputs (1)

    NameTypeDescription
    PTLOSSPTLOSS