Squared Loss
The loss function that taught a generation of regression — half, on purpose
- y_hat
- y
- loss
If you're trying to learn how a training loop actually works - not how to prompt a model, how the math loops - this is the node you want in the middle of your graph. Squared Loss computes the classic mean-free squared loss (y_hat - y)^2 / 2 between a prediction and a ground-truth label. It's the exact function from the Dive into Deep Learning textbook that this whole pack is built around, and it's what you reach for the moment you want to watch a linear regression actually learn in ComfyUI instead of in a Jupyter notebook.
The name undersells the details. Notice the /2 - that's not noise. It's a d2l convention: the derivative of (y_hat - y)^2/2 with respect to y_hat is just y_hat - y, no stray factor of 2 to track. When you hand this to the pack's SGD step, the gradient math stays clean. Also note it does not average. Wire in a batch and you get one loss value per example, not a mean. If you want a scalar to watch, sum or mean the output before you plot it.
What goes in, what comes out
Only two inputs, both the pack's cdlTensor type:
y_hat- your model's prediction.y- the ground truth. The node reshapesyto matchy_hat's shape, so you don't have to sweat a missing batch dimension; it just works as long as the element counts line up.
The single output is loss, a cdlTensor of the same shape as y_hat. That output is a dead end until you do something with it, and ComfyDL's own way of seeing a tensor is to wire it into Tensor → String (CdlTensorToStr). There are also visualization nodes in the pack if you want a histogram of residuals - the README's linear regression example plots exactly that.
Where it lives in a real workflow
The pack's canonical demo: Synthetic Data generates X and y, Linear Regression (CdlLinReg) produces y_hat from X, Squared Loss compares y_hat against y, and SGD Step applies the update. Run that loop a few times and watch the loss shrink. It's the cleanest possible "see gradient descent work" moment ComfyUI can give you, and it's a genuinely fun way to internalize what a loss function is if you've only ever seen them as numbers on a training dashboard.
Installing it
This ships as part of ComfyDL - it isn't a standalone node. Easiest path is ComfyUI Manager: search "ComfyDL" and install. Or clone it yourself:
cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
pip install -r ./ComfyDL/requirements.txt
Then restart ComfyUI. Good news: the entire pack's only Python dependency is matplotlib. No model downloads, no torch version pins, nothing that's going to fight your existing environment.
Gotchas
The inputs are cdlTensor, a custom type, so y_hat and y have to come from other ComfyDL nodes - you can't plug a native ComfyUI tensor in here. That's the price of the pack's self-contained design. And remember the no-averaging thing: if you feed it a whole batch, the output is a vector, and you need to reduce it yourself before feeding it to anything expecting one number.
This is also a young, niche pack - it has essentially zero community footprint yet (no threads, no tutorials to lean on), so if something behaves oddly, your best bet is the GitHub repo and the FUNCTIONS.md reference rather than a search box. The good news: this node is one line of textbook math, so there's not much to break.
Inputs (2)
| Name | Type | Default | Description |
|---|---|---|---|
| y_hat | TENSOR | — | |
| y | TENSOR | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| loss | TENSOR | — |