ComfyUI Node

Ptn L1 Loss

Mean absolute error as a node — the outlier-proof regression loss

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

    L1 loss - mean absolute error - is the regression loss you pick when you don't want a few big outliers to dominate training. Where MSE squares the error (punishing large mistakes hard), L1 averages the absolute differences, so every error contributes linearly. PtnL1Loss wraps nn.L1Loss and hands you the result as a PTLOSS, ready to plug into a trainer node. It's one of the simplest nodes in the pack and there's almost nothing to configure.

    How it works

    The node constructs torch.nn.L1Loss with the reduction you choose. The math is mean(|predicted − target|) per element, which in gradient terms gives a constant-magnitude gradient - that's exactly why it's more robust to outliers than MSE. The only thing you set is how the per-element errors get combined into a single scalar.

    The input that matters

    • reduction - a dropdown with mean (average the errors), sum (add them up), or none (keep the per-element tensor). mean is the default choice for most training; sum only differs by a constant factor for fixed batch sizes; none is for when you want to reduce it yourself downstream.

    The output is a single PTLOSS, which you wire into the loss_function input of a trainer such as Pt Train Model, or into Pt Compute Loss for a standalone evaluation pass. Pair it with a regression-style model (e.g. the pack's simple linear regression example) - it's not the natural fit for classification, where you'd use Ptn BCE With Logits Loss or Ptn Cross Entropy Loss instead.

    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. Nothing extra is needed for the loss nodes - they're pure PyTorch.

    Where people get burned

    The main gotcha is shape agreement: L1 compares element-wise, so predicted and target must have identical shapes. A mismatch shows up as a confusing broadcast error at the trainer. Also, if your loss curve looks jagged compared to an MSE run, that's L1 doing its thing - its gradient magnitude is constant, so it never accelerates toward a minimum the way MSE does. That's a property, not a bug.

    CategoryTraining

    Inputs (1)

    NameTypeDefaultDescription
    reductionCOMBO3 options: mean, sum, none

    Outputs (1)

    NameTypeDescription
    PTLOSSPTLOSS