ComfyUI Node

Ptn MSE Loss

MSE as a node — the default regression loss, squared and averaged

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

    Mean squared error is the default starting point for almost any regression task, and PtnMSELoss is how you get it in this pack - a thin wrapper around nn.MSELoss that emits a PTLOSS you plug straight into a trainer. You reach for it when your model outputs a continuous value (a number, a coordinate, a price) and you want to minimize the squared difference from the target. It's the loss you'd pair with the pack's simple linear regression example.

    How it works

    The node builds torch.nn.MSELoss. Per element it computes (predicted − target)², then combines the results according to your reduction setting. The squaring is the whole personality of MSE: large errors dominate the loss and its gradient scales with the error size, so the optimizer aggressively corrects big mistakes. That's great when your targets are clean and terrible when your data has outliers - one bad label can steer the whole run.

    The input that matters

    • reduction - mean (average, the usual choice), sum (total), or none (keep per-element values for manual reduction downstream). For a fixed batch size, mean vs sum is just a constant factor, but mean keeps your learning rate meaningful if batch size changes.

    Output is a single PTLOSS, wired into the loss_function input of Pt Train Model or a compute-loss node for evaluation. Note this node expects the model's raw output to already be in target units - there's no sigmoid or softmax involved, so don't use it on classification logits unless that's genuinely what you mean.

    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.

    Where people get burned

    Two things. First, shapes must match element-wise between prediction and target, or you'll get a broadcast error that's easy to misread as a model bug. Second - the subtle one - the squared error means your loss scale depends on the units of your target. If your target values are in the thousands, MSE can be astronomically large and your learning rate will look broken. Normalize targets or lower the LR, not the loss.

    CategoryTraining

    Inputs (1)

    NameTypeDefaultDescription
    reductionCOMBO3 options: mean, sum, none

    Outputs (1)

    NameTypeDescription
    PTLOSSPTLOSS