Nodes/ComfyUI-Pt-Wrapper/Ptn Smooth L1 Loss
ComfyUI Node

Ptn Smooth L1 Loss

Ptn Smooth L1 Loss sits between L1 and L2

By HowToSD·Created about a year ago·Updated about a year ago· 7
Ptn Smooth L1 Loss
    • PTLOSS
    reduction
    beta1

    PtnSmoothL1Loss gives you PyTorch's nn.SmoothL1Loss as a node - a loss function that's mathematically wedged between mean absolute error and mean squared error. For regression training in ComfyUI-Pt-Wrapper, it's the sensible default whenever your data has the occasional wild outlier that would otherwise yank your whole model around. It's one of several loss nodes the pack exposes (BCE, cross-entropy, and friends), and it outputs the pack's PTLOSS type that plugs into Pt Compute Loss or the training nodes.

    Why you'd reach for it

    Plain L1 loss treats every error equally, which means one bad data point can drag training. Plain L2 (MSE) punishes big errors quadratically, which makes training very sensitive to outliers - a single crazy value can dominate the gradient. Smooth L1 splits the difference: small errors are penalized like L1 (robust, linear), large errors also like L1 (so they don't blow up), and there's a smooth quadratic transition in between. That's why it's the default choice in a lot of object-detection heads - it's the pragmatic middle child. Reach for it when your regression data has noise spikes; reach for plain MSE when your errors are well-behaved and you want stronger gradient pressure near the minimum.

    How it works

    Two inputs, both from PyTorch's own recipe:

    • reduction - mean, sum, or none. mean is the standard for training (a per-batch average, so the loss doesn't scale with batch size); sum adds everything up; none returns a per-element tensor, which you'd only want if you're doing your own post-processing of the loss.
    • beta - the threshold that defines "small error." Errors below beta are treated with the smooth quadratic behavior; errors above it get the linear L1 treatment. Default 1.0 is fine in most cases. A larger beta makes more of the error range behave like L2 (more sensitivity); a smaller one makes the function more purely L1.

    The output is a PTLOSS object, not a number. You wire it into Pt Compute Loss (which pairs it with the model's output and the target tensors) or directly into a training node that accepts a loss. That's the pack's pattern: model nodes output PTMODEL, optimizers output PTOPTIMIZER, losses output PTLOSS, and the trainer ties the whole bundle together.

    The one thing worth knowing

    The reduction choice is the only setting you'll revisit. If your training loss numbers jump around between runs and you're comparing against another pipeline, make sure both use the same reduction - mean vs sum gives different scales entirely. Also note beta has a huge allowed range in the node (up to 1e9), which is just PyTorch's own bounds surfacing; you'll practically never leave 0.1–10.

    Installing it

    Ships in ComfyUI-Pt-Wrapper (HowToSD's no-code PyTorch pack, a spin-off of ComfyUI-Data-Analysis). Install via ComfyUI Manager (search "ComfyUI-Pt-Wrapper") or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/HowToSD/ComfyUI-Pt-Wrapper
    

    Restart ComfyUI - the first start is slow while requirements.txt installs pandas, scikit-learn, transformers, sentencepiece, peft and the rest. No model files to fetch for this node.

    CategoryTraining

    Inputs (2)

    NameTypeDefaultDescription
    reductionCOMBO3 options: mean, sum, none
    betaFLOAT10–1000000000

    Outputs (1)

    NameTypeDescription
    PTLOSSPTLOSS