ComfyUI Node

Pt Median

The middle value, with a torch quirk you should know

By HowToSD·Created about a year ago·Updated about a year ago· 7
Pt Median
  • tens
  • TENSOR
dim
keepdimfalse

The median is the summary statistic that shrugs at outliers, and Pt Median brings it to the graph. It's torch.median along a single dimension. Where the mean gets dragged around by extreme values - a few garbage samples ruin everything - the median stays put, which makes it the robust choice for real-world data with noisy tails.

How it works

Three inputs, same layout as Pt Mean and Pt Max:

  • tens - the TENSOR.
  • dim - a STRING, but here's the first difference: it must be a single integer. 0, 1, fine. Lists and tuples are rejected with a type error, because torch's median only reduces one axis at a time. The other reduction nodes accept (1, 2); this one doesn't.
  • keepdim - boolean, default off.

Implementation is torch.median(tens, dim=dim, keepdim=keepdim), and it returns just the values - the source calls torch.median and discards the index half.

The quirk: even counts take the lower middle

This is the one worth committing to memory. When a dimension has an even number of elements, the true median is the average of the two middle values. torch.median doesn't do that - it returns the lower of the two. The pack's own docstring calls this out explicitly. So for a row of [1, 2, 3, 100], torch's median is 2, not 2.5. If you need the true interpolated median, you'd compute it yourself with sort-and-slice nodes; for most purposes the lower-middle is close enough and nobody notices.

When you'd use it

Anywhere a mean would be poisoned by outliers. Loss curves with the occasional exploded step, feature magnitudes with a few wild values, robustness checks on your dataset stats. If you're comparing a mean and a median and they disagree a lot, that disagreement is itself a signal that your data has outliers worth investigating.

Installing

Pt Median is part of the HowToSD/ComfyUI-Pt-Wrapper pack under the "Data Analysis" menu. ComfyUI Manager: search ComfyUI-Pt-Wrapper, install, restart. Manual:

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

The pack's heavy requirements (transformers, peft, accelerate…) are for the training side; median needs only PyTorch. Skip the pip line for math-only use. No models to download.

Pack-wide: the TENSOR type is separate from ComfyUI's IMAGE/LATENT - convert with Pt From Image (Pt From Image Transpose for (b, c, h, w)) and Pt To Image.

CategoryData Analysis

Inputs (3)

NameTypeDefaultDescription
tensTENSOR
dimSTRING
keepdimBOOLEANfalse

Outputs (1)

NameTypeDescription
TENSORTENSOR